Skip to content

Instantly share code, notes, and snippets.

@lukaszx0
Created July 27, 2015 11:30
Show Gist options
  • Save lukaszx0/feee902c0b1838ebeb69 to your computer and use it in GitHub Desktop.
Save lukaszx0/feee902c0b1838ebeb69 to your computer and use it in GitHub Desktop.
container class
class Container
SETTER_METHOD_REGEX = /^(?<name>\w+)=$/.freeze
def initialize(params = {})
@registry = {}
@registry.merge(params.symbolize_keys) if params.present?
end
def method_missing(method_name, *args, &block)
matched = method_name.to_s.match(SETTER_METHOD_REGEX)
if matched
@registry[matched[:name].to_sym] = args.first
else
if @registry.key?(method_name)
@registry.fetch(method_name)
else
super
end
end
end
def respond_to_missing?(method_name, include_private = false)
method_name =~ SETTER_METHOD_REGEX ||
key?(method_name) ||
super
end
def key?(key)
@registry.key?(key)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment