Class: Object

Inherits:
BasicObject
Defined in:
lib/trigger.rb

Overview

Allows the declaration of events to be triggered.

Example:

class User < Person
  def save
    ...
  end
end

user = User.new
user.before(:save) { puts "Saving user..." }
user.after(:save) { puts "User saved." }
user.save

The block yields the object itself, so it’s possible to construct callbacks like this:

user.before :save do |u|
  u.encrypt_password!
end

Instance Method Summary collapse

Instance Method Details

#after(sym, &block) ⇒ Object



31
32
33
# File 'lib/trigger.rb', line 31

def after sym, &block
  append_triggerable sym, :after, &block
end

#before(sym, &block) ⇒ Object



27
28
29
# File 'lib/trigger.rb', line 27

def before sym, &block
  append_triggerable sym, :before, &block
end