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

ARRAY =

Empty array used to check if an array responds to the given method.

[]

Constants inherited from BasicObject

BasicObject::KEEP_METHODS

Instance Method Summary collapse

Methods inherited from BasicObject

const_missing, remove_methods!

Constructor Details

#initialize(instance, reflection, reload = nil) ⇒ AssociationProxy

Set the association reflection to use, and whether the association should be reloaded if an array method is called.



26
27
28
29
30
# File 'lib/sequel/plugins/association_proxies.rb', line 26

def initialize(instance, reflection, reload=nil)
  @instance = instance
  @reflection = reflection
  @reload = reload
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.



34
35
36
37
# File 'lib/sequel/plugins/association_proxies.rb', line 34

def method_missing(meth, *args, &block)
  (ARRAY.respond_to?(meth) ? @instance.send(:load_associated_objects, @reflection, @reload) : @instance.send(@reflection.dataset_method)).
    send(meth, *args, &block)
end