Created
April 23, 2014 19:56
-
-
Save carlthuringer/11230097 to your computer and use it in GitHub Desktop.
Composition or Decoration?
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
class Decorator | |
def self.decorate(subject) | |
subject.define_singleton_method(:with) do |delegate| | |
@delegate_list ||= [] | |
@delegate_list << delegate | |
end | |
subject.define_singleton_method(:method_missing) do |meth, *args, &block| | |
begin | |
@delegate_list.find do |delegate| | |
delegate.respond_to?(meth) | |
end.send(meth) | |
rescue | |
super(meth, *args, &block) | |
end | |
end | |
subject | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment