No commit activity in last 3 years
No release in over 3 years
This plugin makes it possible to declare simple before and after callbacks on the class level just like ActiveRecord
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
 Project Readme

sequel_simple_callbacks¶ ↑

This adds ActiveRecord style callback declarations to standard Sequel models.

Sequel::Model with no plugins:

class MyModel < Sequel::Model
  def before_validation
    check_something
    check_something_else
  end

  def before_save
    return unless (some_condition?)

    do_something
  end
end

Sequel::Model with SequelSimpleCallbacks plugin added:

Sequel::Model.plugin(SequelSimpleCallbacks)

class MyModel < Sequel::Model
  before_validation :check_something, :check_something_else

  before_save :do_something, :unless => :some_condition?
end

Installation¶ ↑

You can install the gem manually using:

gem install sequel_simple_callbacks

You also have the option to declare it in your Gemspec file if using bundler:

gem  'sequel_simple_callbacks'

Notes¶ ↑

If any of the callbacks returns false then additional checking will be pre-empted and the callback will return false. This will halt processing of not only the chain, but the entire operation being performed, as is the expected behavior of Sequel::Model.

Each of the callback methods takes zero or more method names to call as part of that callback cycle:

before_save :method_1, :method_2

The execution of these methods can be limited conditionally using the :if or :unless options either independently or in tandem:

before_save :method_1, :if => :working?, :unless => :on_break?

The blocks referenced by :if and :unless should return true or false, but any value that evaluates as false for :if or true for :unless will block execution of these callbacks. Note that this does not halt the callback chain.

These arguments can be combined as demonstrated here:

before_save :method_1, :method_2,
  :if => :method,
  :unless => lambda { other_method },
  :on => :create do
  check_something
end

Any blocks given are evaluated within the context of the model in question, but the model may be explicitly specified as a parameter to the block:

before_save do |model|
  model.check_something
end

It is important to node that using these class-level declarations means that the instance methods with the same name should not be defined:

before_save :do_something

def before_save
  # WARNING: This will block the :do_something method from running,
  # as this method over-rides that behavior. Calling super will not
  # restore this functionality.

  do_some_stuff

  # Execute the default behavior as defined in the class if this mixed
  # approach is strictly required. This is intended as a last-resort.
  self.class.run_callbacks(self, :before_save)
end

Copyright © 2010 Scott Tadman, The Working Group