Module: Hooks::ClassMethods

Defined in:
lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks.rb

Instance Method Summary collapse

Instance Method Details

#callbacks_for_hook(name) ⇒ Object

Returns the callbacks for name. Handy if you want to run the callbacks yourself, say when they should be executed in another context.

Example:

def initialize
  self.class.callbacks_for_hook(:after_eight).each do |callback|
    instance_exec(self, &callback)
  end

would run callbacks in the object instance context, passing self as block parameter.



69
70
71
# File 'lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks.rb', line 69

def callbacks_for_hook(name)
  send("_#{name}_callbacks")
end

#define_hook(name) ⇒ Object



27
28
29
30
31
32
# File 'lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks.rb', line 27

def define_hook(name)
  accessor_name = "_#{name}_callbacks"

  setup_hook_accessors(accessor_name)
  define_hook_writer(name, accessor_name)
end

#run_hook(name, *args) ⇒ Object

Like Hooks#run_hook but for the class. Note that :callbacks must be class methods.

Example:

class Cat

after_eight :grab_a_beer

def self.grab_a_beer(*) # and so on...

where Cat.run_hook :after_eight will call the class method grab_a_beer.



44
45
46
# File 'lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks.rb', line 44

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

#run_hook_for(name, scope, *args) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/vendored-middleman-deps/hooks-0.2.0/lib/hooks.rb', line 48

def run_hook_for(name, scope, *args)
  callbacks_for_hook(name).each do |callback|
    if callback.kind_of? Symbol
      scope.send(callback, *args)
    else
      scope.instance_exec(*args, &callback)
    end
  end
end