class AWS::Core::Resource

@private

Public Class Methods

new(*args) click to toggle source
# File lib/aws/core/resource.rb, line 27
def initialize *args
  
  super
  
  # cache static attributes passed into options
  
  options = args.last.is_a?(Hash) ? args.last : {}
  options.each_pair do |opt_name,opt_value|
    if 
      self.class.attributes.has_key?(opt_name) and
      self.class.attributes[opt_name].static?
    then
      static_attributes[opt_name] = opt_value
    end
  end
  
end

Protected Class Methods

attribute(name, options = {}) click to toggle source

@private

# File lib/aws/core/resource.rb, line 184
def attribute name, options = {}, &block
  attr = Attribute.new(name, options)
  attr.instance_eval(&block) if block_given?
  define_attribute_getter(attr)
  define_attribute_setter(attr) if attr.mutable?
  attributes[attr.name] = attr
end
attribute_providers() click to toggle source

@private

# File lib/aws/core/resource.rb, line 171
def attribute_providers
  @attribute_providers ||= []
end
attribute_providers_for(request_type) click to toggle source

@private

# File lib/aws/core/resource.rb, line 176
def attribute_providers_for request_type
  attribute_providers.select do |provider|
    provider.request_types.include?(request_type)
  end
end
attributes() click to toggle source

@private

# File lib/aws/core/resource.rb, line 164
def attributes
  @attributes ||= Hash.new do |hash,attr_name|
    raise "uknown attribute #{attr_name}"
  end
end
define_attribute_getter(attribute) click to toggle source

@private

# File lib/aws/core/resource.rb, line 200
def define_attribute_getter attribute
  define_method(attribute.name) do
    
    return static_attributes[attribute.name] if 
      static_attributes.has_key?(attribute.name)
  
    begin
      retrieve_attribute(attribute) { get_resource(attribute) }
    rescue Cacheable::NoData => e
      name = ruby_name.tr("_", " ")
      raise NotFound, "unable to find the #{name}"
    end
  
  end
end
define_attribute_setter(attribute) click to toggle source

@private

# File lib/aws/core/resource.rb, line 218
def define_attribute_setter attribute
  setter = attribute.name.to_s.sub(%r\?/, '') + '='
  define_method(setter) do |value|
    translated_value = attribute.translate_input_value(value)
    update_resource(attribute, translated_value)
    if attribute.static?
      static_attributes[attribute.name] = translated_value
    end
    value
  end
end
mutable_attribute(name, options = {}) click to toggle source

@private

# File lib/aws/core/resource.rb, line 194
def mutable_attribute name, options = {}, &block
  attribute(name, options.merge(:mutable => true), &block)
end
new_from(request_type, resp_obj, *args) click to toggle source

@private

# File lib/aws/core/resource.rb, line 157
def new_from request_type, resp_obj, *args
  resource = new(*args)  
  resource.send(:cache_static_attributes, request_type, resp_obj)
  resource
end
populates_from(*request_types, &block) click to toggle source

@private

# File lib/aws/core/resource.rb, line 232
def populates_from *request_types, &block
  provider = provider(*request_types)
  provider.find(&block)
  provider.provides(*attributes.keys)
  provider
end
provider(*request_types) { |provider| ... } click to toggle source

@private

# File lib/aws/core/resource.rb, line 241
def provider *request_types, &block
  provider = AttributeProvider.new(self, request_types)
  if block_given?
    yield(provider)
  end
  attribute_providers << provider 
  provider
end

Public Instance Methods

==(other) click to toggle source
Alias for: eql?
attributes_from_response(resp) click to toggle source

@private

# File lib/aws/core/resource.rb, line 122
def attributes_from_response resp
  
  attributes = {}
  
  self.class.attribute_providers_for(resp.request_type).each do |provider|
    attributes.merge!(provider.attributes_from_response(self, resp))
  end
  
  # cache static attributes
  attributes.each do |attr_name,value|
    if self.class.attributes[attr_name].static?
      static_attributes[attr_name] = value
    end
  end
  
  attributes.empty? ? nil : attributes
  
end
eql?(other) click to toggle source

@return [Boolean] Returns true if the objects references the same

AWS resource.
# File lib/aws/core/resource.rb, line 63
def eql? other
  other.kind_of?(self.class) and 
    resource_identifiers == other.resource_identifiers
end
Also aliased as: ==
inspect() click to toggle source

@return [String] Returns a simple string representation of this resource.

# File lib/aws/core/resource.rb, line 46
def inspect
  
  identifiers = []
  resource_identifiers.each do |key, value|
    if attr = self.class.attributes.values.find{|a| a.get_as == key }
      identifiers << "#{attr.name}:#{value}"
    else
      identifiers << "#{key}:#{value}"
    end
  end
  
  "<#{self::class} #{identifiers.join(' ')}>"
  
end

Protected Instance Methods

cache_static_attributes(request_type, resp_obj) click to toggle source

@private

# File lib/aws/core/resource.rb, line 143
def cache_static_attributes request_type, resp_obj
  self.class.attribute_providers_for(request_type).each do |provider|
    attributes = provider.attributes_from_response_object(resp_obj)
    attributes.each_pair do |attr_name,value|
      if self.class.attributes[attr_name].static?
        static_attributes[attr_name] = value
      end
    end
  end
end
get_resource(attr_name) click to toggle source

@private

# File lib/aws/core/resource.rb, line 71
def get_resource attr_name
  raise NotImplementedError
end
local_cache_key() click to toggle source

@private

# File lib/aws/core/resource.rb, line 104
def local_cache_key
  resource_identifiers.collect{|name,value| value.to_s }.join(":")
end
resource_identifiers() click to toggle source

Overide this method is subclasses of Resource. This method should return an array of identifying key/value pairs.

# @private
protected
def resource_identifiers
  [[:user_name, name]]
end

@private

# File lib/aws/core/resource.rb, line 92
def resource_identifiers
  raise NotImplementedError
end
resource_options(additional = {}) click to toggle source

@protected

# File lib/aws/core/resource.rb, line 98
def resource_options(additional = {})
  Hash[resource_identifiers].merge(additional)
end
ruby_name() click to toggle source

@private

# File lib/aws/core/resource.rb, line 116
def ruby_name
  @ruby_name ||= Inflection.ruby_name(self.class.name)
end
static_attributes() click to toggle source

@private

# File lib/aws/core/resource.rb, line 110
def static_attributes
  @static_attributes ||= {}
end
update_resource(attr, value) click to toggle source

@private

# File lib/aws/core/resource.rb, line 77
def update_resource attr, value
  raise NotImplementedError
end