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. What this plugin allows you to do is, for example:

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

Pretty much anything you can do with a hook class method, you can also do with an instance method instead:

def before_save
  return false if super == false
  self.created_at =

Note that returning false in any before hook block will skip further before hooks and abort the action. So if a before_save hook block returns false, future before_save hook blocks are not called, and the save is aborted.


# 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


Public Class

  1. apply


DEPRECATION_REPLACEMENTS = { :after_commit=>"Use after_save{db.after_commit{}} instead", :after_destroy_commit=>"Use after_destroy{db.after_commit{}} instead", :after_destroy_rollback=>"Use before_destroy{db.after_rollback{}} instead", :after_rollback=>"Use before_save{db.after_rollback{}} instead" }.freeze  

SEQUEL5: Remove

Public Class methods

apply (model)

Set up the hooks instance variable in the model.

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