Expressive callbacks module for Crystal 🚉
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
spec
src
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
shard.yml

README.md

Callbacks

Built with Crystal Build status Docs Releases Awesome vladfaust.com Patrons count

An expressive callbacks module for Crystal.

Become a Patron

Installation

Add this to your application's shard.yml:

dependencies:
  callbacks:
    github: vladfaust/callbacks.cr
    version: ~> 0.1.1

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, call, 4, 9, 5

Contributing

  1. Fork it (https://github.com/vladfaust/callbacks.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors