Module: Hooks::ClassMethods

Defined in:
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.

As callbacks can be static values, lambdas or methods, they're wrapped by Uber::Option::Value which gives you a very convenient way to execute the callback without knowing its type using Value#evaluate.

Example:

def initialize
  self.class.callbacks_for_hook(:after_eight).each do |callback|
    callback.evaluate(self, "create")
  end

Runs callbacks in the object instance context and pass “create” as the only argument.


73
74
75
# File 'lib/hooks.rb', line 73

def callbacks_for_hook(name)
  _hooks[name]
end

#define_hooks(*names) ⇒ Object Also known as: define_hook


31
32
33
34
35
36
37
# File 'lib/hooks.rb', line 31

def define_hooks(*names)
  options = extract_options!(names)

  names.each do |name|
    setup_hook(name, options)
  end
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.


50
51
52
# File 'lib/hooks.rb', line 50

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

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


54
55
56
# File 'lib/hooks.rb', line 54

def run_hook_for(name, scope, *args)
  _hooks[name].run(scope, *args)
end