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
create_link_class, create_link_set_class, #embedded_data, from_embedded, #get_embedded, get_link_set_class, hal_link, #has_embedded?, init_links_definition
Class Method Details
.inherited(subclass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/lutaml/hal/page.rb', line 23 def self.inherited(subclass) super # Skip automatic link creation for anonymous classes (used in tests) return unless subclass.name 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
75 76 77 78 79 |
# File 'lib/lutaml/hal/page.rb', line 75 def first return nil unless links.first links.first.realize end |
#first? ⇒ Boolean
Checks if there is a first page link available
114 115 116 |
# File 'lib/lutaml/hal/page.rb', line 114 def first? !links.first.nil? end |
#first_page ⇒ Object?
Returns the first page link
142 143 144 |
# File 'lib/lutaml/hal/page.rb', line 142 def first_page links.first end |
#last ⇒ Object?
Returns the last page of results
84 85 86 87 88 |
# File 'lib/lutaml/hal/page.rb', line 84 def last return nil unless links.last links.last.realize end |
#last? ⇒ Boolean
Checks if there is a last page link available
121 122 123 |
# File 'lib/lutaml/hal/page.rb', line 121 def last? !links.last.nil? end |
#last_page ⇒ Object?
Returns the last page link
149 150 151 |
# File 'lib/lutaml/hal/page.rb', line 149 def last_page links.last end |
#next ⇒ Object?
Returns the next page of results, or nil if on the last page
42 43 44 45 46 |
# File 'lib/lutaml/hal/page.rb', line 42 def next return nil unless links.next links.next.realize end |
#next? ⇒ Boolean
Checks if there is a next page available
100 101 102 |
# File 'lib/lutaml/hal/page.rb', line 100 def next? !links.next.nil? end |
#next_page ⇒ Object?
Returns the next page link, or nil if on the last page
128 129 130 |
# File 'lib/lutaml/hal/page.rb', line 128 def next_page links.next end |
#prev ⇒ Object?
Returns the previous page of results, or nil if on the first page
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lutaml/hal/page.rb', line 51 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
107 108 109 |
# File 'lib/lutaml/hal/page.rb', line 107 def prev? !links.prev.nil? end |
#prev_page ⇒ Object?
Returns the previous page link, or nil if on the first page
135 136 137 |
# File 'lib/lutaml/hal/page.rb', line 135 def prev_page links.prev end |
#total_pages ⇒ Integer
Returns the total number of pages
93 94 95 |
# File 'lib/lutaml/hal/page.rb', line 93 def total_pages pages end |