Method: OldApiResource::Base#load

Defined in:
lib/old_api_resource/base.rb

#load(attributes) ⇒ Object

Raises:

  • (ArgumentError)


401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/old_api_resource/base.rb', line 401

def load(attributes)
  return if attributes.nil?
  raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash)
  @prefix_options, attributes = split_options(attributes)
 
  attributes.symbolize_keys.each do |key, value|
    # If this attribute doesn't exist define it as a protected attribute
    self.class.define_protected_attributes(key) unless self.respond_to?(key)
    self.attributes[key] =
    case value
      when Array
        if self.has_many?(key)
          MultiObjectProxy.new(self.has_many_class_name(key), value)
        elsif self.association?(key)
          raise ArgumentError, "Expected a hash value or nil, got: #{value.inspect}"
        else
          value.dup rescue value
        end
      when Hash
        if self.has_many?(key)
          MultiObjectProxy.new(self.has_many_class_name(key), value)
        elsif self.association?(key)
          SingleObjectProxy.new(self.association_class_name(key), value)
        else
          value.dup rescue value
        end
      when NilClass
        # If it's nil and an association then create a blank object
        if self.has_many?(key)
          return MultiObjectProxy.new(self.has_many_class_name(key), [])
        elsif self.association?(key)
          SingleObjectProxy.new(self.association_class_name(key), value)
        end
      else
        raise ArgumentError, "expected an array or a hash for the association #{key}, got: #{value.inspect}" if self.association?(key)
        value.dup rescue value
    end
  end
  return self
end