Giter Club home page Giter Club logo

sequel_simple_callbacks's Introduction

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

sequel_simple_callbacks's People

Contributors

tadman avatar

Stargazers

afa avatar Gabriel Chaney avatar  avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.