Class: Draper::CollectionDecorator
- Inherits:
-
Object
- Object
- Draper::CollectionDecorator
- Extended by:
- Delegation
- Includes:
- QueryMethods, ViewHelpers, Enumerable
- Defined in:
- lib/draper/collection_decorator.rb
Instance Attribute Summary collapse
-
#context ⇒ Hash
Extra data to be used in user-defined methods, and passed to each item's decorator.
-
#decorator_class ⇒ Class
readonly
The decorator class used to decorate each item, as set by #initialize.
-
#object ⇒ Object
readonly
The collection being decorated.
Instance Method Summary collapse
- #decorated? ⇒ true
-
#decorated_collection ⇒ Array
The decorated items.
-
#initialize(object, options = {}) ⇒ CollectionDecorator
constructor
A new instance of CollectionDecorator.
- #kind_of?(klass) ⇒ Boolean (also: #is_a?)
- #replace(other) ⇒ Object
- #to_s ⇒ Object
Methods included from Delegation
Methods included from QueryMethods
Methods included from ViewHelpers
Constructor Details
#initialize(object, options = {}) ⇒ CollectionDecorator
Returns a new instance of CollectionDecorator.
30 31 32 33 34 35 |
# File 'lib/draper/collection_decorator.rb', line 30 def initialize(object, = {}) .assert_valid_keys(:with, :context) @object = object @decorator_class = [:with] @context = .fetch(:context, {}) end |
Instance Attribute Details
#context ⇒ Hash
Returns extra data to be used in user-defined methods, and passed to each item's decorator.
17 18 19 |
# File 'lib/draper/collection_decorator.rb', line 17 def context @context end |
#decorator_class ⇒ Class (readonly)
Returns the decorator class used to decorate each item, as set by #initialize.
13 14 15 |
# File 'lib/draper/collection_decorator.rb', line 13 def decorator_class @decorator_class end |
#object ⇒ Object (readonly)
Returns the collection being decorated.
9 10 11 |
# File 'lib/draper/collection_decorator.rb', line 9 def object @object end |
Instance Method Details
#decorated? ⇒ true
58 59 60 |
# File 'lib/draper/collection_decorator.rb', line 58 def decorated? true end |
#decorated_collection ⇒ Array
Returns the decorated items.
42 43 44 |
# File 'lib/draper/collection_decorator.rb', line 42 def decorated_collection @decorated_collection ||= object.map{|item| decorate_item(item)} end |
#kind_of?(klass) ⇒ Boolean Also known as: is_a?
64 65 66 |
# File 'lib/draper/collection_decorator.rb', line 64 def kind_of?(klass) decorated_collection.kind_of?(klass) || super end |
#replace(other) ⇒ Object
70 71 72 73 |
# File 'lib/draper/collection_decorator.rb', line 70 def replace(other) decorated_collection.replace(other) self end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/draper/collection_decorator.rb', line 48 def to_s "#<#{self.class.name} of #{decorator_class || "inferred decorators"} for #{object.inspect}>" end |