Created
May 8, 2014 01:18
-
-
Save NinoScript/73fbe785c5b7a3184fa2 to your computer and use it in GitHub Desktop.
This file contains 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
module Helpers | |
def arguments(method:, binding:) | |
method.parameters.inject({}) do |args, (type, name)| | |
args[name] = { | |
type:type, | |
value:binding.eval(name.to_s) | |
} | |
args | |
end | |
end | |
module_function :arguments | |
end | |
class Hash | |
def populate(method:, binding:) | |
arguments = Helpers.arguments method:method, binding:binding | |
arguments.each do |arg_name, arg_info| | |
next if arg_info[:type] == :key && arg_info[:value] == nil | |
self[arg_name] = arg_info[:value] | |
end | |
end | |
end | |
module NetworkAPI | |
class Response < Hash | |
def initialize(status:, data:, code:nil, message:nil) | |
populate method:method(__method__), binding:binding | |
end | |
end | |
end | |
response_hash = NetworkAPI::Response.new status:"OK", data: {a:123}, message: "optional" | |
p response_hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment