Class: Lutaml::Hal::Page

Inherits:
Resource
  • Object
show all
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

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

#firstObject?

Returns the first page of results

Returns:

  • (Object, nil)

    The first page instance or nil



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

Returns:

  • (Boolean)

    true if first page link exists, false otherwise



114
115
116
# File 'lib/lutaml/hal/page.rb', line 114

def first?
  !links.first.nil?
end

#first_pageObject?

Returns the first page link

Returns:

  • (Object, nil)

    The first page link or nil



142
143
144
# File 'lib/lutaml/hal/page.rb', line 142

def first_page
  links.first
end

#lastObject?

Returns the last page of results

Returns:

  • (Object, nil)

    The last page instance or nil



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

Returns:

  • (Boolean)

    true if last page link exists, false otherwise



121
122
123
# File 'lib/lutaml/hal/page.rb', line 121

def last?
  !links.last.nil?
end

#last_pageObject?

Returns the last page link

Returns:

  • (Object, nil)

    The last page link or nil



149
150
151
# File 'lib/lutaml/hal/page.rb', line 149

def last_page
  links.last
end

#nextObject?

Returns the next page of results, or nil if on the last page

Returns:

  • (Object, nil)

    The next page instance or nil



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

Returns:

  • (Boolean)

    true if next page exists, false otherwise



100
101
102
# File 'lib/lutaml/hal/page.rb', line 100

def next?
  !links.next.nil?
end

#next_pageObject?

Returns the next page link, or nil if on the last page

Returns:

  • (Object, nil)

    The next page link or nil



128
129
130
# File 'lib/lutaml/hal/page.rb', line 128

def next_page
  links.next
end

#prevObject?

Returns the previous page of results, or nil if on the first page

Returns:

  • (Object, nil)

    The previous page instance or nil



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

Returns:

  • (Boolean)

    true if previous page exists, false otherwise



107
108
109
# File 'lib/lutaml/hal/page.rb', line 107

def prev?
  !links.prev.nil?
end

#prev_pageObject?

Returns the previous page link, or nil if on the first page

Returns:

  • (Object, nil)

    The previous page link or nil



135
136
137
# File 'lib/lutaml/hal/page.rb', line 135

def prev_page
  links.prev
end

#total_pagesInteger

Returns the total number of pages

Returns:

  • (Integer)

    The total number of pages



93
94
95
# File 'lib/lutaml/hal/page.rb', line 93

def total_pages
  pages
end