class MongoMapper::Plugins::Associations::Proxy

Attributes

association[R]
proxy_association[R]
proxy_owner[R]
target[R]

Public Class Methods

define_proxy_method(method) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 11
def define_proxy_method(method)
  define_method(method) do |*args, &block|
    proxy_method(method, *args, &block)
  end
end
new(owner, association) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 27
def initialize(owner, association)
  @proxy_owner, @association, @loaded = owner, association, false
  Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Public Instance Methods

inspect() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 55
def inspect
  load_target
  "#<#{self.class.inspect}:#{object_id} #{@target.inspect}>"
end
loaded() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 64
def loaded
  @loaded = true
end
loaded?() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 60
def loaded?
  @loaded
end
proxy_method(method, *args, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 99
def proxy_method(method, *args, &block)
  load_target
  target.public_send(method, *args, &block)
end
proxy_respond_to?(*args)
Alias for: respond_to?
read() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 89
def read
  load_target
  @target
end
reload() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 68
def reload
  reset
  load_target
  self unless target.nil?
end
replace(v) click to toggle source

:nocov:

# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 75
def replace(v)
  raise NotImplementedError
end
reset() click to toggle source

:nocov:

# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 80
def reset
  @loaded = false
  @target = nil
end
respond_to?(*args) click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 85
def respond_to?(*args)
  super || (load_target && target.respond_to?(*args))
end
Also aliased as: proxy_respond_to?
write(value) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 94
def write(value)
  replace(value)
  read
end

Protected Instance Methods

find_target() click to toggle source

:nocov:

# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 121
def find_target
  raise NotImplementedError
end
flatten_deeper(array) click to toggle source

:nocov:

# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 126
def flatten_deeper(array)
  array.collect do |element|
    (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
  end.flatten
end
load_target() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 106
def load_target
  if !loaded? || stale_target?
    if @target.is_a?(Array) && @target.any?
      @target = find_target + @target.find_all { |record| !record.persisted? }
    else
      @target = find_target
    end
    loaded
  end
  @target
rescue MongoMapper::DocumentNotFound
  reset
end

Private Instance Methods

define_and_call_proxy_method(method, *args, &block) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 145
def define_and_call_proxy_method(method, *args, &block)
  define_proxy_method(method)
  public_send(method, *args, &block)
end
define_proxy_method(method) click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 138
def define_proxy_method(method)
  metaclass = class << self; self; end
  metaclass.class_eval do
    define_proxy_method(method)
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 150
def method_missing(method, *args, &block)
  # load the target just in case it isn't loaded
  load_target

  # only define the method if the target has the method
  # NOTE: include private methods!
  if target.respond_to?(method, true)
    define_and_call_proxy_method(method, *args, &block)
  else
    super
  end
end
stale_target?() click to toggle source
# File lib/mongo_mapper/plugins/associations/proxy/proxy.rb, line 134
def stale_target?
  false
end