Class: MongoMapper::Plugins::Associations::Proxy
- Inherits:
-
Object
- Object
- MongoMapper::Plugins::Associations::Proxy
show all
- Extended by:
- Forwardable
- Defined in:
- lib/mongo_mapper/plugins/associations/proxy/proxy.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(owner, association) ⇒ Proxy
Returns a new instance of Proxy.
27
28
29
30
31
|
# 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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 150
def method_missing(method, *args, &block)
load_target
if target.respond_to?(method, true)
define_and_call_proxy_method(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
#association ⇒ Object
Also known as:
proxy_association
Returns the value of attribute association.
18
19
20
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18
def association
@association
end
|
#proxy_owner ⇒ Object
Returns the value of attribute proxy_owner.
18
19
20
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18
def proxy_owner
@proxy_owner
end
|
Returns the value of attribute target.
18
19
20
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18
def target
@target
end
|
Class Method Details
.define_proxy_method(method) ⇒ Object
11
12
13
14
15
|
# 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
|
Instance Method Details
55
56
57
58
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 55
def inspect
load_target
"#<#{self.class.inspect}:#{object_id} #{@target.inspect}>"
end
|
64
65
66
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 64
def loaded
@loaded = true
end
|
60
61
62
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 60
def loaded?
@loaded
end
|
#proxy_method(method, *args, &block) ⇒ Object
99
100
101
102
|
# 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? ⇒ Object
20
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 20
alias_method :proxy_respond_to?, :respond_to?
|
89
90
91
92
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 89
def read
load_target
@target
end
|
68
69
70
71
72
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 68
def reload
reset
load_target
self unless target.nil?
end
|
#replace(v) ⇒ Object
75
76
77
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 75
def replace(v)
raise NotImplementedError
end
|
80
81
82
83
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 80
def reset
@loaded = false
@target = nil
end
|
#respond_to?(*args) ⇒ Boolean
85
86
87
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 85
def respond_to?(*args)
super || (load_target && target.respond_to?(*args))
end
|
#write(value) ⇒ Object
94
95
96
97
|
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 94
def write(value)
replace(value)
read
end
|