Class: Kward::Hooks::Manager
- Inherits:
-
Object
- Object
- Kward::Hooks::Manager
- Defined in:
- lib/kward/hooks/manager.rb
Overview
Dispatches lifecycle events to registered hooks and combines decisions.
Defined Under Namespace
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#on_result ⇒ Object
writeonly
Sets the attribute on_result.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(audit_log: AuditLog.new, on_result: nil) ⇒ Manager
constructor
A new instance of Manager.
- #register(event, id: nil, source: nil, order: 100, match: nil, failure_policy: nil, &callback) ⇒ Object
- #run(event, context: nil) ⇒ Object
Constructor Details
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
28 29 30 |
# File 'lib/kward/hooks/manager.rb', line 28 def handlers @handlers end |
#on_result=(value) ⇒ Object (writeonly)
Sets the attribute on_result
29 30 31 |
# File 'lib/kward/hooks/manager.rb', line 29 def on_result=(value) @on_result = value end |
Instance Method Details
#empty? ⇒ Boolean
54 55 56 |
# File 'lib/kward/hooks/manager.rb', line 54 def empty? @handlers.empty? end |
#register(event, id: nil, source: nil, order: 100, match: nil, failure_policy: nil, &callback) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kward/hooks/manager.rb', line 37 def register(event, id: nil, source: nil, order: 100, match: nil, failure_policy: nil, &callback) raise ArgumentError, "Hook requires an event name" if event.to_s.empty? raise ArgumentError, "Hook requires a handler" unless callback handler = Handler.new( id: id || "#{source || "hook"}:#{event}:#{@handlers.length + 1}", event: event.to_s, source: source, order: order.to_i, match: Matcher.new(match), callback: callback, failure_policy: Catalog.failure_policy(event, failure_policy) ) @handlers << handler handler end |
#run(event, context: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/kward/hooks/manager.rb', line 58 def run(event, context: nil) event = normalize_event(event) decisions = [] warnings = [] = [] payload = DeepCopy.dup(event.payload) handlers_for(event).each do |handler| decision = call_handler(handler, event, context) decisions << decision warnings << decision. if decision.warning? && decision. << decision. if decision. && !decision.warning? if decision.modify? modifications = Catalog.filter_modifications(event.name, decision.payload) payload = DeepCopy.merge(payload, modifications) event = event_with_payload(event, payload) end break if decision.deny? end final_decision = final_decision(decisions) result = Result.new( event: event, decision: final_decision, decisions: decisions, warnings: warnings, messages: , payload: payload ) @audit_log&.log_result(event: event, result: result) @on_result&.call(event, result) unless decisions.empty? result end |