Class: JsonApiClient::Paginating::NestedParamPaginator
- Inherits:
-
Object
- Object
- JsonApiClient::Paginating::NestedParamPaginator
- Defined in:
- lib/json_api_client/paginating/nested_param_paginator.rb
Overview
Constant Summary collapse
- DEFAULT_WRAPPER_NAME =
"page".freeze
- DEFAULT_PAGE_PARAM =
"page".freeze
- DEFAULT_PER_PAGE_PARAM =
"per_page".freeze
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.
Class Method Summary collapse
- .page_param ⇒ Object
- .page_param=(param = DEFAULT_PAGE_PARAM) ⇒ Object
- .per_page_param ⇒ Object
- .per_page_param=(param = DEFAULT_PER_PAGE_PARAM) ⇒ Object
- .wrapper_name ⇒ Object
- .wrapper_name=(param = DEFAULT_WRAPPER_NAME) ⇒ Object
Instance Method Summary collapse
- #current_page ⇒ Object
- #first ⇒ Object
-
#initialize(result_set, data) ⇒ NestedParamPaginator
constructor
A new instance of NestedParamPaginator.
- #last ⇒ Object
- #next ⇒ Object
- #next_page ⇒ Object
- #offset ⇒ Object
- #out_of_bounds? ⇒ Boolean
- #page_param ⇒ Object
- #per_page ⇒ Object (also: #limit_value)
- #per_page_param ⇒ Object
- #prev ⇒ Object
- #previous_page ⇒ Object
- #total_count ⇒ Object
-
#total_entries ⇒ Object
this is an estimate, not necessarily an exact count.
- #total_pages ⇒ Object
Constructor Details
#initialize(result_set, data) ⇒ NestedParamPaginator
Returns a new instance of NestedParamPaginator.
57 58 59 60 61 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 57 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.
55 56 57 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55 def links @links end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
55 56 57 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55 def params @params end |
#result_set ⇒ Object (readonly)
Returns the value of attribute result_set.
55 56 57 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 55 def result_set @result_set end |
Class Method Details
.page_param ⇒ Object
25 26 27 28 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 25 def page_param @_page_param ||= DEFAULT_PAGE_PARAM "#{wrapper_name}[#{@_page_param}]" end |
.page_param=(param = DEFAULT_PAGE_PARAM) ⇒ Object
30 31 32 33 34 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 30 def page_param=(param = DEFAULT_PAGE_PARAM) raise ArgumentError, "don't wrap page_param" unless valid_param?(param) @_page_param = param.to_s end |
.per_page_param ⇒ Object
36 37 38 39 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 36 def per_page_param @_per_page_param ||= DEFAULT_PER_PAGE_PARAM "#{wrapper_name}[#{@_per_page_param}]" end |
.per_page_param=(param = DEFAULT_PER_PAGE_PARAM) ⇒ Object
41 42 43 44 45 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 41 def per_page_param=(param = DEFAULT_PER_PAGE_PARAM) raise ArgumentError, "don't wrap per_page_param" unless valid_param?(param) @_per_page_param = param end |
.wrapper_name ⇒ Object
15 16 17 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 15 def wrapper_name @_wrapper_name ||= DEFAULT_WRAPPER_NAME end |
.wrapper_name=(param = DEFAULT_WRAPPER_NAME) ⇒ Object
19 20 21 22 23 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 19 def wrapper_name=(param = DEFAULT_WRAPPER_NAME) raise ArgumentError, "don't wrap wrapper_name" unless valid_param?(param) @_wrapper_name = param.to_s end |
Instance Method Details
#current_page ⇒ Object
105 106 107 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 105 def current_page params.fetch(page_param, 1).to_i end |
#first ⇒ Object
71 72 73 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 71 def first result_set.links.fetch_link("first") end |
#last ⇒ Object
75 76 77 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 75 def last result_set.links.fetch_link("last") end |
#next ⇒ Object
63 64 65 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 63 def next result_set.links.fetch_link("next") end |
#next_page ⇒ Object
117 118 119 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 117 def next_page current_page < total_pages ? (current_page + 1) : nil end |
#offset ⇒ Object
95 96 97 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 95 def offset per_page * (current_page - 1) end |
#out_of_bounds? ⇒ Boolean
109 110 111 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 109 def out_of_bounds? current_page > total_pages end |
#page_param ⇒ Object
121 122 123 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 121 def page_param self.class.page_param end |
#per_page ⇒ Object Also known as: limit_value
99 100 101 102 103 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 99 def per_page params.fetch(per_page_param) do result_set.length end.to_i end |
#per_page_param ⇒ Object
125 126 127 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 125 def per_page_param self.class.per_page_param end |
#prev ⇒ Object
67 68 69 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 67 def prev result_set.links.fetch_link("prev") end |
#previous_page ⇒ Object
113 114 115 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 113 def previous_page current_page > 1 ? (current_page - 1) : nil end |
#total_count ⇒ Object
93 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 93 def total_count; total_entries; end |
#total_entries ⇒ Object
this is an estimate, not necessarily an exact count
90 91 92 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 90 def total_entries per_page * total_pages end |
#total_pages ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/json_api_client/paginating/nested_param_paginator.rb', line 79 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, &method(:current_page)).to_i else current_page end end |