Class: Reviewed::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/reviewed/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, klass, response, params = {}) ⇒ Collection

Returns a new instance of Collection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/reviewed/collection.rb', line 11

def initialize(client, klass, response, params={})
  body = response.body
  data = body.data

  @client = client
  @klass = klass
  @params = params
  @items = []
  @page_attributes = body.pagination || {}

  data.each do |obj|
    self.items << klass.new(obj, client)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



48
49
50
51
52
53
54
55
# File 'lib/reviewed/collection.rb', line 48

def method_missing(sym, *args, &block)
  clean_sym = sym.to_s.gsub(/\?/, '').to_sym
  if @page_attributes.has_key?(clean_sym)
    @page_attributes[clean_sym]
  else
    super
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#page_attributesObject

Returns the value of attribute page_attributes.



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

def page_attributes
  @page_attributes
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#raw_responseObject

Returns the value of attribute raw_response.



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

def raw_response
  @raw_response
end

Instance Method Details

#limit_valueObject



26
27
28
# File 'lib/reviewed/collection.rb', line 26

def limit_value
  per_page
end

#next_pageObject



38
39
40
41
# File 'lib/reviewed/collection.rb', line 38

def next_page
  return nil if @page_attributes[:last_page]
  fetch_page(@page_attributes[:next_page])
end

#num_pagesObject



30
31
32
# File 'lib/reviewed/collection.rb', line 30

def num_pages
  total_pages
end

#previous_pageObject



43
44
45
46
# File 'lib/reviewed/collection.rb', line 43

def previous_page
  return nil if @page_attributes[:first_page]
  fetch_page(@page_attributes[:previous_page])
end

#total_countObject



34
35
36
# File 'lib/reviewed/collection.rb', line 34

def total_count
  total
end