Crochets
This gem is kinda syntactic sugar for setting hooks on ruby method calls.
It uses Aquarium for aspects.
Installation
Add this line to your application's Gemfile:
gem 'crochets'
And then execute:
$ bundle
Or install it yourself as:
$ gem install crochets
Usage
The usage is straightforward:
class MyClass
def my_method
puts "Hello, world!"
end
end
@aspect = Crochets.hook(MyClass, :my_method, after: true, blocking: false) do |obj, *args|
puts "NB: world is now helloed."
end
# ⇒ Hello, world!
# ⇒ NB: world is now helloed.
The hook may then be unset:
@aspect.unhook
There are many hooks on the same method may be set simultaneously. They will be invoked in the order they were set.
Arguments
The method Crochets::Crocheter#hook
accepts following parameters:
-
object or class
[Class|Object]
the object or class to set the hook on; -
method
Symbol
the name of the method to set the hook on, or:all_methods
-
after
[TrueClass|FalseClass]
apply the hook after method body execution -
before
[TrueClass|FalseClass]
apply the hook before method body execution -
blocking
[TrueClass|FalseClass]
whenfalse
, the hook will be executed in the separate thread.
Contributing
- Fork it
- 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 new Pull Request