Class: Flare::Collection
- Inherits:
-
Array
- Object
- Array
- Flare::Collection
- Defined in:
- lib/flare/collection.rb
Instance Attribute Summary collapse
-
#current_page ⇒ Object
readonly
Returns the value of attribute current_page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#response ⇒ Object
Returns the value of attribute response.
-
#total_entries ⇒ Object
readonly
Returns the value of attribute total_entries.
-
#total_pages ⇒ Object
readonly
Returns the value of attribute total_pages.
Class Method Summary collapse
- .create_from_response(response, page, per_page, options) ⇒ Object
- .ids_from_response(response, page, per_page, options) ⇒ Object
Instance Method Summary collapse
-
#initialize(page, per_page, total) ⇒ Collection
constructor
A new instance of Collection.
- #max_pages=(value) ⇒ Object
- #next_page ⇒ Object
- #offset ⇒ Object
- #previous_page ⇒ Object
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_page ⇒ Object (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_page ⇒ Object (readonly)
Returns the value of attribute per_page.
3 4 5 |
# File 'lib/flare/collection.rb', line 3 def per_page @per_page end |
#response ⇒ Object
Returns the value of attribute response.
4 5 6 |
# File 'lib/flare/collection.rb', line 4 def response @response end |
#total_entries ⇒ Object (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_pages ⇒ Object (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, ) # raise response.inspect collection = self.new(page, per_page, response['response']['numFound'] || 0) collection.response = response collection.replace(instantiate_objects(response, )) 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, ) 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_page ⇒ Object
35 36 37 |
# File 'lib/flare/collection.rb', line 35 def next_page current_page < total_pages ? (current_page + 1): nil end |
#offset ⇒ Object
39 40 41 |
# File 'lib/flare/collection.rb', line 39 def offset (current_page - 1) * @per_page end |
#previous_page ⇒ Object
31 32 33 |
# File 'lib/flare/collection.rb', line 31 def previous_page current_page > 1 ? (current_page - 1) : nil end |