class MSS::Core::Resource::AttributeProvider

@api private

Attributes

request_types[R]

Public Class Methods

new(klass, request_types) click to toggle source
# File lib/mss/core/resource.rb, line 340
def initialize klass, request_types
  @klass = klass
  @id = klass.attribute_providers.length
  @request_types = request_types
  @provides = {}
end

Public Instance Methods

attributes_from_response(resource, response) click to toggle source
# File lib/mss/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) click to toggle source
# File lib/mss/core/resource.rb, line 387
def attributes_from_response_object resp_obj

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

    attr = @klass.attributes[attr_name]

    methods = [options[:from] || attr.from].flatten

    v = resp_obj
    methods.each do |method|
      v = v.key?(method) ? v[method] : v[method.to_s]
      break if v.nil?
    end
    v = v[:value] if v and options[:value_wrapped]
    v = attr.translate_output_value(v)

    attributes.merge(attr_name => v)

  end

end
find(&block) click to toggle source
# File lib/mss/core/resource.rb, line 349
def find &block
  @klass.send(:define_method, finder_method, &block)
end
finder_method() click to toggle source
# File lib/mss/core/resource.rb, line 353
def finder_method
  "_find_in_#{request_types.join('_or_')}_response_#{@id}"
end
provides(*attr_names) click to toggle source

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

@overload provides(*attr_names, options = {})

@param [Symbol] attr_names A list of attributes provided
@param [Hash] options
@option options [Boolean] :value_wrapped (false) If true, then
  the value returned by the response object will also receive
  the message :value before it is translated and returned.
@option options [Symbol] :from 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.
# File lib/mss/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