Class: Lolita::Hooks::NamedHook

Inherits:
Object
  • Object
show all
Defined in:
lib/lolita/hooks/named_hook.rb

Overview

Named hooks is special hook class that uses Lolita::Hooks, but also add some other useful methods and behaviour. Main reason for named hooks is to provide filter like hooks. First thing is to define it for futher use.

Lolita::Hooks::NamedHook.add(:people)

Next step is to add hook for it. Like this

Lolita::Hooks.people.add_hook(:get_married)

And then add callback for named hook.

Lolita::Hooks.person.get_merried do
  go_to_honeymoon
end
john=Person.new("John")
john.run(:get_merried) # and now john will go to honeymoon

Hooks are added for pluralized hook name, even if you pass like #add(:person), it will create named hook with people name. This will be class, and when you call #person it will create instance for that class.

Class Method Summary collapse

Class Method Details

.add(name) ⇒ Object

Add named hook name with NamedHook class. Each hook is Hash and keys is filters. Also it have :_class key that is class for named hook.



22
23
24
25
26
# File 'lib/lolita/hooks/named_hook.rb', line 22

def add(name)
  name=name.to_s.pluralize
  named_hooks[name.to_sym]={:_class=>get_named_hook_class(name)}
  add_filter_method(name)
end

.by_name(name) ⇒ Object

Return named hook by given name.



58
59
60
61
# File 'lib/lolita/hooks/named_hook.rb', line 58

def by_name(name)
  name=name.to_s.pluralize
  named_hooks[name.to_sym]
end

.exist?(name) ⇒ Boolean

Detect that named hook exist with given name.

Returns:

  • (Boolean)


34
35
36
# File 'lib/lolita/hooks/named_hook.rb', line 34

def exist?(name)
  self.names.include?(name)
end

.find(hook_name, filter_name) ⇒ Object

Find named hook filter.

Example

Lolita::Hooks::NamedHook.find(:components,:"list")


51
52
53
54
55
# File 'lib/lolita/hooks/named_hook.rb', line 51

def find(hook_name,filter_name)
  if named_hook=self.by_name(hook_name)
    named_hook[filter_name.to_sym]
  end
end

.find_or_create(hook_name, filter_name) ⇒ Object

Find or create named hook instance.



39
40
41
42
43
44
45
46
# File 'lib/lolita/hooks/named_hook.rb', line 39

def find_or_create(hook_name,filter_name)
  unless filtered_hook=self.find(hook_name,filter_name)
    named_hook=self.by_name(hook_name)
    named_hook[:_class].new(hook_name,filter_name)
  else
    filtered_hook
  end
end

.namesObject

All defined named hook names



29
30
31
# File 'lib/lolita/hooks/named_hook.rb', line 29

def names
  named_hooks.keys
end