Class: StorageRoom::Proxy
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
- METHODS_WITHOUT_RELOAD =
TODO: figure out what other methods might be needed?
[:reload, :response_data, :loaded?, :class, :set_from_response_data, :present?, :is_a?, :respond_to?, :as_json, :to_hash]
Instance Method Summary collapse
- #_object ⇒ Object
-
#initialize(object, parameters = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #inspect ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Forward all method calls to the proxied object, reload if necessary.
- #proxy? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(object, parameters = {}) ⇒ Proxy
Returns a new instance of Proxy.
11 12 13 14 |
# File 'lib/storage_room/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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/storage_room/proxy.rb', line 37 def method_missing(method_name, *args, &block) if @object.loaded? || METHODS_WITHOUT_RELOAD.include?(method_name) # no need to reload else StorageRoom.log("Reloading #{@object['url']} due to #{method_name}") @object.reload(@object['url'], @parameters) end @object.send(method_name, *args, &block) end |
Instance Method Details
#_object ⇒ Object
32 33 34 |
# File 'lib/storage_room/proxy.rb', line 32 def _object @object end |
#inspect ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/storage_room/proxy.rb', line 20 def inspect if @object.loaded? @object.inspect else "#<#{@object.class} (proxied)>" end end |
#proxy? ⇒ Boolean
16 17 18 |
# File 'lib/storage_room/proxy.rb', line 16 def proxy? true end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/storage_room/proxy.rb', line 28 def to_s inspect end |