Class: MongoMapper::Associations::Proxy

Inherits:
BasicObject
Defined in:
lib/mongo_mapper/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
11
# File 'lib/mongo_mapper/associations/proxy.rb', line 6

def initialize(owner, association)
  @owner = owner
  @association = association
  @association.options[:extend].each { |ext| class << self; self; end.instance_eval { include ext } }
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



53
54
55
56
57
58
59
60
61
# File 'lib/mongo_mapper/associations/proxy.rb', line 53

def method_missing(method, *args, &block)
  if load_target
    if block.nil?
      @target.send(method, *args)
    else
      @target.send(method, *args)  { |*block_args| block.call(*block_args) }
    end
  end
end

Instance Attribute Details

#associationObject (readonly)

Returns the value of attribute association.



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

def association
  @association
end

#ownerObject (readonly)

Returns the value of attribute owner.



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

def owner
  @owner
end

Instance Method Details

#inspectObject



42
43
44
45
# File 'lib/mongo_mapper/associations/proxy.rb', line 42

def inspect
  load_target
  @target.inspect
end

#nil?Boolean

Returns:



47
48
49
50
# File 'lib/mongo_mapper/associations/proxy.rb', line 47

def nil?
  load_target
  @target.nil?
end

#reload_targetObject



21
22
23
24
25
# File 'lib/mongo_mapper/associations/proxy.rb', line 21

def reload_target
  reset
  load_target
  self
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


38
39
40
# File 'lib/mongo_mapper/associations/proxy.rb', line 38

def replace(v)
  raise NotImplementedError
end

#resetObject



17
18
19
# File 'lib/mongo_mapper/associations/proxy.rb', line 17

def reset
  @target = nil
end

#respond_to?(*methods) ⇒ Boolean

Returns:



13
14
15
# File 'lib/mongo_mapper/associations/proxy.rb', line 13

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

#send(method, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/mongo_mapper/associations/proxy.rb', line 27

def send(method, *args)
  metaclass_instance_methods = class << self; self; end.instance_methods
  
  if metaclass_instance_methods.any? { |m| m.to_s == method.to_s }
    return __send__(method, *args)
  end
  
  load_target
  @target.send(method, *args)
end