Module: Hooks
- Defined in:
- lib/hooks.rb,
lib/hooks/hook.rb,
lib/hooks/version.rb,
lib/hooks/instance_hooks.rb,
lib/hooks/inheritable_attribute.rb
Overview
Almost like ActiveSupport::Callbacks but 76,6% less complex.
Example:
class CatWidget < Apotomo::Widget
define_hooks :before_dinner, :after_dinner
Now you can add callbacks to your hook declaratively in your class.
before_dinner :wash_paws
after_dinner { puts "Ice cream!" }
after_dinner :have_a_dessert # => refers to CatWidget#have_a_dessert
Running the callbacks happens on instances. It will run the block and #have_a_dessert from above.
cat.run_hook :after_dinner
Defined Under Namespace
Modules: ClassMethods, InstanceHooks Classes: Hook, HookSet
Constant Summary collapse
- VERSION =
"0.4.1"
- InheritableAttribute =
Uber::InheritableAttr
Class Method Summary collapse
Instance Method Summary collapse
-
#run_hook(name, *args) ⇒ Object
Runs the callbacks (method/block) for the specified hook
name
.
Class Method Details
.included(base) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/hooks.rb', line 21 def self.included(base) base.class_eval do extend Uber::InheritableAttr extend ClassMethods inheritable_attr :_hooks self._hooks= HookSet.new end end |
Instance Method Details
#run_hook(name, *args) ⇒ Object
Runs the callbacks (method/block) for the specified hook name
. Additional arguments will be passed to the callback.
Example:
cat.run_hook :after_dinner, "i want ice cream!"
will invoke the callbacks like
dessert("i want ice cream!")
block.call("i want ice cream!")
114 115 116 |
# File 'lib/hooks.rb', line 114 def run_hook(name, *args) self.class.run_hook_for(name, self, *args) end |