Last active
May 1, 2017 14:54
-
-
Save MatthewRDodds/b446976df06affb92d147ee7626d71f4 to your computer and use it in GitHub Desktop.
Ruby Decorator Recipe
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
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