Callbacks
An expressive callbacks module.
Installation
Add this to your application's shard.yml
:
dependencies:
callbacks:
github: vladfaust/callbacks
version: ~> 0.1.0
This shard follows Semantic Versioning 2.0.0, so see releases and change the version
accordingly.
Usage
require "callbacks"
class Foo
include Callbacks
def call
with_callbacks { puts "call" }
end
before do
puts "1"; true # Must return truthy value for the callchain to proceed
end
before do
puts "2"; true
end
around do
puts "3"
yield
puts "4"
end
after do
puts "5"
end
after do
puts "6" # Will not be called because previous after callback returned falsey value
end
end
Foo.new.call
# 1, 2, 3, call, 4, 5
Objects including Callbacks
module can also be inherited preserving all callbacks:
class Bar < Foo
# Childrens before callbacks have higher precedence
before do
puts "7"; true
end
# Childrens around callbacks are higher in the stack
around do
puts "8"
yield
puts "9"
end
# Childrens after callbacks executed after parents'
after do
puts "10" # Will not be called as well because Foo's arounds stop at 5
end
end
Bar.new.call
# 7, 1, 2, 8, 3, 4, 9, 5
Contributing
- Fork it (https://github.com/vladfaust/callbacks.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- @vladfaust Vlad Faust - creator, maintainer