class Draper::CollectionDecorator
Attributes
context[RW]
@return [Hash] extra data to be used in user-defined methods, and passed
to each item's decorator.
decorator_class[R]
@return [Class] the decorator class used to decorate each item, as set by
{#initialize}.
object[R]
@return the collection being decorated.
Public Class Methods
new(object, options = {})
click to toggle source
@param [Enumerable] object
collection to decorate.
@option options [Class, nil] :with (nil)
the decorator class used to decorate each item. When `nil`, each item's {Decoratable#decorate decorate} method will be used.
@option options [Hash] :context ({})
extra data to be stored in the collection decorator and used in user-defined methods, and passed to each item's decorator.
# File lib/draper/collection_decorator.rb, line 30 def initialize(object, options = {}) options.assert_valid_keys(:with, :context) @object = object @decorator_class = options[:with] @context = options.fetch(:context, {}) end
Also aliased as: decorate
Public Instance Methods
context=(value)
click to toggle source
# File lib/draper/collection_decorator.rb, line 52 def context=(value) @context = value each {|item| item.context = value } if @decorated_collection end
decorated?()
click to toggle source
@return [true]
# File lib/draper/collection_decorator.rb, line 58 def decorated? true end
decorated_collection()
click to toggle source
@return [Array] the decorated items.
# File lib/draper/collection_decorator.rb, line 42 def decorated_collection @decorated_collection ||= object.map{|item| decorate_item(item)} end
kind_of?(klass)
click to toggle source
Calls superclass method
# File lib/draper/collection_decorator.rb, line 64 def kind_of?(klass) decorated_collection.kind_of?(klass) || super end
Also aliased as: is_a?
replace(other)
click to toggle source
# File lib/draper/collection_decorator.rb, line 70 def replace(other) decorated_collection.replace(other) self end
to_s()
click to toggle source
# File lib/draper/collection_decorator.rb, line 48 def to_s "#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{object.inspect}>" end
Protected Instance Methods
decorate_item(item)
click to toggle source
Decorates the given item.
# File lib/draper/collection_decorator.rb, line 78 def decorate_item(item) item_decorator.call(item, context: context) end
Private Instance Methods
item_decorator()
click to toggle source
# File lib/draper/collection_decorator.rb, line 84 def item_decorator if decorator_class decorator_class.method(:decorate) else ->(item, options) { item.decorate(options) } end end