Class: Sequel::Plugins::AssociationProxies::AssociationProxy
- Inherits:
- BasicObject
- Defined in:
- lib/sequel/plugins/association_proxies.rb
Overview
A proxy for the association. Calling an array method will load the associated objects and call the method on the associated object array. Calling any other method will call that method on the association’s dataset.
Constant Summary collapse
- DEFAULT_PROXY_TO_DATASET =
Default proc used to determine whether to sent the method to the dataset. If the array would respond to it, sends it to the array instead of the dataset.
proc{|opts| !array.respond_to?(opts[:method])}
Constants inherited from BasicObject
Instance Method Summary collapse
-
#initialize(instance, reflection, proxy_argument, &proxy_block) ⇒ AssociationProxy
constructor
Set the association reflection to use, and whether the association should be reloaded if an array method is called.
-
#method_missing(meth, *args, &block) ⇒ Object
Call the method given on the array of associated objects if the method is an array method, otherwise call the method on the association’s dataset.
Methods inherited from BasicObject
const_missing, remove_methods!
Constructor Details
#initialize(instance, reflection, proxy_argument, &proxy_block) ⇒ AssociationProxy
Set the association reflection to use, and whether the association should be reloaded if an array method is called.
72 73 74 75 76 77 |
# File 'lib/sequel/plugins/association_proxies.rb', line 72 def initialize(instance, reflection, proxy_argument, &proxy_block) @instance = instance @reflection = reflection @proxy_argument = proxy_argument @proxy_block = proxy_block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Call the method given on the array of associated objects if the method is an array method, otherwise call the method on the association’s dataset.
81 82 83 84 85 86 87 88 |
# File 'lib/sequel/plugins/association_proxies.rb', line 81 def method_missing(meth, *args, &block) v = if @instance.model.association_proxy_to_dataset.call(:method=>meth, :arguments=>args, :block=>block, :instance=>@instance, :reflection=>@reflection, :proxy_argument=>@proxy_argument, :proxy_block=>@proxy_block) @instance.send(@reflection.dataset_method) else @instance.send(:load_associated_objects, @reflection, @proxy_argument, &@proxy_block) end v.send(meth, *args, &block) end |