Class: Liquid::Rails::CollectionDrop
- Inherits:
-
Drop
- Object
- Drop
- Liquid::Rails::CollectionDrop
- Defined in:
- lib/liquid4-rails/drops/collection_drop.rb
Class Attribute Summary collapse
-
._scopes ⇒ Object
Returns the value of attribute _scopes.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](method) ⇒ Object
:[] is invoked by parser before the actual.
- #dropped_collection ⇒ Object
-
#initialize(objects, options = {}) ⇒ CollectionDrop
constructor
A new instance of CollectionDrop.
- #inspect ⇒ Object
- #kind_of?(klass) ⇒ Boolean (also: #is_a?)
- #page(number) ⇒ Object
- #per(number) ⇒ Object
-
#to_liquid ⇒ Object
Need to override this.
Constructor Details
#initialize(objects, options = {}) ⇒ CollectionDrop
Returns a new instance of CollectionDrop.
31 32 33 34 35 36 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 31 def initialize(objects, ={}) .assert_valid_keys(:with, :scope) @objects = [:scope].nil? ? objects : objects.send([:scope]) @drop_class_name = [:with] end |
Class Attribute Details
._scopes ⇒ Object
Returns the value of attribute _scopes.
6 7 8 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 6 def _scopes @_scopes end |
Class Method Details
.inherited(base) ⇒ Object
9 10 11 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 9 def self.inherited(base) base._scopes = [] end |
.scope(*scope_names) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 13 def self.scope(*scope_names) @_scopes.concat scope_names scope_names.each do |scope_name| define_method(scope_name) do value = instance_variable_get("@_#{scope_name}") return value if value raise ArgumentError, "#{objects.class.name} doesn't define scope: #{scope_name}" unless objects.respond_to?(scope_name) instance_variable_set("@_#{scope_name}", self.class.new(objects.send(scope_name))) end end end |
Instance Method Details
#[](method) ⇒ Object
:[] is invoked by parser before the actual. However, this method has the same name as array method. Override this, so it will work for both cases. post_drop.comments } post_drop.<other_methods> }
59 60 61 62 63 64 65 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 59 def [](method) if method.is_a?(Integer) dropped_collection.at(method) else public_send(method) end end |
#dropped_collection ⇒ Object
46 47 48 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 46 def dropped_collection @dropped_collection ||= @objects.map { |item| drop_item(item) } end |
#inspect ⇒ Object
73 74 75 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 73 def inspect "#<#{self.class.name} of #{drop_class} for #{objects.inspect}>" end |
#kind_of?(klass) ⇒ Boolean Also known as: is_a?
50 51 52 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 50 def kind_of?(klass) dropped_collection.kind_of?(klass) || super end |
#page(number) ⇒ Object
38 39 40 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 38 def page(number) self.class.new(objects.page(number)) end |
#per(number) ⇒ Object
42 43 44 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 42 def per(number) self.class.new(objects.per(number)) end |
#to_liquid ⇒ Object
Need to override this. I don’t understand too, otherwise it will return an array of drop objects. Need to return self so that we can do chaining.
69 70 71 |
# File 'lib/liquid4-rails/drops/collection_drop.rb', line 69 def to_liquid self end |