Class: Featurevisor::Hooks::HooksManager
- Inherits:
-
Object
- Object
- Featurevisor::Hooks::HooksManager
- Defined in:
- lib/featurevisor/hooks.rb
Overview
HooksManager class for managing hooks
Instance Attribute Summary collapse
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#add(hook) ⇒ Proc?
Add a hook to the manager.
-
#get_all ⇒ Array<Hook>
Get all hooks.
-
#initialize(options) ⇒ HooksManager
constructor
Initialize a new HooksManager.
-
#remove(name) ⇒ Object
Remove a hook by name.
-
#run_after_hooks(evaluation, options) ⇒ Hash
Run after hooks.
-
#run_before_hooks(options) ⇒ Hash
Run before hooks.
-
#run_bucket_key_hooks(options) ⇒ String
Run bucket key hooks.
-
#run_bucket_value_hooks(options) ⇒ Integer
Run bucket value hooks.
Constructor Details
#initialize(options) ⇒ HooksManager
Initialize a new HooksManager
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/featurevisor/hooks.rb', line 71 def initialize() @logger = [:logger] @hooks = [] if [:hooks] [:hooks].each do |hook| add(hook) end end end |
Instance Attribute Details
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
65 66 67 |
# File 'lib/featurevisor/hooks.rb', line 65 def hooks @hooks end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
65 66 67 |
# File 'lib/featurevisor/hooks.rb', line 65 def logger @logger end |
Instance Method Details
#add(hook) ⇒ Proc?
Add a hook to the manager
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/featurevisor/hooks.rb', line 85 def add(hook) if @hooks.any? { |existing_hook| existing_hook.name == hook.name } @logger.error("Hook with name \"#{hook.name}\" already exists.", { name: hook.name, hook: hook }) return nil end @hooks << hook # Return a remove function -> { remove(hook.name) } end |
#get_all ⇒ Array<Hook>
Get all hooks
109 110 111 |
# File 'lib/featurevisor/hooks.rb', line 109 def get_all @hooks end |
#remove(name) ⇒ Object
Remove a hook by name
103 104 105 |
# File 'lib/featurevisor/hooks.rb', line 103 def remove(name) @hooks = @hooks.reject { |hook| hook.name == name } end |
#run_after_hooks(evaluation, options) ⇒ Hash
Run after hooks
150 151 152 153 154 155 156 |
# File 'lib/featurevisor/hooks.rb', line 150 def run_after_hooks(evaluation, ) result = evaluation @hooks.each do |hook| result = hook.call_after(result, ) end result end |
#run_before_hooks(options) ⇒ Hash
Run before hooks
116 117 118 119 120 121 122 |
# File 'lib/featurevisor/hooks.rb', line 116 def run_before_hooks() result = @hooks.each do |hook| result = hook.call_before(result) end result end |
#run_bucket_key_hooks(options) ⇒ String
Run bucket key hooks
127 128 129 130 131 132 133 |
# File 'lib/featurevisor/hooks.rb', line 127 def run_bucket_key_hooks() bucket_key = [:bucket_key] @hooks.each do |hook| bucket_key = hook.call_bucket_key(.merge(bucket_key: bucket_key)) end bucket_key end |
#run_bucket_value_hooks(options) ⇒ Integer
Run bucket value hooks
138 139 140 141 142 143 144 |
# File 'lib/featurevisor/hooks.rb', line 138 def run_bucket_value_hooks() bucket_value = [:bucket_value] @hooks.each do |hook| bucket_value = hook.call_bucket_value(.merge(bucket_value: bucket_value)) end bucket_value end |