Class: MongoMapper::Associations::Proxy

Inherits:
BasicObject
Defined in:
lib/mongomapper/associations/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, association) ⇒ Proxy

Returns a new instance of Proxy.



6
7
8
9
10
# File 'lib/mongomapper/associations/proxy.rb', line 6

def initialize(owner, association)
  @owner = owner
  @association = association
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (protected)



36
37
38
39
40
41
42
43
44
# File 'lib/mongomapper/associations/proxy.rb', line 36

def method_missing(method, *args)
  if load_target
    if block_given?
      @target.send(method, *args)  { |*block_args| yield(*block_args) }
    else
      @target.send(method, *args)
    end
  end
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



4
5
6
# File 'lib/mongomapper/associations/proxy.rb', line 4

def association
  @association
end

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/mongomapper/associations/proxy.rb', line 4

def owner
  @owner
end

Instance Method Details

#reload_targetObject



20
21
22
23
24
# File 'lib/mongomapper/associations/proxy.rb', line 20

def reload_target
  reset
  load_target
  self
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/mongomapper/associations/proxy.rb', line 31

def replace(v)
  raise NotImplementedError
end

#resetObject



16
17
18
# File 'lib/mongomapper/associations/proxy.rb', line 16

def reset
  @target = nil
end

#respond_to?(*methods) ⇒ Boolean

Returns:



12
13
14
# File 'lib/mongomapper/associations/proxy.rb', line 12

def respond_to?(*methods)
  (load_target && @target.respond_to?(*methods))
end

#send(method, *args) ⇒ Object



26
27
28
29
# File 'lib/mongomapper/associations/proxy.rb', line 26

def send(method, *args)
  load_target
  @target.send(method, *args)
end