Class: XeroKiwi::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xero_kiwi/page.rb

Overview

A paginated list result from a Xero endpoint. Wraps the items with the pagination metadata Xero returns in the response envelope when ‘page=` is passed. Behaves like an Array for iteration and collection methods (Enumerable + `size` / `empty?` / `to_a`), so most callers can keep working with the value directly.

Callers that need raw Array behaviour (mutation, slicing, ‘JSON.dump`, `is_a?(Array)` checks) should call `.to_a`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items:, page: nil, page_size: nil, item_count: nil, total_count: nil) ⇒ Page

Returns a new instance of Page.



17
18
19
20
21
22
23
# File 'lib/xero_kiwi/page.rb', line 17

def initialize(items:, page: nil, page_size: nil, item_count: nil, total_count: nil)
  @items       = items
  @page        = page
  @page_size   = page_size
  @item_count  = item_count
  @total_count = total_count
end

Instance Attribute Details

#item_countObject (readonly)

Returns the value of attribute item_count.



15
16
17
# File 'lib/xero_kiwi/page.rb', line 15

def item_count
  @item_count
end

#itemsObject (readonly)

Returns the value of attribute items.



15
16
17
# File 'lib/xero_kiwi/page.rb', line 15

def items
  @items
end

#pageObject (readonly)

Returns the value of attribute page.



15
16
17
# File 'lib/xero_kiwi/page.rb', line 15

def page
  @page
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



15
16
17
# File 'lib/xero_kiwi/page.rb', line 15

def page_size
  @page_size
end

#total_countObject (readonly)

Returns the value of attribute total_count.



15
16
17
# File 'lib/xero_kiwi/page.rb', line 15

def total_count
  @total_count
end

Instance Method Details

#each(&block) ⇒ Object



25
26
27
28
29
# File 'lib/xero_kiwi/page.rb', line 25

def each(&block)
  return to_enum(:each) unless block

  @items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/xero_kiwi/page.rb', line 36

def empty?
  @items.empty?
end

#inspectObject



44
45
46
47
# File 'lib/xero_kiwi/page.rb', line 44

def inspect
  "#<#{self.class} items=#{size} page=#{page.inspect} " \
    "page_size=#{page_size.inspect} item_count=#{item_count.inspect}>"
end

#sizeObject Also known as: length



31
32
33
# File 'lib/xero_kiwi/page.rb', line 31

def size
  @items.size
end

#to_aObject



40
41
42
# File 'lib/xero_kiwi/page.rb', line 40

def to_a
  @items.dup
end