Module: Lolita::Hooks

Included in:
Configuration::Field::Array, Navigation::Tree, RestController
Defined in:
lib/lolita/hooks.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 #run 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 runned 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.run(:before_save) #=>
   class_callback

 my_object.run(: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. #run can be called on class or on instance. Also it is possible to pass block to run 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.run(:before_save) do
  puts "replaced text"
end
# will produce #=> replaced text

MyClass.run(: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, Runner

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

end of Runner



210
211
212
213
214
215
216
217
# File 'lib/lolita/hooks.rb', line 210

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.



220
221
222
223
224
225
226
# File 'lib/lolita/hooks.rb', line 220

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