Class: MongoMapper::Plugins::Associations::Proxy
- Extended by:
- Forwardable
- Defined in:
- lib/mongo_mapper/plugins/associations/proxy.rb
Direct Known Subclasses
BelongsToPolymorphicProxy, BelongsToProxy, Collection, OneEmbeddedProxy, OneProxy
Instance Attribute Summary collapse
-
#association ⇒ Object
(also: #proxy_association)
readonly
Returns the value of attribute association.
-
#proxy_owner ⇒ Object
readonly
Returns the value of attribute proxy_owner.
-
#target ⇒ Object
(also: #proxy_target)
readonly
Returns the value of attribute target.
Instance Method Summary collapse
-
#as_json(*options) ⇒ Object
see comments to to_json.
- #blank? ⇒ Boolean
-
#initialize(owner, association) ⇒ Proxy
constructor
A new instance of Proxy.
- #inspect ⇒ Object
- #loaded ⇒ Object
- #loaded? ⇒ Boolean
- #nil? ⇒ Boolean
- #present? ⇒ Boolean
- #proxy_respond_to? ⇒ Object
- #reload ⇒ Object
-
#replace(v) ⇒ Object
:nocov:.
-
#reset ⇒ Object
:nocov:.
- #respond_to?(*args) ⇒ Boolean
- #send(method, *args, &block) ⇒ Object
-
#to_json(*options) ⇒ Object
Active support in rails 3 beta 4 can override to_json after this is loaded, at least when run in mongomapper tests.
Constructor Details
#initialize(owner, association) ⇒ Proxy
Returns a new instance of Proxy.
22 23 24 25 26 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 22 def initialize(owner, association) @proxy_owner, @association, @loaded = owner, association, false Array(association.[: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)
104 105 106 107 108 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 104 def method_missing(method, *args, &block) if load_target target.send(method, *args, &block) end end |
Instance Attribute Details
#association ⇒ Object (readonly) Also known as: proxy_association
Returns the value of attribute association.
14 15 16 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14 def association @association end |
#proxy_owner ⇒ Object (readonly)
Returns the value of attribute proxy_owner.
14 15 16 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14 def proxy_owner @proxy_owner end |
#target ⇒ Object (readonly) Also known as: proxy_target
Returns the value of attribute target.
14 15 16 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14 def target @target end |
Instance Method Details
#as_json(*options) ⇒ Object
see comments to to_json
40 41 42 43 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 40 def as_json(*) load_target target.as_json(*) end |
#blank? ⇒ Boolean
63 64 65 66 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 63 def blank? load_target target.blank? end |
#inspect ⇒ Object
45 46 47 48 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 45 def inspect load_target target.inspect end |
#loaded ⇒ Object
54 55 56 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 54 def loaded @loaded = true end |
#loaded? ⇒ Boolean
50 51 52 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 50 def loaded? @loaded end |
#nil? ⇒ Boolean
58 59 60 61 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 58 def nil? load_target target.nil? end |
#present? ⇒ Boolean
68 69 70 71 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 68 def present? load_target target.present? end |
#proxy_respond_to? ⇒ Object
9 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 9 alias :proxy_respond_to? :respond_to? |
#reload ⇒ Object
73 74 75 76 77 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 73 def reload reset load_target self unless target.nil? end |
#replace(v) ⇒ Object
:nocov:
80 81 82 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 80 def replace(v) raise NotImplementedError end |
#reset ⇒ Object
:nocov:
85 86 87 88 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 85 def reset @loaded = false @target = nil end |
#respond_to?(*args) ⇒ Boolean
90 91 92 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 90 def respond_to?(*args) proxy_respond_to?(*args) || (load_target && target.respond_to?(*args)) end |
#send(method, *args, &block) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 94 def send(method, *args, &block) if proxy_respond_to?(method, true) super else load_target target.send(method, *args, &block) end end |
#to_json(*options) ⇒ Object
Active support in rails 3 beta 4 can override to_json after this is loaded, at least when run in mongomapper tests. The implementation was changed in master some time after this, so not sure whether this is still a problem.
In rails 2, this isn’t a problem however it also solves an issue where to_json isn’t forwarded because it supports to_json itself
34 35 36 37 |
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 34 def to_json(*) load_target target.to_json(*) end |