Class: ApiResource::Associations::MultiObjectProxy

Inherits:
AssociationProxy show all
Includes:
Enumerable
Defined in:
lib/api_resource/associations/multi_object_proxy.rb

Direct Known Subclasses

HasManyRemoteObjectProxy

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



66
67
68
69
70
71
72
73
74
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 66

def ==(other)
  return false if self.class != other.class
  if self.internal_object.is_a?(Array)
    self.internal_object.sort.each_with_index do |elem, i|
      return false if other.internal_object.sort[i].attributes != elem.attributes
    end
  end
  return true
end

#allObject



9
10
11
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 9

def all
  self.internal_object
end

#collection?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 13

def collection?
  true
end

#each(*args, &block) ⇒ Object



17
18
19
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 17

def each(*args, &block)
  self.internal_object.each(*args, &block)
end

#internal_objectObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 21

def internal_object
  
  # if we don't have a remote path or any data so we set it to a
  # blank array
  if self.remote_path.blank? && @internal_object.blank?
    return @internal_object ||= [] 
  end

  # if we aren't loaded and we don't have data added load here
  if !self.loaded? && @internal_object.blank?
    @internal_object = self.load
  end
  @internal_object
end

#internal_object=(contents) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 36

def internal_object=(contents)
  # if we were passed in a service uri, stop here
  # but if we have a service uri already then don't overwrite
  unless self.remote_path.present?
    return true if self.set_remote_path(contents)
  end

  if contents.try(:first).is_a?(self.klass)
    @loaded = true
    return @internal_object = contents
  elsif contents.instance_of?(self.class)
    @loaded = true
    return @internal_object = contents.internal_object
  elsif contents.is_a?(Array)
    @loaded = true
    return @internal_object = self.klass.instantiate_collection(
      contents
    )
  # we have only provided the resource definition - that's the same
  # as a blank array in this case
  elsif contents.nil?
    return @internal_object = []
  else
    raise ArgumentError.new(
      "#{contents} must be a #{self.klass}, #{self.class}, " + 
      "Array or nil"
    )
  end
end

#serializable_hash(options) ⇒ Object



76
77
78
# File 'lib/api_resource/associations/multi_object_proxy.rb', line 76

def serializable_hash(options)
  self.internal_object.collect{|obj| obj.serializable_hash(options) }
end