Class: Tagcrumbs::Collection

Inherits:
Resource show all
Defined in:
lib/tagcrumbs/resources/collection.rb

Overview

Basically a regular Array that holds many Resources. Used to get collections from the webservice.

Constant Summary collapse

ATTRIBUTES =
[:total_entries, :pages, :per_page, :page, :sort, :order, :next_page, :previous_page]

Instance Attribute Summary collapse

Attributes inherited from Resource

#loaded, #properties, #requestor, #resource_url

Instance Method Summary collapse

Methods inherited from Resource

load, #loaded?, new_from_document, #reload

Constructor Details

#initializeCollection

Returns a new instance of Collection.



23
24
25
# File 'lib/tagcrumbs/resources/collection.rb', line 23

def initialize
  reset!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

delegate unkown methods to the underlying array



72
73
74
75
76
77
78
# File 'lib/tagcrumbs/resources/collection.rb', line 72

def method_missing(name, *args, &block)
  if @array.respond_to?(name)
    @array.send(name, *args, &block) 
  else
    super
  end
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



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

def array
  @array
end

Instance Method Details

#each_page(args = {}) ⇒ Object

Iterate over all pages



46
47
48
49
50
# File 'lib/tagcrumbs/resources/collection.rb', line 46

def each_page(args={})
  begin
    yield(self)
  end while(args[:reverse] == true ? previous_page! : next_page!)
end

#each_page_each_item(args = {}) ⇒ Object

Iterate over all items with pagination



53
54
55
56
57
58
59
60
# File 'lib/tagcrumbs/resources/collection.rb', line 53

def each_page_each_item(args={})
  self.each_page(args) do |collection|
    collection.each do |item|
      yield item
    end
  end
  
end

#initialize_from_document(parser, format = ) ⇒ Object

Load data into the object from a document



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tagcrumbs/resources/collection.rb', line 85

def initialize_from_document(parser, format = Tagcrumbs.options[:format])
  parser = super

  if self.loaded?
    nodes = parser.nodes_with_name(parser.name.singularize)
    return if nodes.blank?
    
    
    nodes.each do |node|
      node_parser = Parser.new_for_format(node, parser.format)
      node_type = node_parser.get_attribute('type')

      if node_type # a nested model
        @array << Tagcrumbs.class_by_type(node_type).new_from_document(node, parser.format)
      else # a nested node
        @array << node_parser.get_value 
      end
    end
  end
end

#inspectObject



80
81
82
# File 'lib/tagcrumbs/resources/collection.rb', line 80

def inspect
  @array.inspect
end

#next_page!Object

Replace the Collection with the next page of elements if there is one



36
37
38
# File 'lib/tagcrumbs/resources/collection.rb', line 36

def next_page!
  reload(next_page) if next_page.present?
end

#previous_page!Object

Replace the Collection with the privious page of elements if there is one



41
42
43
# File 'lib/tagcrumbs/resources/collection.rb', line 41

def previous_page!
  reload(previous_page) if previous_page.present?
end

#properties=(properties) ⇒ Object

Set the properties and typecast some attributes automatically



63
64
65
66
67
68
69
# File 'lib/tagcrumbs/resources/collection.rb', line 63

def properties=(properties)
  super
  
  self.properties.each do |key, value|
    send("#{key}=", value) if ATTRIBUTES.include?(key.to_sym)
  end
end

#reset!Object

Reset the state of the Array



28
29
30
31
32
33
# File 'lib/tagcrumbs/resources/collection.rb', line 28

def reset!
  super
  @array = []  

  true
end