Skip to content

vladfaust/callbacks.cr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Callbacks

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

An expressive callbacks module for Crystal.

Supporters

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

Become Patron

About

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.).

Installation

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.

Usage

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

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