Class: Bogo::Stack::Hooks

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/bogo/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack:) ⇒ self

Create a new set hooks

Parameters:



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_entriesArray<Entry> (readonly)

Returns list of entries to append to specific actions.

Returns:

  • (Array<Entry>)

    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_entriesArray<Entry> (readonly)

Returns list of entries to append to stack actions.

Returns:

  • (Array<Entry>)

    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_entriesArray<Entry> (readonly)

Returns list of entries to prepend to specific actions.

Returns:

  • (Array<Entry>)

    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_entriesArray<Entry> (readonly)

Returns list of entries to prepend to stack actions.

Returns:

  • (Array<Entry>)

    list of entries to prepend to stack actions



10
11
12
# File 'lib/bogo/stack.rb', line 10

def prepend_entries
  @prepend_entries
end

#stackStack (readonly)

Returns stack associated with these hooks.

Returns:

  • (Stack)

    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

Parameters:

  • identifier (Symbol, Class, Proc)

    action to hook after

Returns:

  • (self)


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

Returns:

  • (self)


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.

Returns:

  • (Boolean)

    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

Returns:



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

Parameters:

  • identifier (Symbol, Class, Proc)

    action to hook before

Returns:

  • (self)


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

Returns:

  • (self)


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