Class: JsonApiClient::Paginating::Paginator
- Inherits:
-
Object
- Object
- JsonApiClient::Paginating::Paginator
- Defined in:
- lib/json_api_client/paginating/paginator.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#result_set ⇒ Object
readonly
Returns the value of attribute result_set.
Instance Method Summary collapse
- #current_page ⇒ Object
- #first ⇒ Object
-
#initialize(result_set, data) ⇒ Paginator
constructor
A new instance of Paginator.
- #last ⇒ Object
- #next ⇒ Object
- #next_page ⇒ Object
- #offset ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #per_page ⇒ Object (also: #limit_value)
- #prev ⇒ Object
- #previous_page ⇒ Object
- #total_count ⇒ Object
-
#total_entries ⇒ Object
this number may be off.
- #total_pages ⇒ Object
Constructor Details
#initialize(result_set, data) ⇒ Paginator
Returns a new instance of Paginator.
5 6 7 8 9 |
# File 'lib/json_api_client/paginating/paginator.rb', line 5 def initialize(result_set, data) @params = params_for_uri(result_set.uri) @result_set = result_set @links = data['links'] end |
Instance Attribute Details
#links ⇒ Object (readonly)
Returns the value of attribute links.
4 5 6 |
# File 'lib/json_api_client/paginating/paginator.rb', line 4 def links @links end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/json_api_client/paginating/paginator.rb', line 4 def params @params end |
#result_set ⇒ Object (readonly)
Returns the value of attribute result_set.
4 5 6 |
# File 'lib/json_api_client/paginating/paginator.rb', line 4 def result_set @result_set end |
Instance Method Details
#current_page ⇒ Object
55 56 57 |
# File 'lib/json_api_client/paginating/paginator.rb', line 55 def current_page params.fetch("page", 1).to_i end |
#first ⇒ Object
19 20 21 |
# File 'lib/json_api_client/paginating/paginator.rb', line 19 def first result_set.links.fetch_link("first") end |
#last ⇒ Object
23 24 25 |
# File 'lib/json_api_client/paginating/paginator.rb', line 23 def last result_set.links.fetch_link("last") end |
#next ⇒ Object
11 12 13 |
# File 'lib/json_api_client/paginating/paginator.rb', line 11 def next result_set.links.fetch_link("next") end |
#next_page ⇒ Object
67 68 69 |
# File 'lib/json_api_client/paginating/paginator.rb', line 67 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
45 46 47 |
# File 'lib/json_api_client/paginating/paginator.rb', line 45 def offset per_page * (current_page - 1) end |
#out_of_bounds? ⇒ Boolean
59 60 61 |
# File 'lib/json_api_client/paginating/paginator.rb', line 59 def out_of_bounds? current_page > total_pages end |
#per_page ⇒ Object Also known as: limit_value
49 50 51 52 53 |
# File 'lib/json_api_client/paginating/paginator.rb', line 49 def per_page params.fetch("per_page") do result_set.length end.to_i end |
#prev ⇒ Object
15 16 17 |
# File 'lib/json_api_client/paginating/paginator.rb', line 15 def prev result_set.links.fetch_link("prev") end |
#previous_page ⇒ Object
63 64 65 |
# File 'lib/json_api_client/paginating/paginator.rb', line 63 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#total_count ⇒ Object
43 |
# File 'lib/json_api_client/paginating/paginator.rb', line 43 def total_count; total_entries; end |
#total_entries ⇒ Object
this number may be off
40 41 42 |
# File 'lib/json_api_client/paginating/paginator.rb', line 40 def total_entries per_page * total_pages end |
#total_pages ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/json_api_client/paginating/paginator.rb', line 27 def total_pages if links["last"] uri = result_set.links.link_url_for("last") last_params = params_for_uri(uri) last_params.fetch("page") do current_page end.to_i else current_page end end |