Created
July 27, 2015 11:30
-
-
Save lukaszx0/feee902c0b1838ebeb69 to your computer and use it in GitHub Desktop.
container class
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
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