class OldApiResource::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/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

Public Instance Methods

inspect() click to toggle source
# File lib/old_api_resource/associations/association_proxy.rb, line 69
def inspect
  self.internal_object.inspect
end
method_missing(method, *args, &block) click to toggle source
# 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
reload(scope = nil, opts = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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_s() click to toggle source
# File lib/old_api_resource/associations/association_proxy.rb, line 65
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/old_api_resource/associations/association_proxy.rb, line 79
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/old_api_resource/associations/association_proxy.rb, line 75
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/old_api_resource/associations/association_proxy.rb, line 85
def loaded_hash_key(scope, options)
  options.to_a.sort.inject(scope) {|accum,(k,v)| accum << "_#{k}_#{v}"}
end