Class: ApiResource::Associations::AssociationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/api_resource/associations.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.



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/api_resource/associations.rb', line 284

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 = {})
        ApiResource::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



320
321
322
# File 'lib/api_resource/associations.rb', line 320

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.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def internal_object
  @internal_object
end

#klassObject

Returns the value of attribute klass.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def klass
  @klass
end

#loadedObject

Returns the value of attribute loaded.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def loaded
  @loaded
end

#remote_pathObject

Returns the value of attribute remote_path.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def remote_path
  @remote_path
end

#scopesObject

Returns the value of attribute scopes.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def scopes
  @scopes
end

#times_loadedObject

Returns the value of attribute times_loaded.



282
283
284
# File 'lib/api_resource/associations.rb', line 282

def times_loaded
  @times_loaded
end

Instance Method Details

#inspectObject



341
342
343
# File 'lib/api_resource/associations.rb', line 341

def inspect
  self.internal_object.inspect
end

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



324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/api_resource/associations.rb', line 324

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)


312
313
314
# File 'lib/api_resource/associations.rb', line 312

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

#serializable_hash(options = {}) ⇒ Object



304
305
306
# File 'lib/api_resource/associations.rb', line 304

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

#to_sObject



337
338
339
# File 'lib/api_resource/associations.rb', line 337

def to_s
  self.internal_object.to_s
end