class AWS::Core::Resource
@api private
def attribute name, options = {}, &block
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
def attribute_providers
- Api: - private
def attribute_providers @attribute_providers ||= [] end
def attribute_providers_for request_type
- Api: - private
def attribute_providers_for request_type attribute_providers.select do |provider| provider.request_types.include?(request_type) end end
def attributes
- Api: - private
def attributes @attributes ||= Hash.new do |hash,attr_name| raise "uknown attribute #{attr_name}" end end
def attributes_from_response resp
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
def cache_static_attributes request_type, resp_obj
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
def define_attribute_getter attribute
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
def define_attribute_setter attribute
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
def define_attribute_type type_name
- Api: - private
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
def eql? other
-
(Boolean)
- Returns true if the objects references the same
def eql? other other.kind_of?(self.class) and other.resource_identifiers == resource_identifiers end
def get_resource attr_name
def get_resource attr_name raise NotImplementedError end
def initialize *args
- Api: - private
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
def inspect
-
(String)
- Returns a simple string representation of this resource.
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
def local_cache_key
def local_cache_key resource_identifiers.collect{|name,value| value.to_s }.join(":") end
def mutable_attribute name, options = {}, &block
def mutable_attribute name, options = {}, &block attribute(name, options.merge(:mutable => true), &block) end
def new_from request_type, resp_obj, *args
- Api: - private
def new_from request_type, resp_obj, *args resource = new(*args) resource.send(:cache_static_attributes, request_type, resp_obj) resource end
def populates_from *request_types, &block
def populates_from *request_types, &block provider = provider(*request_types) provider.find(&block) provider.provides(*attributes.keys) provider end
def provider *request_types, &block
def provider *request_types, &block provider = AttributeProvider.new(self, request_types) if block_given? yield(provider) end attribute_providers << provider provider end
def resource_identifiers
def resource_identifiers raise NotImplementedError end
def resource_options(additional = {})
def resource_options(additional = {}) Hash[resource_identifiers].merge(additional) end
def ruby_name
def ruby_name @ruby_name ||= Inflection.ruby_name(self.class.name) end
def static_attributes
def static_attributes @static_attributes ||= {} end
def update_resource attr, value
def update_resource attr, value raise NotImplementedError end