Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created February 11, 2013 21:05
Show Gist options
  • Select an option

  • Save tylerhunt/4757604 to your computer and use it in GitHub Desktop.

Select an option

Save tylerhunt/4757604 to your computer and use it in GitHub Desktop.
Simple Decorator
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