Callbacks
This module allows to define callbacks.
Example usage:
require "callbacks"
class MyClass
include Callbacks
def call
with_callbacks do
puts "call"
end
end
before do
puts "before"
end
before do
puts "another before"
end
after do
puts "after"
end
after do
puts "another after"
end
end
MyClass.new.call
# => before
# => another before
# => call
# => after
# => another after
Objects including Callbacks can be inherited, please refer to each method's description for more information.
Instance methods
with_callbacks
SourceMacros
after
Add after callback.
Further after callbacks are called later in the scope of a single object. When defined in children, their after callbacks have lower precedence:
class Foo
include Callbacks
after do
puts "1"
end
after do
puts "2"
end
end
class Bar < Foo
after do
puts "3"
end
end
Bar.new.with_callbacks { puts "call" }
# => call, 1, 2, 3
before
Add before callback.
Further before callbacks are called later in the scope of a single object. When defined in children, their before callbacks have higher precedence:
class Foo
include Callbacks
before do
puts "1"
end
before do
puts "2"
end
end
class Bar < Foo
before do
puts "3"
end
end
Bar.new.with_callbacks { puts "call" }
# => 3, 1, 2, call