Class: ApiResource::Associations::SingleObjectProxy

Inherits:
AssociationProxy show all
Defined in:
lib/api_resource/associations/single_object_proxy.rb

Instance Attribute Summary

Attributes inherited from AssociationProxy

#finder_opts, #klass, #owner, #remote_path

Instance Method Summary collapse

Methods inherited from AssociationProxy

define_association_as_attribute, #expires_in, #includes, #initialize, #load_resource_definition, #loaded?, #reload, #ttl

Constructor Details

This class inherits a constructor from ApiResource::Associations::AssociationProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ApiResource::Associations::AssociationProxy

Instance Method Details

#==(other) ⇒ Object



51
52
53
54
55
# File 'lib/api_resource/associations/single_object_proxy.rb', line 51

def ==(other)
  return false if self.class != other.class
  return false if other.internal_object.attributes != self.internal_object.attributes
  return true
end

#collection?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/api_resource/associations/single_object_proxy.rb', line 12

def collection?
  false
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/api_resource/associations/single_object_proxy.rb', line 61

def eql?(other)
  return self == other
end

#hashObject



57
58
59
# File 'lib/api_resource/associations/single_object_proxy.rb', line 57

def hash
  self.id.hash
end

#internal_objectObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/api_resource/associations/single_object_proxy.rb', line 16

def internal_object
  # we are already loaded - return the internal object
  if self.loaded?
    return @internal_object
  elsif self.remote_path.present?
    @internal_object = self.load
  else
    nil
  end
end

#internal_object=(contents) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/api_resource/associations/single_object_proxy.rb', line 27

def internal_object=(contents)
  if contents.is_a?(self.klass) || contents.nil?
    @loaded = true
    return @internal_object = contents 
  elsif contents.is_a?(self.class)
    @loaded = true
    return @internal_object = contents.internal_object
  # a Hash may be attributes and/or a service_uri
  elsif contents.is_a?(Hash) 
    contents = contents.symbolize_keys
    @remote_path = contents.delete(
      self.class.remote_path_element.to_sym
    )
    if contents.present?
      @loaded = true
      return @internal_object = self.klass.instantiate_record(contents)
    end
  else
    raise ArgumentError.new(
      "#{contents} must be a #{self.klass}, a #{self.class} or a Hash"
    )
  end
end

#serializable_hash(options = {}) ⇒ Object



7
8
9
10
# File 'lib/api_resource/associations/single_object_proxy.rb', line 7

def serializable_hash(options = {})
  return if self.internal_object.nil?
  self.internal_object.serializable_hash(options)
end