Skip to content

Instantly share code, notes, and snippets.

@MatthewRDodds
Last active May 1, 2017 14:54
Show Gist options
  • Save MatthewRDodds/b446976df06affb92d147ee7626d71f4 to your computer and use it in GitHub Desktop.
Save MatthewRDodds/b446976df06affb92d147ee7626d71f4 to your computer and use it in GitHub Desktop.
Ruby Decorator Recipe
module Decorator
def initialize(component)
@component = component
end
def method_missing(meth, *args)
if @component.respond_to?(meth)
@component.send(meth, *args)
else
super
end
end
def respond_to?(meth)
@component.respond_to?(meth)
end
end
class UserDecorator < User
include Decorator
def full_name
first_name + ' ' + last_name
end
end
# Credit: https://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment