An expressive callbacks module for Crystal.
Thanks to all my patrons, I can build and support beautiful Open Source Software! ๐
Lauri Jutila, Alexander Maslov, Dainel Vera
You can become a patron too in exchange of prioritized support and other perks
Callbacks defined with this module are properly inherited and run within a scope of the object itself (i.e. have an access to instance variables etc.).
Add this to your application's shard.yml
:
dependencies:
callbacks:
github: vladfaust/callbacks.cr
version: ~> 0.2.0
This shard follows Semantic Versioning 2.0.0, so see releases and change the version
accordingly.
require "callbacks"
class Foo
include Callbacks
def call
with_callbacks { puts "call" }
end
before do
puts "1"
end
before do
puts "2"
end
after do
puts "3"
end
after do
puts "4"
end
end
Foo.new.call
# 1, 2, call, 3, 4
Objects including Callbacks
module can also be inherited preserving all callbacks:
class Bar < Foo
# Childrens before callbacks have higher precedence
before do
puts "5"
end
# Childrens after callbacks executed after parents'
after do
puts "6"
end
end
Bar.new.call
# 5, 1, 2, call, 3, 4, 6
- 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
- @vladfaust Vlad Faust - creator, maintainer