before.cr
before hook for Crystal.
- crystal: 0.23.1
Installation
Add this to your application's shard.yml
:
dependencies:
before:
github: maiha/before.cr
version: 0.1.0
Usage
original code
Let's image some notification application.
class Notify
def send(msg : String)
# some logic to send message
end
end
before.cr
add before hook by -
- insert
before
signature beferedef
- insert
-
- insert
__before__
at the beginning of method body
- insert
require "before"
class Notify
before def send(msg : String)
__before__
# some logic to send message
end
end
-
- This generates following codes
class Notify
def before_send(&callback : (String) ->)
(@before_send ||= [] of ((String) ->)) << callback
end
def send(msg : String)
@before_send.try &.each &.call(msg)
# some logic to send message
end
end
-
- now you can use
before_send
block where send is method name
- now you can use
notify = Notify.new
notify.before_send do |msg|
raise "Do Not Disturb" unless (10..18) === Time.now.hour
end
notify.send("Come to my office now!")
CAUTIONS
- variable type must be defined
- doesn't work with blocks
Development
make test
Contributing
- Fork it ( https://github.com/maiha/before.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
- maiha maiha - creator, maintainer