class ApiResource::Associations::MultiObjectProxy

Public Instance Methods

all() click to toggle source
# File lib/api_resource/associations.rb, line 415
def all
  self.internal_object
end
each(*args, &block) click to toggle source
# File lib/api_resource/associations.rb, line 419
def each(*args, &block)
  self.internal_object.each(*args, &block)
end
internal_object() click to toggle source

force a load when calling this method

# File lib/api_resource/associations.rb, line 428
def internal_object
  @internal_object ||= self.load_scope_with_options(:all, {})
end
serializable_hash(options) click to toggle source
# File lib/api_resource/associations.rb, line 423
def serializable_hash(options)
  self.internal_object.collect{|obj| obj.serializable_hash(options) }
end

Protected Instance Methods

load(contents) click to toggle source
# File lib/api_resource/associations.rb, line 443
      def load(contents)
        # If we have a blank array or it's just nil then we should just return after setting internal_object to a blank array
        @internal_object = [] and return nil if (contents.is_a?(Array) && contents.blank?) || contents.nil?
        if contents.is_a?(Array) && contents.first.is_a?(Hash) && contents.first[self.class.remote_path_element]
          settings = contents.slice!(0).with_indifferent_access
        end

        settings = contents if contents.is_a?(Hash)
        settings ||= {}.with_indifferent_access

        raise "Invalid response for multi object relationship: #{contents}" unless settings[self.class.remote_path_element] || contents.is_a?(Array)
        self.remote_path = settings.delete(self.class.remote_path_element)

        settings.each do |key, value|
          raise "Expected the scope #{key} to point to a hash, to #{value}" unless value.is_a?(Hash)
          self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
            def #{key}(opts = {})
              ApiResource::Associations::RelationScope.new(self, :#{key}, opts)
            end
          EOE
          self.scopes[key.to_s] = value
        end

        # Create the internal object
        @internal_object = contents.is_a?(Array) ? contents.collect{|item| self.klass.new(item)} : nil
      end
load_scope_with_options(scope, options) click to toggle source
# File lib/api_resource/associations.rb, line 433
def load_scope_with_options(scope, options)
  scope = self.loaded_hash_key(scope.to_s, options)
  return [] if self.remote_path.blank?
  unless self.loaded[scope]
    self.times_loaded += 1
    self.loaded[scope] = self.klass.connection.get("#{self.remote_path}.#{self.klass.format.extension}?#{options.to_query}")
  end
  self.loaded[scope].collect{|item| self.klass.new(item)}
end