Class: Flare::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/flare/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, per_page, total) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
# File 'lib/flare/collection.rb', line 6

def initialize(page, per_page, total)
  @current_page, @per_page, @total_entries = page, per_page, total
  
  @total_pages = (@total_entries / @per_page.to_f).ceil
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/flare/collection.rb', line 3

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/flare/collection.rb', line 3

def per_page
  @per_page
end

#responseObject

Returns the value of attribute response.



4
5
6
# File 'lib/flare/collection.rb', line 4

def response
  @response
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



3
4
5
# File 'lib/flare/collection.rb', line 3

def total_entries
  @total_entries
end

#total_pagesObject (readonly)

Returns the value of attribute total_pages.



3
4
5
# File 'lib/flare/collection.rb', line 3

def total_pages
  @total_pages
end

Class Method Details

.create_from_response(response, page, per_page, options) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/flare/collection.rb', line 19

def self.create_from_response(response, page, per_page, options)
  # raise response.inspect
  collection = self.new(page, per_page, response['response']['numFound'] || 0)
  collection.response = response      
  collection.replace(instantiate_objects(response, options))
  return collection
end

.ids_from_response(response, page, per_page, options) ⇒ Object



12
13
14
15
16
17
# File 'lib/flare/collection.rb', line 12

def self.ids_from_response(response, page, per_page, options)
  collection = self.new(page, per_page, response['response']['numFound'] || 0)
  collection.response = response
  collection.replace(response['response']['docs'].map {|doc| doc['id']})
  return collection
end

Instance Method Details

#max_pages=(value) ⇒ Object



27
28
29
# File 'lib/flare/collection.rb', line 27

def max_pages=(value)
  @total_pages = value if value < @total_pages
end

#next_pageObject



35
36
37
# File 'lib/flare/collection.rb', line 35

def next_page
  current_page < total_pages ? (current_page + 1): nil
end

#offsetObject



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

def offset
  (current_page - 1) * @per_page
end

#previous_pageObject



31
32
33
# File 'lib/flare/collection.rb', line 31

def previous_page 
  current_page > 1 ? (current_page - 1) : nil
end