Class: AWS::Core::Resource::AttributeProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, request_types) ⇒ AttributeProvider

Returns a new instance of AttributeProvider.


340
341
342
343
344
345
# File 'lib/aws/core/resource.rb', line 340

def initialize klass, request_types
  @klass = klass
  @id = klass.attribute_providers.length
  @request_types = request_types
  @provides = {}
end

Instance Attribute Details

#request_typesObject (readonly)

Returns the value of attribute request_types


347
348
349
# File 'lib/aws/core/resource.rb', line 347

def request_types
  @request_types
end

Instance Method Details

#attributes_from_response(resource, response) ⇒ Object


379
380
381
382
383
384
385
# File 'lib/aws/core/resource.rb', line 379

def attributes_from_response resource, response
  if response_object = resource.send(finder_method, response)
    attributes_from_response_object(response_object)
  else
    nil
  end
end

#attributes_from_response_object(resp_obj) ⇒ Object


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/aws/core/resource.rb', line 387

def attributes_from_response_object resp_obj
  
  @provides.inject({}) do |attributes,(attr_name,options)|

    attr = @klass.attributes[attr_name]

    method = options[:get_as] || attr.get_as
  
    v = resp_obj.key?(method) ? resp_obj[method] : resp_obj[method.to_s]
    v = v[:value] if v and options[:value_wrapped]
    v = attr.translate_output_value(v)

    attributes.merge(attr_name => v)

  end
  
end

#find(&block) ⇒ Object


349
350
351
# File 'lib/aws/core/resource.rb', line 349

def find &block
  @klass.send(:define_method, finder_method, &block)
end

#finder_methodObject


353
354
355
# File 'lib/aws/core/resource.rb', line 353

def finder_method
  "_find_in_#{request_types.join('_or_')}_response_#{@id}"
end

#provides(*attr_names, options = {}) ⇒ Object

Indicates that all of the the named attributes can be retrieved from an appropriate response object.

Parameters:

  • attr_names (Symbol)

    A list of attributes provided

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :value_wrapped (Boolean) — default: false

    If true, then the value returned by the response object will also receive the message :value before it is translated and returned.

  • :get_as (Symbol)

    Defaults to the method named by the attribute. This is useful when you have two providers for the same attribute but their response object name them differently.


370
371
372
373
374
375
376
377
# File 'lib/aws/core/resource.rb', line 370

def provides *attr_names
  options = attr_names.last.is_a?(Hash) ? attr_names.pop : {}
  attr_names.each do |attr_name|
    attr = @klass.attributes[attr_name]
    attr.request_types.push(*request_types)
    @provides[attr_name] = options
  end
end