Class: Tagcrumbs::Proxy
- Inherits:
-
Object
- Object
- Tagcrumbs::Proxy
- Defined in:
- lib/tagcrumbs/proxy.rb
Overview
The Proxy class is used in resource associations to delay loading of a resource for as long as possible. Some method calls are directly forwarded to the resource, for others the resource is loaded first from the webservice and afterwards the call is made. The Proxy tries to be as transparent as possible so that you basically never know that you have a proxy object instead of a real resource.
Constant Summary collapse
- UNPROXIED_METHODS =
TODO: figure out what other methods might be needed?
[:reload, :resource_url, :properties, :loaded?, :class]
Instance Method Summary collapse
-
#initialize(object, parameters = {}) ⇒ Proxy
constructor
A new instance of Proxy.
-
#method_missing(method_name, *args, &block) ⇒ Object
Forward all method calls to the proxied object, reload if necessary.
Constructor Details
#initialize(object, parameters = {}) ⇒ Proxy
Returns a new instance of Proxy.
11 12 13 14 |
# File 'lib/tagcrumbs/proxy.rb', line 11 def initialize(object, parameters={}) @object = object @parameters = parameters end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Forward all method calls to the proxied object, reload if necessary
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tagcrumbs/proxy.rb', line 18 def method_missing(method_name, *args, &block) #puts "** Proxying #{method_name}. Loaded: #{@object.loaded?}" if @object.loaded? || UNPROXIED_METHODS.include?(method_name) #|| [email protected]_to?(method_name) # having this doesn't allow first etc on collections @object.send(method_name, *args, &block) elsif method_name == :total_entries && @object.respond_to?(method_name) && @object.total_entries.present? # let total_entries go through if present @object.total_entries else #puts "** Reloading" @object.reload(nil, @parameters) if @object.resource_url.present? @object.send(method_name, *args, &block) end end |