Module: AWS::Core::Collection::Limitable

Includes:
AWS::Core::Collection, Enumerable
Included in:
IAM::Collection, SimpleDB::ItemCollection
Defined in:
lib/aws/core/collection/limitable.rb

Overview

AWS::Core::Collection::Limitable is used by collections that may truncate responses but that also accept a upper limit of results to return in a single request.

See AWS::Core::Collection for documentation on the available methods.

Instance Method Summary collapse

Methods included from AWS::Core::Collection

#each, #enum, #first, #in_groups_of, #page

Instance Method Details

#each_batch(options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/aws/core/collection/limitable.rb', line 30

def each_batch options = {}, &block

  each_opts  = options.dup
  limit      = each_opts.delete(:limit) || _limit
  next_token = each_opts.delete(:next_token)
  batch_size = each_opts.delete(:batch_size)

  total = 0  # count of items yeileded across all batches

  begin

    max = nil
    if limit or batch_size
      max = []
      max << (limit - total) if limit
      max << batch_size if batch_size
      max = max.min
    end

    batch = []
    next_token = _each_item(next_token, max, each_opts.dup) do |item|

      total += 1
      batch << item

    end

    yield(batch)

  end until next_token.nil? or (limit and limit == total)

  next_token

end