Class: Bogo::Stack::Hooks
- Inherits:
-
Object
- Object
- Bogo::Stack::Hooks
- Includes:
- MonitorMixin
- Defined in:
- lib/bogo/stack.rb
Instance Attribute Summary collapse
-
#after_entries ⇒ Array<Entry>
readonly
List of entries to append to specific actions.
-
#append_entries ⇒ Array<Entry>
readonly
List of entries to append to stack actions.
-
#before_entries ⇒ Array<Entry>
readonly
List of entries to prepend to specific actions.
-
#prepend_entries ⇒ Array<Entry>
readonly
List of entries to prepend to stack actions.
-
#stack ⇒ Stack
readonly
Stack associated with these hooks.
Instance Method Summary collapse
-
#after(identifier, &block) ⇒ self
Add hook after identifier.
-
#append(&block) ⇒ self
Add hook after stack actions.
-
#applied? ⇒ Boolean
Hooks have been applied to stack.
-
#apply! ⇒ Array<Action>
Apply hooks to stack action list.
-
#before(identifier, &block) ⇒ self
Add hook before identifier.
-
#initialize(stack:) ⇒ self
constructor
Create a new set hooks.
-
#prepend(&block) ⇒ self
Add hook before stack actions.
Constructor Details
#initialize(stack:) ⇒ self
Create a new set hooks
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/bogo/stack.rb', line 25 def initialize(stack:) super() if !stack.is_a?(Stack) raise TypeError, "Expecting `#{Stack.name}` but received `#{stack.class.name}`" end @prepend_entries = [].freeze @append_entries = [].freeze @after_entries = [].freeze @before_entries = [].freeze @applied = false @stack = stack end |
Instance Attribute Details
#after_entries ⇒ Array<Entry> (readonly)
Returns list of entries to append to specific actions.
16 17 18 |
# File 'lib/bogo/stack.rb', line 16 def after_entries @after_entries end |
#append_entries ⇒ Array<Entry> (readonly)
Returns list of entries to append to stack actions.
12 13 14 |
# File 'lib/bogo/stack.rb', line 12 def append_entries @append_entries end |
#before_entries ⇒ Array<Entry> (readonly)
Returns list of entries to prepend to specific actions.
14 15 16 |
# File 'lib/bogo/stack.rb', line 14 def before_entries @before_entries end |
#prepend_entries ⇒ Array<Entry> (readonly)
Returns list of entries to prepend to stack actions.
10 11 12 |
# File 'lib/bogo/stack.rb', line 10 def prepend_entries @prepend_entries end |
#stack ⇒ Stack (readonly)
Returns stack associated with these hooks.
19 20 21 |
# File 'lib/bogo/stack.rb', line 19 def stack @stack end |
Instance Method Details
#after(identifier, &block) ⇒ self
Add hook after identifier
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/bogo/stack.rb', line 44 def after(identifier, &block) be_callable!(identifier) unless identifier.is_a?(Symbol) be_callable!(block) synchronize do if applied? raise Error::ApplyError, "Hooks have already been applied to stack" end @after_entries = after_entries + [Entry.new(identifier: identifier, action: Action.new(stack: stack, callable: block))] @after_entries.freeze end self end |
#append(&block) ⇒ self
Add hook after stack actions
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/bogo/stack.rb', line 103 def append(&block) be_callable!(block) synchronize do if applied? raise Error::ApplyError, "Hooks have already been applied to stack" end @append_entries = append_entries + [Action.new(stack: stack, callable: block)] @append_entries.freeze end self end |
#applied? ⇒ Boolean
Returns hooks have been applied to stack.
118 119 120 |
# File 'lib/bogo/stack.rb', line 118 def applied? !!@applied end |
#apply! ⇒ Array<Action>
Apply hooks to stack action list
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/bogo/stack.rb', line 125 def apply! synchronize do if applied? raise Error::ApplyError, "Hooks have already been applied to stack" end actions = stack.actions.dup stubs = [:stub] * actions.size before_entries.find_all { |e| e.identifier == :all }.each do |entry| stubs.count.times.to_a.reverse.each do |i| stubs.insert(i, entry.action) end end after_entries.find_all { |e| e.identifier == :all }.each do |entry| stubs.count.times.to_a.reverse.each do |i| stubs.insert(i + 1, entry.action) end end actions = stubs.map do |item| item == :stub ? actions.pop : item end before_entries.find_all { |e| e.identifier != :all }.each do |entry| idx = actions.index { |a| a.callable == entry.identifier } next if idx.nil? actions.insert(idx, entry.action) end after_entries.find_all { |e| e.identifier != :all }.each do |entry| idx = actions.index { |a| a.callable == entry.identifier } next if idx.nil? actions.insert(idx + 1, entry.action) end @applied = true actions = prepend_entries + actions + append_entries end end |
#before(identifier, &block) ⇒ self
Add hook before identifier
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bogo/stack.rb', line 65 def before(identifier, &block) be_callable!(identifier) unless identifier.is_a?(Symbol) be_callable!(block) synchronize do if applied? raise Error::ApplyError, "Hooks have already been applied to stack" end @before_entries = before_entries + [Entry.new(identifier: identifier, action: Action.new(stack: stack, callable: block))] @before_entries.freeze end self end |
#prepend(&block) ⇒ self
Add hook before stack actions
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bogo/stack.rb', line 85 def prepend(&block) be_callable!(block) synchronize do if applied? raise Error::ApplyError, "Hooks have already been applied to stack" end @prepend_entries = prepend_entries + [Action.new(stack: stack, callable: block)] @prepend_entries.freeze end self end |