Class: Lutaml::Hal::Page
- Defined in:
- lib/lutaml/hal/page.rb
Overview
Models the pagination of a collection of resources This class is used to represent the pagination information for a collection of resources in the HAL format.
Class Method Summary collapse
Instance Method Summary collapse
-
#first ⇒ Object?
Returns the first page of results.
-
#first? ⇒ Boolean
Checks if there is a first page link available.
-
#first_page ⇒ Object?
Returns the first page link.
-
#last ⇒ Object?
Returns the last page of results.
-
#last? ⇒ Boolean
Checks if there is a last page link available.
-
#last_page ⇒ Object?
Returns the last page link.
-
#next ⇒ Object?
Returns the next page of results, or nil if on the last page.
-
#next? ⇒ Boolean
Checks if there is a next page available.
-
#next_page ⇒ Object?
Returns the next page link, or nil if on the last page.
-
#prev ⇒ Object?
Returns the previous page of results, or nil if on the first page.
-
#prev? ⇒ Boolean
Checks if there is a previous page available.
-
#prev_page ⇒ Object?
Returns the previous page link, or nil if on the first page.
-
#total_pages ⇒ Integer
Returns the total number of pages.
Methods inherited from Resource
Class Method Details
.inherited(subclass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lutaml/hal/page.rb', line 23 def self.inherited(subclass) super page_links_symbols = %i[self next prev first last up] subclass_name = subclass.name subclass.class_eval do # Define common page links page_links_symbols.each do |link_symbol| hal_link link_symbol, key: link_symbol.to_s, realize_class: subclass_name end end end |
Instance Method Details
#first ⇒ Object?
Returns the first page of results
72 73 74 75 76 |
# File 'lib/lutaml/hal/page.rb', line 72 def first return nil unless links.first links.first.realize end |
#first? ⇒ Boolean
Checks if there is a first page link available
111 112 113 |
# File 'lib/lutaml/hal/page.rb', line 111 def first? !links.first.nil? end |
#first_page ⇒ Object?
Returns the first page link
139 140 141 |
# File 'lib/lutaml/hal/page.rb', line 139 def first_page links.first end |
#last ⇒ Object?
Returns the last page of results
81 82 83 84 85 |
# File 'lib/lutaml/hal/page.rb', line 81 def last return nil unless links.last links.last.realize end |
#last? ⇒ Boolean
Checks if there is a last page link available
118 119 120 |
# File 'lib/lutaml/hal/page.rb', line 118 def last? !links.last.nil? end |
#last_page ⇒ Object?
Returns the last page link
146 147 148 |
# File 'lib/lutaml/hal/page.rb', line 146 def last_page links.last end |
#next ⇒ Object?
Returns the next page of results, or nil if on the last page
39 40 41 42 43 |
# File 'lib/lutaml/hal/page.rb', line 39 def next return nil unless links.next links.next.realize end |
#next? ⇒ Boolean
Checks if there is a next page available
97 98 99 |
# File 'lib/lutaml/hal/page.rb', line 97 def next? !links.next.nil? end |
#next_page ⇒ Object?
Returns the next page link, or nil if on the last page
125 126 127 |
# File 'lib/lutaml/hal/page.rb', line 125 def next_page links.next end |
#prev ⇒ Object?
Returns the previous page of results, or nil if on the first page
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lutaml/hal/page.rb', line 48 def prev # If the API provides a prev link, use it return links.prev.realize if links.prev # If we're on page 1, there's no previous page return nil if page <= 1 # Construct the previous page URL manually prev_page_url = construct_page_url(page - 1) return nil unless prev_page_url # Use the HAL register to fetch the previous page register_name = instance_variable_get("@#{Lutaml::Hal::REGISTER_ID_ATTR_NAME}") return nil unless register_name hal_register = Lutaml::Hal::GlobalRegister.instance.get(register_name) return nil unless hal_register hal_register.resolve_and_cast(nil, prev_page_url) end |
#prev? ⇒ Boolean
Checks if there is a previous page available
104 105 106 |
# File 'lib/lutaml/hal/page.rb', line 104 def prev? !links.prev.nil? end |
#prev_page ⇒ Object?
Returns the previous page link, or nil if on the first page
132 133 134 |
# File 'lib/lutaml/hal/page.rb', line 132 def prev_page links.prev end |
#total_pages ⇒ Integer
Returns the total number of pages
90 91 92 |
# File 'lib/lutaml/hal/page.rb', line 90 def total_pages pages end |