Class: Aws::Resources::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/aws-sdk-resources/collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(operation, options) ⇒ Collection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Collection.

Parameters:

  • operation (HasManyOperation)


10
11
12
13
# File 'lib/aws-sdk-resources/collection.rb', line 10

def initialize(operation, options)
  @operation = operation
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
78
79
80
81
82
# File 'lib/aws-sdk-resources/collection.rb', line 73

def method_missing(method_name, *args, &block)
  if respond_to?(method_name)
    Batch.validate_batch_args!(args)
    batches.each do |batch|
      batch.send(method_name, *args, &block)
    end
  else
    super
  end
end

Instance Method Details

#batches(&block) ⇒ Enumerator<Batch>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



26
27
28
# File 'lib/aws-sdk-resources/collection.rb', line 26

def batches(&block)
  @operation.batches(@options)
end

#each(&block) ⇒ Enumerator<Resource>

Returns:



16
17
18
19
20
21
22
# File 'lib/aws-sdk-resources/collection.rb', line 16

def each(&block)
  if block_given?
    batches.each { |batch| batch.each(&block) }
  else
    self
  end
end

#first(count = 1) ⇒ Resource, Batch

Returns the first resource from the collection.

resource = collection.first

If you pass a count, then the first ‘count` resources are returned in a single batch. See the resource specific batch documentation for a list of supported batch methods.

resources = collection.first(10)
resources.delete

Returns:



55
56
57
58
59
60
61
# File 'lib/aws-sdk-resources/collection.rb', line 55

def first(count = 1)
  if count == 1
    limit(1).to_a.first
  else
    Batch.new(resource_class, limit(count).to_a)
  end
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
91
92
# File 'lib/aws-sdk-resources/collection.rb', line 85

def inspect
  parts = {}
  parts[:type] = resource_class.name
  parts[:limit] = @options[:limit]
  parts[:params] = @options[:params] || {}
  parts = parts.inject("") {|s,(k,v)| s << " #{k}=#{v.inspect}" }
  ['#<', self.class.name, parts, '>'].join
end

#limit(limit) ⇒ Collection

Specifies the maximum number of items to enumerate.

collection.limit(10).each do |resource|
  # yields at most 10 times
end

Parameters:

  • limit (Integer)

    The maximum number of items to yield via #each or #batches.

Returns:



39
40
41
# File 'lib/aws-sdk-resources/collection.rb', line 39

def limit(limit)
  self.class.new(@operation, @options.merge(limit: limit))
end

#respond_to?(method_name, *args) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


64
65
66
67
68
69
70
# File 'lib/aws-sdk-resources/collection.rb', line 64

def respond_to?(method_name, *args)
  if resource_class.batch_operation_names.include?(method_name.to_sym)
    true
  else
    super
  end
end