Class: Drip::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/drip/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_items) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
# File 'lib/drip/collection.rb', line 9

def initialize(raw_items)
  @raw_items = raw_items.dup.freeze
  @items = parse_items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/drip/collection.rb', line 7

def items
  @items
end

#raw_itemsObject (readonly)

Returns the value of attribute raw_items.



7
8
9
# File 'lib/drip/collection.rb', line 7

def raw_items
  @raw_items
end

Class Method Details

.collection_nameObject



14
15
16
# File 'lib/drip/collection.rb', line 14

def self.collection_name
  "resources"
end

.resource_nameObject



18
19
20
# File 'lib/drip/collection.rb', line 18

def self.resource_name
  "resource"
end

Instance Method Details

#each(&block) ⇒ Object



37
38
39
# File 'lib/drip/collection.rb', line 37

def each(&block)
  items.each { |item| yield(item) }
end

#item_classObject



22
23
24
25
# File 'lib/drip/collection.rb', line 22

def item_class
  @item_class ||= Drip::Resources.
    find_class(self.class.resource_name)
end

#parse_itemsObject



27
28
29
30
31
# File 'lib/drip/collection.rb', line 27

def parse_items
  raw_items.map do |raw_item|
    raw_item.is_a?(String) ? raw_item : item_class.new(raw_item)
  end
end

#singular?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/drip/collection.rb', line 33

def singular?
  items.length < 2
end