module Sequel::Plugins::HookClassMethods

  1. lib/sequel/plugins/hook_class_methods.rb

Sequel’s built-in hook_class_methods plugin is designed for backwards compatibility. Its use is not encouraged, it is recommended to use instance methods and super instead of this plugin. This plugin allows calling class methods with blocks to define hooks:

# Block only, can cause duplicate hooks if code is reloaded
before_save{self.created_at = Time.now}
# Block with tag, safe for reloading
before_save(:set_created_at){self.created_at = Time.now}
# Tag only, safe for reloading, calls instance method
before_save(:set_created_at)

Pretty much anything you can do with a hook class method, you can also do with an instance method instead (making sure to call super), which is the recommended way to add hooks in Sequel:

def before_save
  super
  self.created_at = Time.now
end

Usage:

# Allow use of hook class methods in all model subclasses (called before loading subclasses)
Sequel::Model.plugin :hook_class_methods

# Allow the use of hook class methods in the Album class
Album.plugin :hook_class_methods

Methods

Public Class

  1. apply

Public Class methods

apply(model)

Set up the hooks instance variable in the model.

[show source]
   # File lib/sequel/plugins/hook_class_methods.rb
35 def self.apply(model)
36   hooks = model.instance_variable_set(:@hooks, {})
37   Model::HOOKS.each{|h| hooks[h] = []}
38 end