Skip to content

Instantly share code, notes, and snippets.

@carlthuringer
Created April 23, 2014 19:56
Show Gist options
  • Save carlthuringer/11230097 to your computer and use it in GitHub Desktop.
Save carlthuringer/11230097 to your computer and use it in GitHub Desktop.
Composition or Decoration?
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