Skip to content

Instantly share code, notes, and snippets.

@NinoScript
Created May 8, 2014 01:18
Show Gist options
  • Save NinoScript/73fbe785c5b7a3184fa2 to your computer and use it in GitHub Desktop.
Save NinoScript/73fbe785c5b7a3184fa2 to your computer and use it in GitHub Desktop.
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