Created
August 27, 2013 21:29
-
-
Save wm/6359415 to your computer and use it in GitHub Desktop.
Decorate an Enumerable collection
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 FancyDecorator < SimpleDelegator | |
def full_name | |
"#{first_name} #{last_name}" | |
end | |
def last_name_first | |
"#{last_name}, #{first_name}" | |
end | |
def self.decorate_collection(collection) | |
DecoratableCollectionProxy.new collection | |
end | |
class DecoratableCollectionProxy < SimpleDelegator | |
def each | |
__getobj__.each do |object| | |
yield FancyDecorator.new object | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment