class AWS::Core::Resource

@private

Public Class Methods

new(*args) click to toggle source

@private

Calls superclass method AWS::Core::Model::new
# File lib/aws/core/resource.rb, line 28
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 211
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?
  alias_method(options[:alias], name) if options[:alias]
  attributes[attr.name] = attr
end
attribute_providers() click to toggle source

@private

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

@private

# File lib/aws/core/resource.rb, line 203
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 191
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 228
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 246
def define_attribute_setter attribute
  setter = attribute.name.to_s.sub(/\?/, '') + '='
  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
define_attribute_type(type_name) click to toggle source

@private

# File lib/aws/core/resource.rb, line 168
        def define_attribute_type type_name
          class_eval <<-METHODS

            def self.#{type_name}_attributes
              @#{type_name}_attributes ||= {}
            end

            def self.#{type_name}_attribute name, options = {}, &block
              attr = attribute(name, options, &block)
              #{type_name}_attributes[attr.name] = attr
            end

          METHODS
        end
mutable_attribute(name, options = {}) click to toggle source

@private

# File lib/aws/core/resource.rb, line 222
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 184
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 260
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 269
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)
Alias for: eql?
attributes_from_response(resp) click to toggle source

@private

# File lib/aws/core/resource.rb, line 123
def attributes_from_response resp

  # check each provider for this request type to see if it
  # can find the resource and some of its attributes
  attributes = []
  self.class.attribute_providers_for(resp.request_type).each do |provider|
    attributes << provider.attributes_from_response(self, resp)
  end

  # drop out those that returned no attributesj
  attributes.compact!

  # stop here if nothing was found for this resource
  return nil if attributes.empty?

  # merge the attributes together into a single hash
  attributes = attributes.inject({}) {|hash,attribs| hash.merge(attribs) }

  # cache static attributes
  attributes.each_pair do |attr_name,value|
    if self.class.attributes[attr_name].static?
      static_attributes[attr_name] = value
    end
  end

  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 64
def eql? other
  other.kind_of?(self.class) and
  other.resource_identifiers == 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 47
def inspect

  identifiers = []
  resource_identifiers.each do |key, value|
    if attr = self.class.attributes.values.find{|a| a.from == 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 154
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 72
def get_resource attr_name
  raise NotImplementedError
end
local_cache_key() click to toggle source

@private

# File lib/aws/core/resource.rb, line 105
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 93
def resource_identifiers
  raise NotImplementedError
end
resource_options(additional = {}) click to toggle source

@protected

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

@private

# File lib/aws/core/resource.rb, line 117
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 111
def static_attributes
  @static_attributes ||= {}
end
update_resource(attr, value) click to toggle source

@private

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