Class: LanggraphPlatform::Utils::Paginator
- Inherits:
-
Object
- Object
- LanggraphPlatform::Utils::Paginator
- Includes:
- Enumerable
- Defined in:
- lib/langgraph_platform/utils/paginator.rb
Instance Method Summary collapse
- #all ⇒ Object
- #count ⇒ Object
- #each(&block) ⇒ Object
- #first(count = nil) ⇒ Object
-
#initialize(resource, method, *args, **kwargs) ⇒ Paginator
constructor
A new instance of Paginator.
- #page(page_number, per_page = @limit) ⇒ Object
Constructor Details
#initialize(resource, method, *args, **kwargs) ⇒ Paginator
Returns a new instance of Paginator.
6 7 8 9 10 11 12 13 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 6 def initialize(resource, method, *args, **kwargs) @resource = resource @method = method @args = args @kwargs = kwargs @limit = kwargs[:limit] || 10 @offset = kwargs[:offset] || 0 end |
Instance Method Details
#all ⇒ Object
55 56 57 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 55 def all to_a end |
#count ⇒ Object
50 51 52 53 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 50 def count # TODO: Implement count method nil end |
#each(&block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 15 def each(&block) return enum_for(:each) unless block_given? current_offset = @offset loop do params = @kwargs.merge(limit: @limit, offset: current_offset) results = @resource.send(@method, *@args, **params) break if results.empty? results.each(&block) break if results.length < @limit current_offset += @limit end end |
#first(count = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 34 def first(count = nil) if count take(count) else params = @kwargs.merge(limit: 1, offset: @offset) results = @resource.send(@method, *@args, **params) results.first end end |
#page(page_number, per_page = @limit) ⇒ Object
44 45 46 47 48 |
# File 'lib/langgraph_platform/utils/paginator.rb', line 44 def page(page_number, per_page = @limit) offset = (page_number - 1) * per_page params = @kwargs.merge(limit: per_page, offset: offset) @resource.send(@method, *@args, **params) end |