Created
June 6, 2016 20:55
-
-
Save LBRapid/c4be829f95105ac1c80d6d7a76638b88 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
class Something | |
attr_accessor :groups | |
def initialize | |
@groups = [] | |
end | |
def respond_to_missing?(method_sym, include_private = false) | |
@groups.map(&:name).detect { |name| name == method_sym.to_s } | |
end | |
def method_missing(method_sym, *args, &block) | |
@groups.detect { |group| group.name == method_sym.to_s } || super | |
end | |
end | |
class Group | |
attr_accessor :name, :data | |
def initialize(name, data) | |
@name = name | |
@data = data | |
end | |
def to_h | |
{ name: name, data: data } | |
end | |
end | |
s = Something.new | |
s.groups << Group.new('foobar', 'bazbat') | |
s.foobar.to_h | |
s.respond_to? :foobar #=> true | |
s.respond_to? :bingo #=> false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment