Module: Hooks

Included in:
HooksTest::TestClass, Middleman::Application
Defined in:
lib/middleman-core/vendor/hooks-0.2.0/lib/hooks.rb,
lib/middleman-core/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb

Overview

Almost like ActiveSupport::Callbacks but 76,6% less complex.

Example:

class CatWidget < Apotomo::Widget
  define_hook :after_dinner

Now you can add callbacks to your hook declaratively in your class.

after_dinner do puts "Ice cream!" end
after_dinner :have_a_desert   # => refers to CatWidget#have_a_desert

Running the callbacks happens on instances. It will run the block and #have_a_desert from above.

cat.run_hook :after_dinner

Defined Under Namespace

Modules: ClassMethods, InheritableAttribute

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



21
22
23
24
# File 'lib/middleman-core/vendor/hooks-0.2.0/lib/hooks.rb', line 21

def self.included(base)
  base.extend InheritableAttribute
  base.extend ClassMethods
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

desert("i want ice cream!")
block.call("i want ice cream!")


106
107
108
# File 'lib/middleman-core/vendor/hooks-0.2.0/lib/hooks.rb', line 106

def run_hook(name, *args)
  self.class.run_hook_for(name, self, *args)
end