Class: OldApiResource::Associations::AssociationProxy
- Inherits:
-
Object
- Object
- OldApiResource::Associations::AssociationProxy
- Defined in:
- lib/old_api_resource/associations/association_proxy.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#internal_object ⇒ Object
Returns the value of attribute internal_object.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#loaded ⇒ Object
Returns the value of attribute loaded.
-
#remote_path ⇒ Object
Returns the value of attribute remote_path.
-
#scopes ⇒ Object
Returns the value of attribute scopes.
-
#times_loaded ⇒ Object
Returns the value of attribute times_loaded.
Instance Method Summary collapse
-
#initialize(klass_name, contents) ⇒ AssociationProxy
constructor
A new instance of AssociationProxy.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #reload(scope = nil, opts = {}) ⇒ Object
- #scope?(scp) ⇒ Boolean
- #serializable_hash(options = {}) ⇒ Object
- #to_s ⇒ Object
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_object ⇒ Object
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 |
#klass ⇒ Object
Returns the value of attribute klass.
10 11 12 |
# File 'lib/old_api_resource/associations/association_proxy.rb', line 10 def klass @klass end |
#loaded ⇒ Object
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_path ⇒ Object
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 |
#scopes ⇒ Object
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_loaded ⇒ Object
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
#inspect ⇒ Object
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
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( = {}) raise "Not Implemented: This method must be implemented in a subclass" end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/old_api_resource/associations/association_proxy.rb', line 65 def to_s self.internal_object.to_s end |