Module: Lolita::Hooks
- Defined in:
- lib/lolita/hooks.rb,
lib/lolita.rb,
lib/lolita/hooks/named_hook.rb
Overview
Provide hook mechanism for Lolita. To use hooks for class start with including this in your own class. Next step is hook definition. This may be done using Lolita::Hooks#add_hook method. Hooks are stored in class @hooks variable, that is Hash and each key is hook name and each hook also is Hash that have :methods and :blocks keys. Both of those are Array, and each time you call callback method, like before_save and so on, block and/or methods is stored. Each time #fire is called all blocks and methods will be executed. It may look like this.
class MyClass
include Lolita::Hooks
add_hook :before_save, :after_save
end
# This will define two hooks for MyClass.
To add hook callback just call hook name on class and pass method(-s) or block.
MyClass.after_save :write_log
MyClass.before_save do
validate(self)
end
Scopes
Most times hook callbacks are defined for class like in previous example, but also it’s possible to do it on class instances. Difference between calling it on class or on instance is that instance callbacks will be called only when event is fired on instance. Class callbacks will be called on class and also on instance callbacks.
my_object=MyClass.new
MyClass.before_save do
puts "class callback"
end
my_object.before_save do
puts "instance callback"
end
MyClass.fire(:before_save) #=>
class_callback
my_object.fire(:before_save) #=>
class_callback
instance_callback
# As you can see, first class callbacks is called and after that instance callbacks.
Firing events
To execute callbacks, events should be called on object. Event names is same hooks names. #fire can be called on class or on instance. Also it is possible to pass block to fire event, that will replace callback block or if #let_content is called than it will work like wrapper, like this
# this is continuation of previous code
MyClass.fire(:before_save) do
puts "replaced text"
end
# will produce #=> replaced text
MyClass.fire(:before_save) do
puts "before callback"
let_content
puts "after callback"
end
# this will produce #=>
# before callback
# class callback
# after callback
Named hooks
See Lolita::Hooks::NamedHook for details.
Defined Under Namespace
Modules: ClassMethods, CommonMethods, InstanceMethods Classes: NamedHook
Class Method Summary collapse
- .included(base) ⇒ Object
-
.method_missing(method_name, *args, &block) ⇒ Object
Look for named hook with singular or plural name of method.
Class Method Details
.included(base) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/lolita/hooks.rb', line 62 def self.included(base) base.extend(ClassMethods) base.extend(CommonMethods) base.class_eval{ include CommonMethods include InstanceMethods } end |
.method_missing(method_name, *args, &block) ⇒ Object
Look for named hook with singular or plural name of method.
72 73 74 75 76 77 78 |
# File 'lib/lolita/hooks.rb', line 72 def self.method_missing method_name,*args, &block if named_hook=(Lolita::Hooks::NamedHook.by_name(method_name)) named_hook[:_class] else super end end |