Class: MassiveRecord::ORM::Relations::Proxy
- Inherits:
-
Object
- Object
- MassiveRecord::ORM::Relations::Proxy
- Defined in:
- lib/massive_record/orm/relations/proxy.rb,
lib/massive_record/orm/relations/proxy/references_one.rb,
lib/massive_record/orm/relations/proxy/references_many.rb,
lib/massive_record/orm/relations/proxy/references_one_polymorphic.rb
Overview
Parent class for all proxies sitting between records. It’s responsibility is to transparently load and forward method calls to it’s proxy_target. Iy may also do some small maintenance work like setting foreign key in proxy_owner object etc. That kind of functionality is likely to be implemented in one of it’s more specific sub class proxies.
Direct Known Subclasses
Defined Under Namespace
Classes: ReferencesMany, ReferencesOne, ReferencesOnePolymorphic
Instance Attribute Summary collapse
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#proxy_owner ⇒ Object
Returns the value of attribute proxy_owner.
-
#proxy_target ⇒ Object
Returns the value of attribute proxy_target.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #inspect ⇒ Object
-
#load_proxy_target ⇒ Object
Returns the proxy_target.
- #loaded! ⇒ Object
-
#loaded? ⇒ Boolean
If the proxy is loaded it has a proxy_target.
- #method_missing(method, *args, &block) ⇒ Object
- #reload ⇒ Object
- #replace(proxy_target) ⇒ Object
- #reset ⇒ Object
- #respond_to?(*args) ⇒ Boolean
-
#to_param ⇒ Object
Strange..
Constructor Details
#initialize(options = {}) ⇒ Proxy
Returns a new instance of Proxy.
20 21 22 23 24 25 26 27 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 20 def initialize( = {}) . self. = [:metadata] self.proxy_owner = [:proxy_owner] self.proxy_target = [:proxy_target] reset if proxy_target.nil? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 98 def method_missing(method, *args, &block) return proxy_target.send(method, *args, &block) if load_proxy_target && proxy_target.respond_to?(method) super rescue NoMethodError => e raise e, e..sub(/ for #<.*$/, " via proxy for #{proxy_target}") end |
Instance Attribute Details
#metadata ⇒ Object
Returns the value of attribute metadata.
16 17 18 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 16 def @metadata end |
#proxy_owner ⇒ Object
Returns the value of attribute proxy_owner.
16 17 18 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 16 def proxy_owner @proxy_owner end |
#proxy_target ⇒ Object
Returns the value of attribute proxy_target.
15 16 17 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 15 def proxy_target @proxy_target end |
Instance Method Details
#inspect ⇒ Object
74 75 76 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 74 def inspect load_proxy_target.inspect end |
#load_proxy_target ⇒ Object
Returns the proxy_target. Loads it, if it’s not there. Returns nil if for some reason proxy_target could not be found.
49 50 51 52 53 54 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 49 def load_proxy_target self.proxy_target = find_proxy_target_or_find_with_proc if find_proxy_target? proxy_target rescue RecordNotFound reset end |
#loaded! ⇒ Object
87 88 89 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 87 def loaded! @loaded = true end |
#loaded? ⇒ Boolean
If the proxy is loaded it has a proxy_target
83 84 85 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 83 def loaded? !!@loaded end |
#reload ⇒ Object
56 57 58 59 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 56 def reload reset load_proxy_target end |
#replace(proxy_target) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 65 def replace(proxy_target) if proxy_target.nil? reset else raise_if_type_mismatch(proxy_target) self.proxy_target = proxy_target end end |
#reset ⇒ Object
61 62 63 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 61 def reset @loaded = @proxy_target = nil end |
#respond_to?(*args) ⇒ Boolean
94 95 96 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 94 def respond_to?(*args) super || (load_proxy_target && proxy_target.respond_to?(*args)) end |
#to_param ⇒ Object
Strange.. Without Rails, to_param goes through method_missing,
With Rails it seems like the proxy answered to to_param, which
kinda was not what I wanted.
109 110 111 |
# File 'lib/massive_record/orm/relations/proxy.rb', line 109 def to_param # :nodoc: proxy_target.try :to_param end |