class Aws::Resources::Builder

A {Builder} construct resource objects. It extracts resource identifiers for the objects it builds from another resource object and/or an AWS response.

Attributes

resource_class[R]

@return [Class<Resource>]

sources[R]

@return [Array<BuilderSources::Source>] A list of resource

identifier sources.

Public Class Methods

new(options = {}) click to toggle source

@option options [required, Class<Resource>] #resource_class @option options [Array<BuilderSources::Source>] :sources ([])

# File lib/aws-sdk-resources/builder.rb, line 13
def initialize(options = {})
  @resource_class = options[:resource_class]
  @sources = options[:sources] || []
end

Public Instance Methods

build(options = {}) click to toggle source

@option [Resource] :resource @option [Seahorse::Client::Response] :response @return [Resource, Array<Resource>] Returns a resource object or an

array of resource objects if {#plural?}.
# File lib/aws-sdk-resources/builder.rb, line 35
def build(options = {})
  identifier_map = @sources.each.with_object({}) do |source, hash|
    hash[source.target] = source.extract(options)
  end
  if plural?
    build_batch(identifier_map, options)
  else
    build_one(identifier_map, options)
  end
end
plural?() click to toggle source

@return [Boolean] Returns `true` if this builder returns an array

of resource objects from {#build}.
# File lib/aws-sdk-resources/builder.rb, line 27
def plural?
  @sources.any?(&:plural?)
end

Private Instance Methods

build_batch(identifier_map, options) { |resource| ... } click to toggle source
# File lib/aws-sdk-resources/builder.rb, line 48
def build_batch(identifier_map, options, &block)
  resources = (0...resource_count(identifier_map)).collect do |n|
    identifiers = @sources.inject({}) do |hash, source|
      value = identifier_map[source.target]
      value = value[n] if source.plural?
      hash[source.target] = value
      hash
    end
    resource = build_one(identifiers, options)
    yield(resource) if block_given?
    resource
  end
  Batch.new(resource_class, resources, options)
end
build_one(identifiers, options) click to toggle source
# File lib/aws-sdk-resources/builder.rb, line 63
def build_one(identifiers, options)
  if identifiers.count > 0 && identifiers.values.any?(&:nil?)
    nil
  else
    @resource_class.new(identifiers.merge(
      client: client(options)
    ))
  end
end
client(options) click to toggle source
# File lib/aws-sdk-resources/builder.rb, line 79
def client(options)
  Array(options[:resource]).first.client
end
resource_count(identifier_map) click to toggle source
# File lib/aws-sdk-resources/builder.rb, line 73
def resource_count(identifier_map)
  identifier_map.values.inject(0) do |max, values|
    [max, values.is_a?(Array) ? values.size : 0].max
  end
end