Created
February 11, 2013 21:05
-
-
Save tylerhunt/4757604 to your computer and use it in GitHub Desktop.
Simple Decorator
This file contains hidden or 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 < SimpleDelegator | |
| class Collection < SimpleDelegator | |
| include Enumerable | |
| def initialize(collection, decorator) | |
| super(collection) | |
| @decorator = decorator | |
| end | |
| def each | |
| return to_enum unless block_given? | |
| __getobj__.each { |object| yield @decorator.decorate(object) } | |
| end | |
| def to_ary | |
| to_a | |
| end | |
| end | |
| def self.decorate(object) | |
| new(object) | |
| end | |
| def self.decorate_collection(collection) | |
| Collection.new(collection, self) | |
| end | |
| # This allows decorated objects to be passed directly to named route helpers. | |
| def eql?(other) | |
| if other.is_a?(self.class) | |
| __getobj__.eql?(other.__getobj__) | |
| else | |
| super | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment