Class: JSONAPI::Consumer::Paginating::Paginator
- Inherits:
-
Object
- Object
- JSONAPI::Consumer::Paginating::Paginator
- Defined in:
- lib/jsonapi/consumer/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.
12 13 14 15 16 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 12 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.
10 11 12 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 10 def links @links end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
10 11 12 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 10 def params @params end |
#result_set ⇒ Object (readonly)
Returns the value of attribute result_set.
10 11 12 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 10 def result_set @result_set end |
Instance Method Details
#current_page ⇒ Object
62 63 64 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 62 def current_page params.fetch(page_param, 1).to_i end |
#first ⇒ Object
26 27 28 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 26 def first result_set.links.fetch_link("first") end |
#last ⇒ Object
30 31 32 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 30 def last result_set.links.fetch_link("last") end |
#next ⇒ Object
18 19 20 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 18 def next result_set.links.fetch_link("next") end |
#next_page ⇒ Object
74 75 76 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 74 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
52 53 54 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 52 def offset per_page * (current_page - 1) end |
#out_of_bounds? ⇒ Boolean
66 67 68 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 66 def out_of_bounds? current_page > total_pages end |
#per_page ⇒ Object Also known as: limit_value
56 57 58 59 60 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 56 def per_page params.fetch(per_page_param) do result_set.length end.to_i end |
#prev ⇒ Object
22 23 24 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 22 def prev result_set.links.fetch_link("prev") end |
#previous_page ⇒ Object
70 71 72 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 70 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#total_count ⇒ Object
50 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 50 def total_count; total_entries; end |
#total_entries ⇒ Object
this number may be off
47 48 49 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 47 def total_entries per_page * total_pages end |
#total_pages ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jsonapi/consumer/paginating/paginator.rb', line 34 def total_pages if links["last"] uri = result_set.links.link_url_for("last") last_params = params_for_uri(uri) last_params.fetch(page_param) do current_page end.to_i else current_page end end |