Class: Dynflow::ExecutionPlan::Hooks::Register
- Inherits:
-
Object
- Object
- Dynflow::ExecutionPlan::Hooks::Register
- Defined in:
- lib/dynflow/execution_plan/hooks.rb
Overview
A register holding information about hook classes and events which should trigger the hooks.
Instance Attribute Summary collapse
-
#hooks ⇒ Hash<Class, Set<Symbol>>
readonly
a hash mapping hook classes to events which should trigger the hooks.
Instance Method Summary collapse
-
#do_not_use(class_name, on: HOOK_KINDS) ⇒ void
Disables a hook from being run on certain events.
-
#dup ⇒ Register
Performs a deep clone of the hooks register.
-
#initialize(hooks = {}) ⇒ Register
constructor
A new instance of Register.
-
#on(kind) ⇒ Array<Class>
Returns which hooks should be run on certain event.
-
#run(execution_plan, action, kind) ⇒ Object
Runs the registered hooks.
-
#use(class_name, on: ExecutionPlan.states) ⇒ void
Sets a hook to be run on certain events.
Constructor Details
#initialize(hooks = {}) ⇒ Register
Returns a new instance of Register.
15 16 17 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 15 def initialize(hooks = {}) @hooks = hooks end |
Instance Attribute Details
#hooks ⇒ Hash<Class, Set<Symbol>> (readonly)
a hash mapping hook classes to events which should trigger the hooks
12 13 14 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 12 def hooks @hooks end |
Instance Method Details
#do_not_use(class_name, on: HOOK_KINDS) ⇒ void
This method returns an undefined value.
Disables a hook from being run on certain events
39 40 41 42 43 44 45 46 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 39 def do_not_use(class_name, on: HOOK_KINDS) on = Array[on] unless on.kind_of?(Array) validate_kinds!(on) if hooks[class_name] hooks[class_name] -= on hooks.delete(class_name) if hooks[class_name].empty? end end |
#dup ⇒ Register
Performs a deep clone of the hooks register
51 52 53 54 55 56 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 51 def dup new_hooks = hooks.reduce({}) do |acc, (key, value)| acc.update(key => value.dup) end self.class.new(new_hooks) end |
#on(kind) ⇒ Array<Class>
Returns which hooks should be run on certain event.
82 83 84 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 82 def on(kind) hooks.select { |_key, on| on.include? kind }.keys end |
#run(execution_plan, action, kind) ⇒ Object
Runs the registered hooks
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 63 def run(execution_plan, action, kind) hooks = on(kind) return if hooks.empty? Executors.run_user_code do hooks.each do |hook| begin action.send(hook, execution_plan) rescue => e execution_plan.logger.error "Failed to run hook '#{hook}' for action '#{action.class}'" execution_plan.logger.error e end end end end |
#use(class_name, on: ExecutionPlan.states) ⇒ void
This method returns an undefined value.
Sets a hook to be run on certain events
24 25 26 27 28 29 30 31 32 |
# File 'lib/dynflow/execution_plan/hooks.rb', line 24 def use(class_name, on: ExecutionPlan.states) on = Array[on] unless on.kind_of?(Array) validate_kinds!(on) if hooks[class_name] hooks[class_name] += on else hooks[class_name] = on end end |