Skip to content

Commit

Permalink
Adds the ability to decorate an enumerable proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbristol committed Aug 29, 2012
1 parent 1104b1f commit 67c7125
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/draper/decorated_enumerable_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ class DecoratedEnumerableProxy
include Enumerable

delegate :as_json, :collect, :map, :each, :[], :all?, :include?, :first, :last, :shift, :to => :decorated_collection



# Initialize a new collection decorator instance by passing in
# an instance of a collection. Pass in an optional
# context into the options hash is stored for later use.
#
#
# @param [Object] instances to wrap
# @param [Hash] options (optional)
# @option options [Class] :klass The decorator class to use
# for each item in the collection.
# @option options all other options are passed to Decorator
# class for each item.

def self.decorate(collection, options = {})
new( collection, discern_class_from_my_class(options.delete(:klass)), options)
end

def initialize(collection, klass, options = {})
@wrapped_collection, @klass, @options = collection, klass, options
Expand Down Expand Up @@ -70,5 +88,15 @@ def source
@wrapped_collection
end
alias_method :to_source, :source


private
def self.discern_class_from_my_class default_class
return default_class if default_class
name = InvoiceDecorator.to_s.gsub("Decorator", "")
"#{name.singularize}Decorator".constantize
rescue NameError
raise NameError("You must supply a class (as the klass option) for the members of your collection or the class must be inferable from the name of this class ('#{new.class}')")
end
end
end

0 comments on commit 67c7125

Please sign in to comment.