class Aws::Resources::Collection
Public Class Methods
new(batches, options = {})
click to toggle source
@param [Enumerator<Array>] batches @option options [Integer] :limit @option options [Integer] :size @api private
# File lib/aws-sdk-core/resources/collection.rb, line 14 def initialize(batches, options = {}) @batches = batches @limit = options[:limit] @size = options[:size] end
Public Instance Methods
[](index)
click to toggle source
@deprecated @api private
# File lib/aws-sdk-core/resources/collection.rb, line 40 def [](index) if @size @batches[0][index] else raise "unable to index into a lazy loaded collection" end end
batches()
click to toggle source
@deprecated @api private
# File lib/aws-sdk-core/resources/collection.rb, line 30 def batches ::Enumerator.new do |y| batch_enum.each do |batch| y << self.class.new([batch], size: batch.size) end end end
each(&block)
click to toggle source
@return [Enumerator<Band>]
# File lib/aws-sdk-core/resources/collection.rb, line 50 def each(&block) enum = ::Enumerator.new do |y| batch_enum.each do |batch| batch.each do |band| y.yield(band) end end end enum.each(&block) if block enum end
first(count = nil)
click to toggle source
@param [Integer] count @return [Resource, Collection]
# File lib/aws-sdk-core/resources/collection.rb, line 64 def first(count = nil) if count items = limit(count).to_a self.class.new([items], size: items.size) else begin each.next rescue StopIteration nil end end end
limit(limit)
click to toggle source
Returns a new collection that will enumerate a limited number of items.
collection.limit(10).each do |band| # yields at most 10 times end
@return [Collection] @param [Integer] limit
# File lib/aws-sdk-core/resources/collection.rb, line 85 def limit(limit) Collection.new(@batches, limit: limit) end
size()
click to toggle source
@return [Integer,nil]
Returns the size of this collection if known, returns `nil` when an API call is necessary to enumerate items in this collection.
# File lib/aws-sdk-core/resources/collection.rb, line 23 def size @size end
Also aliased as: length
Private Instance Methods
batch_enum()
click to toggle source
# File lib/aws-sdk-core/resources/collection.rb, line 91 def batch_enum case @limit when 0 then [] when nil then non_empty_batches else limited_batches end end
limited_batches()
click to toggle source
# File lib/aws-sdk-core/resources/collection.rb, line 107 def limited_batches ::Enumerator.new do |y| yielded = 0 @batches.each do |batch| batch = batch.take(@limit - yielded) if batch.size > 0 y.yield(batch) yielded += batch.size end break if yielded == @limit end end end
non_empty_batches()
click to toggle source
# File lib/aws-sdk-core/resources/collection.rb, line 99 def non_empty_batches ::Enumerator.new do |y| @batches.each do |batch| y.yield(batch) if batch.size > 0 end end end