Class: Pluggy::Lists::BaseList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pluggy/lists/base_list.rb

Overview

Shared iteration for all three of Pluggy's pagination shapes. Subclasses differ only in more? and next_page.

Direct Known Subclasses

ArrayList, CursorList, OffsetList

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, klass:, requestor:, path:, filters: {}, client: nil) ⇒ BaseList

Returns a new instance of BaseList.



12
13
14
15
16
17
18
19
20
# File 'lib/pluggy/lists/base_list.rb', line 12

def initialize(payload, klass:, requestor:, path:, filters: {}, client: nil)
  @raw = payload
  @klass = klass
  @requestor = requestor
  @path = path
  @filters = filters || {}
  @client = client
  @results = extract(payload).map { |item| build(item) }
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



10
11
12
# File 'lib/pluggy/lists/base_list.rb', line 10

def filters
  @filters
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/pluggy/lists/base_list.rb', line 10

def path
  @path
end

#rawObject (readonly)

Returns the value of attribute raw.



10
11
12
# File 'lib/pluggy/lists/base_list.rb', line 10

def raw
  @raw
end

#resultsObject (readonly)

Returns the value of attribute results.



10
11
12
# File 'lib/pluggy/lists/base_list.rb', line 10

def results
  @results
end

Instance Method Details

#auto_paging_each(&block) ⇒ Object

Walks every page. Returns an Enumerator when called without a block, so .lazy, .first(n) and .to_a all work and only fetch the pages they actually need.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pluggy/lists/base_list.rb', line 34

def auto_paging_each(&block)
  return enum_for(:auto_paging_each) unless block_given?

  page = self
  loop do
    page.each(&block)
    break unless page.more?

    page = page.next_page
    break if page.nil? || page.empty?
  end
  self
end

#auto_paging_to_aObject

Every page, eagerly. Convenient, but unbounded -- prefer auto_paging_each for large histories.



50
# File 'lib/pluggy/lists/base_list.rb', line 50

def auto_paging_to_a = auto_paging_each.to_a

#eachObject



22
# File 'lib/pluggy/lists/base_list.rb', line 22

def each(&) = @results.each(&)

#empty?Boolean

Returns:

  • (Boolean)


23
# File 'lib/pluggy/lists/base_list.rb', line 23

def empty? = @results.empty?

#inspectObject



52
53
54
# File 'lib/pluggy/lists/base_list.rb', line 52

def inspect
  "#<#{self.class.name} results=#{@results.length} more=#{more?}>"
end

#lengthObject Also known as: size, count



24
# File 'lib/pluggy/lists/base_list.rb', line 24

def length = @results.length

#more?Boolean

Returns:

  • (Boolean)


28
# File 'lib/pluggy/lists/base_list.rb', line 28

def more? = false

#next_pageObject



29
# File 'lib/pluggy/lists/base_list.rb', line 29

def next_page = nil