Class: Orchestrate::Collection::KeyValueList
- Inherits:
-
Object
- Object
- Orchestrate::Collection::KeyValueList
- Includes:
- Enumerable
- Defined in:
- lib/orchestrate/collection.rb
Overview
An enumerator with boundaries for performing a KeyValue List query against a collection.
Instance Attribute Summary collapse
-
#collection ⇒ Collection
readonly
The collection which this KeyValueList enumerates over.
-
#range ⇒ Hash
readonly
Parameters for setting the boundaries of the enumeration.
Instance Method Summary collapse
-
#after(start_key) ⇒ KeyValueList
Sets the exclusive start key for enumeration over the KeyValue items in the collection.
-
#before(end_key) ⇒ KeyValueList
Sets the exclusive end key for enumeration over the KeyValue items in the collection.
-
#each ⇒ Object
Iterates over each KeyValue item in the collection.
-
#end(end_key) ⇒ KeyValueList
Sets the inclusive end key for enumeration over the KeyValue items in the collection.
-
#initialize(collection, range = {}) ⇒ Object
constructor
Instantiate a new KeyValueList to enumerate over a collection.
-
#lazy ⇒ Object
Creates a Lazy Enumerator for the KeyValue list.
-
#start(start_key) ⇒ KeyValueList
Sets the inclusive start key for enumeration over the KeyValue items in the collection.
-
#take(count) ⇒ Array
Returns the first n items.
Constructor Details
#initialize(collection, range = {}) ⇒ Object
Instantiate a new KeyValueList to enumerate over a collection
257 258 259 260 261 |
# File 'lib/orchestrate/collection.rb', line 257 def initialize(collection, range={}) @collection = collection @range = range range[:limit] ||= 100 end |
Instance Attribute Details
#collection ⇒ Collection (readonly)
Returns The collection which this KeyValueList enumerates over.
244 245 246 |
# File 'lib/orchestrate/collection.rb', line 244 def collection @collection end |
#range ⇒ Hash (readonly)
Returns parameters for setting the boundaries of the enumeration.
247 248 249 |
# File 'lib/orchestrate/collection.rb', line 247 def range @range end |
Instance Method Details
#after(start_key) ⇒ KeyValueList
Sets the exclusive start key for enumeration over the KeyValue items in the collection. Overwrites any value given to #start.
275 276 277 |
# File 'lib/orchestrate/collection.rb', line 275 def after(start_key) self.class.new(collection, range.merge({begin: start_key, begin_inclusive: false})) end |
#before(end_key) ⇒ KeyValueList
Sets the exclusive end key for enumeration over the KeyValue items in the collection. Overwrites any value given to #end.
283 284 285 |
# File 'lib/orchestrate/collection.rb', line 283 def before(end_key) self.class.new(collection, range.merge({end: end_key, end_inclusive: false})) end |
#each ⇒ Object #each {|key_value| ... } ⇒ Object
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.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# 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 |
#end(end_key) ⇒ KeyValueList
Sets the inclusive end key for enumeration over the KeyValue items in the collection. Overwrites any value given to #before.
291 292 293 |
# File 'lib/orchestrate/collection.rb', line 291 def end(end_key) self.class.new(collection, range.merge({end: end_key, end_inclusive: true})) end |
#lazy ⇒ Object
Creates a Lazy Enumerator for the KeyValue list. If called inside the
app's #in_parallel
block, will prefetch results.
332 333 334 335 |
# File 'lib/orchestrate/collection.rb', line 332 def lazy return each.lazy if collection.app.inside_parallel? super end |
#start(start_key) ⇒ KeyValueList
Sets the inclusive start key for enumeration over the KeyValue items in the collection. Overwrites any value given to #after.
267 268 269 |
# File 'lib/orchestrate/collection.rb', line 267 def start(start_key) self.class.new(collection, range.merge({begin: start_key, begin_inclusive: true})) end |
#take(count) ⇒ Array
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.
341 342 343 344 345 |
# File 'lib/orchestrate/collection.rb', line 341 def take(count) count = 1 if count < 1 range[:limit] = count > 100 ? 100 : count super(count) end |