Class: MongoMapper::Associations::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/associations/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Proxy

Returns a new instance of Proxy.



19
20
21
22
23
# File 'lib/mongo_mapper/associations/proxy.rb', line 19

def initialize(owner, reflection)
  @owner, @reflection, @loaded = owner, reflection, false
  Array(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



87
88
89
90
91
92
93
94
95
# File 'lib/mongo_mapper/associations/proxy.rb', line 87

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

Instance Attribute Details

#ownerObject (readonly) Also known as: proxy_owner

Returns the value of attribute owner.



9
10
11
# File 'lib/mongo_mapper/associations/proxy.rb', line 9

def owner
  @owner
end

#reflectionObject (readonly) Also known as: proxy_reflection

Returns the value of attribute reflection.



9
10
11
# File 'lib/mongo_mapper/associations/proxy.rb', line 9

def reflection
  @reflection
end

#targetObject (readonly) Also known as: proxy_target

Returns the value of attribute target.



9
10
11
# File 'lib/mongo_mapper/associations/proxy.rb', line 9

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



81
82
83
84
# File 'lib/mongo_mapper/associations/proxy.rb', line 81

def ===(other)
  load_target
  other === target
end

#blank?Boolean

Returns:



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

def blank?
  load_target
  target.blank?
end

#inspectObject



25
26
27
28
# File 'lib/mongo_mapper/associations/proxy.rb', line 25

def inspect
  load_target
  target.inspect
end

#loadedObject



34
35
36
# File 'lib/mongo_mapper/associations/proxy.rb', line 34

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



30
31
32
# File 'lib/mongo_mapper/associations/proxy.rb', line 30

def loaded?
  @loaded
end

#nil?Boolean

Returns:



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

def nil?
  load_target
  target.nil?
end

#present?Boolean

Returns:



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

def present?
  load_target
  target.present?
end

#proxy_respond_to?Object



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

alias :proxy_respond_to? :respond_to?

#reloadObject



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

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/mongo_mapper/associations/proxy.rb', line 59

def replace(v)
  raise NotImplementedError
end

#resetObject



63
64
65
66
# File 'lib/mongo_mapper/associations/proxy.rb', line 63

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



68
69
70
# File 'lib/mongo_mapper/associations/proxy.rb', line 68

def respond_to?(*args)
  proxy_respond_to?(*args) || (load_target && target.respond_to?(*args))
end

#send(method, *args) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/mongo_mapper/associations/proxy.rb', line 72

def send(method, *args)
  if proxy_respond_to?(method)
    super
  else
    load_target
    target.send(method, *args)
  end
end