module Sequel::Plugins

  1. lib/sequel/model/plugins.rb

Empty namespace that plugins should use to store themselves, so they can be loaded via Model.plugin.

Plugins should be modules with one of the following conditions:

  • A singleton method named apply, which takes a model, additional arguments, and an optional block. This is called the first time the plugin is loaded for this model (unless it was already loaded by an ancestor class), before including/extending any modules, with the arguments and block provided to the call to Model.plugin.

  • A module inside the plugin module named ClassMethods, which will extend the model class.

  • A module inside the plugin module named InstanceMethods, which will be included in the model class.

  • A module inside the plugin module named DatasetMethods, which will extend the model’s dataset.

  • A singleton method named configure, which takes a model, additional arguments, and an optional block. This is called every time the Model.plugin method is called, after including/extending any modules.

Constants

SEQUEL_METHOD_NAME = lambda do |suffix| :"_sequel_#{suffix}_#{method_num_mutex.synchronize{method_num += 1}}" end  

Return a unique method name symbol for the given suffix.

Public Class methods

after_set_dataset(mod, meth)

Add method to mod that overrides set_dataset to call the method afterward.

[show source]
   # File lib/sequel/model/plugins.rb
50 def self.after_set_dataset(mod, meth)
51   mod.send(:define_method, :set_dataset) do |*a|
52     r = super(*a)
53     # Allow calling private class methods as methods this specifies are usually private
54     send(meth)
55     r
56   end
57 end
def_dataset_methods(mod, meths)

In the given module mod, define methods that are call the same method on the dataset. This is designed for plugins to define dataset methods inside ClassMethods that call the implementations in DatasetMethods.

This should not be called with untrusted input or method names that can’t be used literally, since it uses class_eval.

[show source]
   # File lib/sequel/model/plugins.rb
31 def self.def_dataset_methods(mod, meths)
32   Array(meths).each do |meth|
33     mod.class_eval("def #{meth}(*args, &block); dataset.#{meth}(*args, &block) end", __FILE__, __LINE__)
34     # :nocov:
35     mod.send(:ruby2_keywords, meth) if respond_to?(:ruby2_keywords, true)
36     # :nocov:
37   end
38 end
def_sequel_method(model, meth, expected_arity, &block)

Define a private instance method using the block with the provided name and expected arity. If the name is given as a Symbol, it is used directly. If the name is given as a String, a unique name will be generated using that string. The expected_arity should be either 0 (no arguments) or 1 (single argument).

If a block with an arity that does not match the expected arity is used, a deprecation warning will be issued. The method defined should still work, though it will be slower than a method with the expected arity.

Sequel only checks arity for regular blocks, not lambdas. Lambdas were already strict in regards to arity, so there is no need to try to fix arity to keep backwards compatibility for lambdas.

Blocks with required keyword arguments are not supported by this method.

[show source]
    # File lib/sequel/model/plugins.rb
 81 def self.def_sequel_method(model, meth, expected_arity, &block)
 82   if meth.is_a?(String)
 83     meth = SEQUEL_METHOD_NAME.call(meth)
 84   end
 85   call_meth = meth
 86 
 87   unless block.lambda?
 88     required_args, optional_args, rest, keyword = _define_sequel_method_arg_numbers(block)
 89 
 90     if keyword == :required
 91       raise Error, "cannot use block with required keyword arguments when calling define_sequel_method with expected arity #{expected_arity}"
 92     end
 93 
 94     case expected_arity
 95     when 0
 96       unless required_args == 0
 97         # SEQUEL6: remove
 98         Sequel::Deprecation.deprecate("Arity mismatch in block passed to define_sequel_method. Expected Arity 0, but arguments required for #{block.inspect}. Support for this will be removed in Sequel 6.")
 99         b = block
100         block = lambda{instance_exec(&b)} # Fallback
101       end
102     when 1
103       if required_args == 0 && optional_args == 0 && !rest
104         # SEQUEL6: remove
105         Sequel::Deprecation.deprecate("Arity mismatch in block passed to define_sequel_method. Expected Arity 1, but no arguments accepted for #{block.inspect}.  Support for this will be removed in Sequel 6.")
106         temp_method = SEQUEL_METHOD_NAME.call("temp")
107         model.class_eval("def #{temp_method}(_) #{meth =~ /\A\w+\z/ ? "#{meth}_arity" : "send(:\"#{meth}_arity\")"} end", __FILE__, __LINE__)
108         model.send(:alias_method, meth, temp_method)
109         model.send(:undef_method, temp_method)
110         model.send(:private, meth)
111         meth = :"#{meth}_arity"
112       elsif required_args > 1
113         # SEQUEL6: remove
114         Sequel::Deprecation.deprecate("Arity mismatch in block passed to define_sequel_method. Expected Arity 1, but more arguments required for #{block.inspect}.  Support for this will be removed in Sequel 6.")
115         b = block
116         block = lambda{|r| instance_exec(r, &b)} # Fallback
117       end
118     else
119       raise Error, "unexpected arity passed to define_sequel_method: #{expected_arity.inspect}"
120     end
121   end
122 
123   model.send(:define_method, meth, &block)
124   model.send(:private, meth)
125   model.send(:alias_method, meth, meth)
126   call_meth
127 end
inherited_instance_variables(mod, hash)

Add method to mod that overrides inherited_instance_variables to include the values in this hash.

[show source]
   # File lib/sequel/model/plugins.rb
42 def self.inherited_instance_variables(mod, hash)
43   mod.send(:define_method, :inherited_instance_variables) do ||
44     super().merge!(hash)
45   end
46   mod.send(:private, :inherited_instance_variables)
47 end