Module: Spree::ThemeSupport::Hook

Defined in:
lib/spree_core/theme_support/hook.rb

Constant Summary collapse

@@listener_classes =
[]
@@listeners =
nil
@@hook_modifiers =
{}

Class Method Summary collapse

Class Method Details

.add_listener(klass) ⇒ Object

Adds a listener class. Automatically called when a class inherits from Spree::ThemeSupport::HookListener.



12
13
14
15
16
# File 'lib/spree_core/theme_support/hook.rb', line 12

def add_listener(klass)
  raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
  @@listener_classes << klass
  clear_listeners_instances
end

.clear_listenersObject

Clears all the listeners.



24
25
26
27
# File 'lib/spree_core/theme_support/hook.rb', line 24

def clear_listeners
  @@listener_classes = []
  clear_listeners_instances
end

.clear_listeners_instancesObject

Clears all the listeners instances.



30
31
32
33
# File 'lib/spree_core/theme_support/hook.rb', line 30

def clear_listeners_instances
  @@listeners = nil
  @@hook_modifiers = {}
end

.listenersObject

Returns all the listerners instances.



19
20
21
# File 'lib/spree_core/theme_support/hook.rb', line 19

def listeners
  @@listeners ||= @@listener_classes.uniq.collect {|listener| listener.instance}
end

.modifiers_for_hook(hook_name) ⇒ Object

All the HookModifier instances that are associated with this hook_name in extension load order and order they were defined



41
42
43
# File 'lib/spree_core/theme_support/hook.rb', line 41

def modifiers_for_hook(hook_name)
  @@hook_modifiers[hook_name] ||= listeners.map {|l| l.modifiers_for_hook(hook_name)}.flatten
end

.render_hook(hook_name, content, context, locals = {}) ⇒ Object

Take the content captured with a hook helper and modify with each HookModifier



36
37
38
# File 'lib/spree_core/theme_support/hook.rb', line 36

def render_hook(hook_name, content, context, locals = {})
  modifiers_for_hook(hook_name).inject(content) { |result, modifier| modifier.apply_to(result, context, locals) }
end