class ApiResource::Associations::AssociationProxy
Attributes
internal_object[RW]
klass[RW]
loaded[RW]
remote_path[RW]
scopes[RW]
times_loaded[RW]
Public Class Methods
new(klass_name, contents)
click to toggle source
# 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
Public Instance Methods
inspect()
click to toggle source
# File lib/api_resource/associations.rb, line 341 def inspect self.internal_object.inspect end
method_missing(method, *args, &block)
click to toggle source
# File lib/api_resource/associations.rb, line 320 def method_missing(method, *args, &block) self.internal_object.send(method, *args, &block) end
reload(scope = nil, opts = {})
click to toggle source
# 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)
click to toggle source
# File lib/api_resource/associations.rb, line 312 def scope?(scp) self.scopes.keys.include?(scp.to_s) end
serializable_hash(options = {})
click to toggle source
# 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_s()
click to toggle source
# File lib/api_resource/associations.rb, line 337 def to_s self.internal_object.to_s end
Protected Instance Methods
load(contents)
click to toggle source
This method is a helper to initialize for loading the data passed in to create this object
# File lib/api_resource/associations.rb, line 351 def load(contents) raise "Not Implemented: This method must be implemented in a subclass" end
load_scope_with_options(scope, options)
click to toggle source
This method loads a particular scope with a set of options from the remote server
# File lib/api_resource/associations.rb, line 347 def load_scope_with_options(scope, options) raise "Not Implemented: This method must be implemented in a subclass" end
loaded_hash_key(scope, options)
click to toggle source
This method create the key for the loaded hash, it ensures that a unique set of scopes with a unique set of options is only loaded once
# File lib/api_resource/associations.rb, line 357 def loaded_hash_key(scope, options) options.to_a.sort.inject(scope) {|accum,(k,v)| accum << "_#{k}_#{v}"} end