Module: Hookem

Defined in:
lib/hookem.rb,
lib/hookem/version.rb

Defined Under Namespace

Classes: HookCallable, HookemNullObject

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerObject



37
38
39
# File 'lib/hookem.rb', line 37

def self.logger
  @_hookem_logger ||= HookemNullObject.new
end

Instance Method Details

#_initialize_hook_moduleObject



29
30
31
32
33
34
35
# File 'lib/hookem.rb', line 29

def _initialize_hook_module
  unless @_hook_module_initialized
    @hooks          = {}
    @hook_observers = []
    @_hook_module_initialized = true
  end
end

#_make_observer(callable, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
# File 'lib/hookem.rb', line 92

def _make_observer(callable, options = {})
  raise ArgumentError, "Must provide callable or block" if callable.nil?
  HookCallable.new({:eventloop => @hook_run_loop}.
                       merge(options).
                       merge(:callable => callable)).freeze
end

#add_hook(hook_name, observer = nil, options = {}, &block) ⇒ Object



66
67
68
69
70
71
# File 'lib/hookem.rb', line 66

def add_hook(hook_name, observer = nil, options = {}, &block)
  @hooks[hook_name] ||= []
  hook = _make_observer(observer || block, options)
  @hooks[hook_name] << hook
  hook.identifier
end

#add_hook_observer(observer = nil, options = {}, &block) ⇒ Object



73
74
75
76
77
# File 'lib/hookem.rb', line 73

def add_hook_observer(observer = nil, options={}, &block)
  hook = _make_observer(observer || block, options)
  @hook_observers << hook
  hook.identifier
end

#add_hooks(hookmap, options = {}) ⇒ Object

Takes a hash of hook_name_symbol => hooklist hooklist can be a single proc or array of procs



58
59
60
61
62
63
64
# File 'lib/hookem.rb', line 58

def add_hooks(hookmap, options = {})
  hookmap.each do |(name, val)|
    Array(val).each do |hook|
      add_hook name.to_sym, hook, options
    end
  end
end

#remove_hook(hook_name, identifier) ⇒ Object



79
80
81
82
# File 'lib/hookem.rb', line 79

def remove_hook(hook_name, identifier)
  return unless @hooks[hook_name]
  @hooks[hook_name].reject! {|hook| hook.identifier == identifier }
end

#remove_hook_observer(identifier) ⇒ Object



84
85
86
# File 'lib/hookem.rb', line 84

def remove_hook_observer(identifier)
  @hook_observers.reject! {|hook| hook.identifier == identifier }
end

#run_hook(hook_name, *args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/hookem.rb', line 41

def run_hook(hook_name, *args)
  hook_name = hook_name.to_sym
  Hookem.logger.debug "<running hook #{hook_name}(#{args.inspect})>"
  if @hooks && @hooks[hook_name]
    Array(@hooks[hook_name]).each do |hook|
      hook.call(self, *args)
    end
  end
  Array(@hook_observers).each do |observer|
    observer.call(hook_name, self, *args)
  end
end

#use_run_loop_for_hooks(runloop) ⇒ Object



88
89
90
# File 'lib/hookem.rb', line 88

def use_run_loop_for_hooks(runloop)
  @hook_run_loop = runloop
end