Class: Textus::Hooks::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/hooks/dispatcher.rb

Constant Summary collapse

HOOK_TIMEOUT_SECONDS =
2

Instance Method Summary collapse

Constructor Details

#initialize(audit_log:) ⇒ Dispatcher

Returns a new instance of Dispatcher.



10
11
12
13
# File 'lib/textus/hooks/dispatcher.rb', line 10

def initialize(audit_log:)
  @audit_log = audit_log
  @subscribers = Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#publish(event, **kwargs) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/textus/hooks/dispatcher.rb', line 19

def publish(event, **kwargs)
  key = kwargs[:key] || "-"
  @subscribers[event.to_sym].each do |sub|
    next unless match?(sub[:keys], key)

    invoke(event, sub, key, kwargs)
  end
end

#subscribe(event, name, keys: nil, &block) ⇒ Object



15
16
17
# File 'lib/textus/hooks/dispatcher.rb', line 15

def subscribe(event, name, keys: nil, &block)
  @subscribers[event.to_sym] << { name: name.to_sym, callable: block, keys: keys }
end