Class: Praxis::Stage
- Inherits:
-
Object
show all
- Defined in:
- lib/praxis/stage.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, context, **opts) ⇒ Stage
Returns a new instance of Stage.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/praxis/stage.rb', line 11
def initialize(name, context,**opts)
@name = name
@context = context
@before_callbacks = Array.new
@after_callbacks = Array.new
@deferred_callbacks = Hash.new do |hash,stage|
hash[stage] = {before: [], after:[]}
end
@stages = Array.new
end
|
Instance Attribute Details
#after_callbacks ⇒ Object
Returns the value of attribute after_callbacks.
5
6
7
|
# File 'lib/praxis/stage.rb', line 5
def after_callbacks
@after_callbacks
end
|
#before_callbacks ⇒ Object
Returns the value of attribute before_callbacks.
5
6
7
|
# File 'lib/praxis/stage.rb', line 5
def before_callbacks
@before_callbacks
end
|
#context ⇒ Object
Returns the value of attribute context.
5
6
7
|
# File 'lib/praxis/stage.rb', line 5
def context
@context
end
|
#name ⇒ Object
Returns the value of attribute name.
5
6
7
|
# File 'lib/praxis/stage.rb', line 5
def name
@name
end
|
#stages ⇒ Object
Returns the value of attribute stages.
5
6
7
|
# File 'lib/praxis/stage.rb', line 5
def stages
@stages
end
|
Instance Method Details
#after(*stage_path, &block) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/praxis/stage.rb', line 78
def after(*stage_path, &block)
if stage_path.any?
stage_name = stage_path.shift
stage = stages.find { |stage| stage.name == stage_name }
if stage
stage.after(*stage_path, &block)
else
@deferred_callbacks[stage_name][:after] << [*stage_path, block]
end
else
@after_callbacks << block
end
end
|
#application ⇒ Object
7
8
9
|
# File 'lib/praxis/stage.rb', line 7
def application
context
end
|
#before(*stage_path, &block) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/praxis/stage.rb', line 64
def before(*stage_path, &block)
if stage_path.any?
stage_name = stage_path.shift
stage = stages.find { |stage| stage.name == stage_name }
if stage
stage.before(*stage_path, &block)
else
@deferred_callbacks[stage_name][:before] << [*stage_path, block]
end
else
@before_callbacks << block
end
end
|
#callback_args ⇒ Object
60
61
62
|
# File 'lib/praxis/stage.rb', line 60
def callback_args
nil
end
|
#execute ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/praxis/stage.rb', line 46
def execute
raise NotImplementedError, 'Subclass must implement Stage#execute' unless @stages.any?
@stages.each do |stage|
stage.run
end
end
|
#execute_callbacks(callbacks) ⇒ Object
54
55
56
57
58
|
# File 'lib/praxis/stage.rb', line 54
def execute_callbacks(callbacks)
callbacks.each do |callback|
callback.call(callback_args, name: name)
end
end
|
#run ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/praxis/stage.rb', line 22
def run
setup!
setup_deferred_callbacks!
execute_callbacks(self.before_callbacks)
execute
execute_callbacks(self.after_callbacks)
end
|
#setup! ⇒ Object
30
31
|
# File 'lib/praxis/stage.rb', line 30
def setup!
end
|
#setup_deferred_callbacks! ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/praxis/stage.rb', line 33
def setup_deferred_callbacks!
@deferred_callbacks.keys.each do |stage_name|
callbacks = @deferred_callbacks.delete stage_name
callbacks[:before].each do |(*stage_path, block)|
self.before(stage_name, *stage_path, &block)
end
callbacks[:after].each do |(*stage_path, block)|
self.after(stage_name, *stage_path, &block)
end
end
end
|