class ActiveResource::Base

Public Instance Methods

load(attributes, remove_root = false) click to toggle source
# File lib/hot_fixes.rb, line 25
def load(attributes, remove_root = false)
  raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @prefix_options, attributes = split_options(attributes)

  if attributes.keys.size == 1
    remove_root = self.class.element_name == attributes.keys.first.to_s
  end

  attributes = Formats.remove_root(attributes) if remove_root

  attributes.each do |key, value|
    @attributes[key.to_s] =
        case value
          when Array
            resource = nil
            value.map do |attrs|
              if attrs.is_a?(Hash)
                resource ||= find_or_create_resource_for_collection(key)
                resource.new(attrs, attrs.has_key?(resource.primary_key.to_s))
              else
                attrs.duplicable? ? attrs.dup : attrs
              end
            end
          when Hash
            resource = find_or_create_resource_for(key)
            resource.new(value, value.has_key?(resource.primary_key.to_s))
          else
            value.duplicable? ? value.dup : value
        end
  end

  self
end