class Orchestrate::Collection::KeyValueList
An enumerator with boundaries for performing a [KeyValue List query](orchestrate.io/docs/api/#key/value/list) against a collection.
Attributes
@return [Collection] The collection which this KeyValueList
enumerates over.
@return [Hash] parameters for setting the boundaries of the enumeration.
Public Class Methods
Instantiate a new KeyValueList
to enumerate over a collection @param collection [Collection] the collection to enumerate over. @param range [Hash] parameters for setting the boundaries of the enumeration. @option range [#to_s] :begin The beginning of the range. @option range [true, false] :begin_inclusive Whether the begin key is inclusive or not. @option range [#to_s] :end The end of the range. @option range [true, false] :end_inclusive Whether the end key is inclusive or not. @return KeyValueList
# File lib/orchestrate/collection.rb, line 257 def initialize(collection, range={}) @collection = collection @range = range range[:limit] ||= 100 end
Public Instance Methods
Sets the exclusive start key for enumeration over the KeyValue
items in the collection. Overwrites any value given to start
. @param start_key [#to_s] The exclusive start of the key range. @return [KeyValueList] A new KeyValueList
with the new range.
# File lib/orchestrate/collection.rb, line 275 def after(start_key) self.class.new(collection, range.merge({begin: start_key, begin_inclusive: false})) end
Sets the exclusive end key for enumeration over the KeyValue
items in the collection. Overwrites any value given to end
. @param end_key [#to_s] The exclusive end of the key range. @return [KeyValueList] A new KeyValueList
with the new range.
# File lib/orchestrate/collection.rb, line 283 def before(end_key) self.class.new(collection, range.merge({end: end_key, end_inclusive: false})) end
Iterates over each KeyValue
item in the collection. Used as the basis for Enumerable methods. Items are provided in lexicographically sorted order by key name. @overload each
@return Enumerator
@overload each(&block)
@yieldparam [Orchestrate::KeyValue] key_value The KeyValue item
@example
keys = collection.after(:foo).take(20).map(&:key) # returns the first 20 keys in the collection that occur after "foo"
# File lib/orchestrate/collection.rb, line 306 def each params = {} if range[:begin] begin_key = range[:begin_inclusive] ? :start : :after params[begin_key] = range[:begin] end if range[:end] end_key = range[:end_inclusive] ? :end : :before params[end_key] = range[:end] end params[:limit] = range[:limit] @response ||= collection.perform(:list, params) return enum_for(:each) unless block_given? raise ResultsNotReady.new if collection.app.inside_parallel? loop do @response.results.each do |doc| yield KeyValue.from_listing(collection, doc, @response) end break unless @response.next_link @response = @response.next_results end @response = nil end
Sets the inclusive end key for enumeration over the KeyValue
items in the collection. Overwrites any value given to before
. @param end_key [#to_s] The inclusive end of the key range. @return [KeyValueList] A new KeyValueList
with the new range.
# File lib/orchestrate/collection.rb, line 291 def end(end_key) self.class.new(collection, range.merge({end: end_key, end_inclusive: true})) end
Creates a Lazy Enumerator for the KeyValue
list. If called inside the app’s ‘#in_parallel` block, will prefetch results.
# File lib/orchestrate/collection.rb, line 332 def lazy return each.lazy if collection.app.inside_parallel? super end
Sets the inclusive start key for enumeration over the KeyValue
items in the collection. Overwrites any value given to after
. @param start_key [#to_s] The inclusive start of the key range. @return [KeyValueList] A new KeyValueList
with the new range.
# File lib/orchestrate/collection.rb, line 267 def start(start_key) self.class.new(collection, range.merge({begin: start_key, begin_inclusive: true})) end
Returns the first n items. Equivalent to Enumerable#take. Sets the ‘limit` parameter on the query to Orchestrate
, so we don’t ask for more than is needed. @param count [Integer] The number of items to limit to. @return [Array]
# File lib/orchestrate/collection.rb, line 341 def take(count) count = 1 if count < 1 range[:limit] = count > 100 ? 100 : count super(count) end