Class: ActionMCP::Client::Collection
- Inherits:
-
Object
- Object
- ActionMCP::Client::Collection
- Includes:
- RequestTimeouts, Enumerable
- Defined in:
- lib/action_mcp/client/collection.rb
Overview
Base collection class for MCP client collections
Direct Known Subclasses
Constant Summary
Constants included from RequestTimeouts
RequestTimeouts::DEFAULT_TIMEOUT
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#next_cursor ⇒ Object
readonly
Returns the value of attribute next_cursor.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #all(limit: nil) ⇒ Object
- #all!(timeout: DEFAULT_TIMEOUT) ⇒ Object
- #each(&block) ⇒ Object
-
#each_page(limit: nil) {|page| ... } ⇒ Object
Iterate through all pages of results.
-
#filter {|item| ... } ⇒ Array<Object>
Filter items based on a given block.
-
#has_more_pages? ⇒ Boolean
Check if there are more pages available.
-
#initialize(items, client, silence_sql: true) ⇒ Collection
constructor
A new instance of Collection.
-
#next_page(limit: nil) ⇒ Array<Object>
Fetch the next page of results.
-
#page(cursor: nil, limit: nil) ⇒ Array<Object>
Fetch a single page of results.
-
#size ⇒ Integer
Number of items in the collection.
Methods included from RequestTimeouts
Constructor Details
#initialize(items, client, silence_sql: true) ⇒ Collection
Returns a new instance of Collection.
11 12 13 14 15 16 17 18 |
# File 'lib/action_mcp/client/collection.rb', line 11 def initialize(items, client, silence_sql: true) @collection_data = items || [] @client = client @loaded = !@collection_data.empty? @silence_sql = silence_sql @next_cursor = nil @total = items&.size || 0 end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/action_mcp/client/collection.rb', line 9 def client @client end |
#loaded ⇒ Object (readonly) Also known as: loaded?
Returns the value of attribute loaded.
9 10 11 |
# File 'lib/action_mcp/client/collection.rb', line 9 def loaded @loaded end |
#next_cursor ⇒ Object (readonly)
Returns the value of attribute next_cursor.
9 10 11 |
# File 'lib/action_mcp/client/collection.rb', line 9 def next_cursor @next_cursor end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
9 10 11 |
# File 'lib/action_mcp/client/collection.rb', line 9 def total @total end |
Instance Method Details
#all(limit: nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/action_mcp/client/collection.rb', line 20 def all(limit: nil) if limit # If a limit is provided, use pagination result = [] each_page(limit: limit) { |page| result.concat(page) } result else # Otherwise, maintain the old behavior silence_logs { load_items unless @loaded } @collection_data end end |
#all!(timeout: DEFAULT_TIMEOUT) ⇒ Object
33 34 35 36 |
# File 'lib/action_mcp/client/collection.rb', line 33 def all!(timeout: DEFAULT_TIMEOUT) silence_logs { load_items(force: true, timeout: timeout) } @collection_data end |
#each(&block) ⇒ Object
102 103 104 |
# File 'lib/action_mcp/client/collection.rb', line 102 def each(&block) all.each(&block) end |
#each_page(limit: nil) {|page| ... } ⇒ Object
Iterate through all pages of results
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/action_mcp/client/collection.rb', line 70 def each_page(limit: nil) return unless block_given? current_page = page(limit: limit) yield current_page while has_more_pages? current_page = next_page(limit: limit) yield current_page unless current_page.empty? end end |
#filter {|item| ... } ⇒ Array<Object>
Filter items based on a given block
88 89 90 |
# File 'lib/action_mcp/client/collection.rb', line 88 def filter(&block) all.select(&block) end |
#has_more_pages? ⇒ Boolean
Check if there are more pages available
51 52 53 |
# File 'lib/action_mcp/client/collection.rb', line 51 def has_more_pages? !@next_cursor.nil? end |
#next_page(limit: nil) ⇒ Array<Object>
Fetch the next page of results
59 60 61 62 63 |
# File 'lib/action_mcp/client/collection.rb', line 59 def next_page(limit: nil) return [] unless has_more_pages? page(cursor: @next_cursor, limit: limit) end |
#page(cursor: nil, limit: nil) ⇒ Array<Object>
Fetch a single page of results
43 44 45 46 |
# File 'lib/action_mcp/client/collection.rb', line 43 def page(cursor: nil, limit: nil) silence_logs { load_page(cursor: cursor, limit: limit) } @collection_data end |
#size ⇒ Integer
Number of items in the collection
95 96 97 |
# File 'lib/action_mcp/client/collection.rb', line 95 def size all.size end |