Class: OldApiResource::Associations::AssociationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/old_api_resource/associations/association_proxy.rb

Direct Known Subclasses

MultiObjectProxy, SingleObjectProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass_name, contents) ⇒ AssociationProxy

Returns a new instance of AssociationProxy.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/old_api_resource/associations/association_proxy.rb', line 12

def initialize(klass_name, contents)
  raise "Cannot create an association proxy to the unknown object #{klass_name}" unless defined?(klass_name.to_s.classify)
  # A simple attr_accessor for testing purposes
  self.times_loaded = 0
  self.klass = klass_name.to_s.classify.constantize
  self.load(contents)
  self.loaded = {}.with_indifferent_access
  if self.class.include_class_scopes
    self.scopes = self.scopes.reverse_merge(self.klass.scopes)
  end
  # Now that we have set up all the scopes with the load method we need to create methods
  self.scopes.each do |key, _|
    self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
      def #{key}(opts = {})
        OldApiResource::Associations::RelationScope.new(self, :#{key}, opts)
      end
    EOE
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



48
49
50
# File 'lib/old_api_resource/associations/association_proxy.rb', line 48

def method_missing(method, *args, &block)
  self.internal_object.send(method, *args, &block)
end

Instance Attribute Details

#internal_objectObject

Returns the value of attribute internal_object.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def internal_object
  @internal_object
end

#klassObject

Returns the value of attribute klass.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def klass
  @klass
end

#loadedObject

Returns the value of attribute loaded.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def loaded
  @loaded
end

#remote_pathObject

Returns the value of attribute remote_path.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def remote_path
  @remote_path
end

#scopesObject

Returns the value of attribute scopes.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def scopes
  @scopes
end

#times_loadedObject

Returns the value of attribute times_loaded.



10
11
12
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10

def times_loaded
  @times_loaded
end

Instance Method Details

#inspectObject



69
70
71
# File 'lib/old_api_resource/associations/association_proxy.rb', line 69

def inspect
  self.internal_object.inspect
end

#reload(scope = nil, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/old_api_resource/associations/association_proxy.rb', line 52

def reload(scope =  nil, opts = {})
  if scope.nil?
    self.loaded.clear
    self.times_loaded = 0
    # Remove the loaded object to force it to reload
    remove_instance_variable(:@internal_object)
  else
    # Delete this key from the loaded hash which will cause it to be reloaded
    self.loaded.delete(self.loaded_hash_key(scope, opts))
  end
  self
end

#scope?(scp) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/old_api_resource/associations/association_proxy.rb', line 40

def scope?(scp)
  self.scopes.keys.include?(scp.to_s)
end

#serializable_hash(options = {}) ⇒ Object



32
33
34
# File 'lib/old_api_resource/associations/association_proxy.rb', line 32

def serializable_hash(options = {})
  raise "Not Implemented: This method must be implemented in a subclass"
end

#to_sObject



65
66
67
# File 'lib/old_api_resource/associations/association_proxy.rb', line 65

def to_s
  self.internal_object.to_s
end