Class: Liquid::Rails::CollectionDrop

Inherits:
Drop
  • Object
show all
Defined in:
lib/liquid4-rails/drops/collection_drop.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  options.assert_valid_keys(:with, :scope)

  @objects         = options[:scope].nil? ? objects : objects.send(options[:scope])
  @drop_class_name = options[:with]
end

Class Attribute Details

._scopesObject

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_collectionObject



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

#inspectObject



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?

Returns:

  • (Boolean)


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_liquidObject

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