Created
January 10, 2020 17:09
-
-
Save makaroni4/1ec6b83f540f2bd502a78b5bd5eb4c28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "ostruct" | |
class User | |
def initialize(basic_params) | |
@json_params = OpenStruct.new(basic_params) | |
end | |
def full_name | |
[ | |
@json_params.first_name, | |
@json_params.last_name | |
].join(" ") | |
end | |
def method_missing(m) | |
if @json_params.respond_to?(m) | |
@json_params.send(m) | |
else | |
raise "Undefined method #{m}" | |
end | |
end | |
end | |
user_json = { | |
first_name: "Foo", | |
last_name: "Bar" | |
} | |
user = User.new(user_json) | |
user.first_name | |
# => Foo | |
user.last_name | |
# => Bar | |
user.full_name | |
# => Foo Bar | |
user.blah | |
# => RuntimeError: Undefined method blah |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment