Module: ConfCtl::Hook

Defined in:
lib/confctl/hook.rb

Class Method Summary collapse

Class Method Details

.call(name, args: [], kwargs: {}) ⇒ any

Parameters:

  • name (Symbol)
  • args (Array) (defaults to: [])
  • kwargs (Hash) (defaults to: {})

Returns:

  • (any)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/confctl/hook.rb', line 23

def self.call(name, args: [], kwargs: {})
  return if @hooks.nil?

  subscribers = @hooks[name]
  return if subscribers.empty?

  subscribers.inject(nil) do |ret, sub|
    hook_kwargs = kwargs.merge(return_value: ret)
    sub.call(*args, **hook_kwargs)
  end
end

.register(name) ⇒ Object

Parameters:

  • name (Symbol)


4
5
6
7
# File 'lib/confctl/hook.rb', line 4

def self.register(name)
  @hooks ||= {}
  @hooks[name] ||= []
end

.subscribe(name, &block) ⇒ Object

Parameters:

  • name (Symbol)


10
11
12
13
14
15
16
17
# File 'lib/confctl/hook.rb', line 10

def self.subscribe(name, &block)
  subscribers = (@hooks || {})[name]

  raise "hook #{name.inspect} not registered" if subscribers.nil?

  subscribers << block
  nil
end