Class: Praxis::Stage

Inherits:
Object
  • 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.



15
16
17
18
19
20
21
22
23
24
# File 'lib/praxis/stage.rb', line 15

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_callbacksObject (readonly)

Returns the value of attribute after_callbacks.



9
10
11
# File 'lib/praxis/stage.rb', line 9

def after_callbacks
  @after_callbacks
end

#before_callbacksObject (readonly)

Returns the value of attribute before_callbacks.



8
9
10
# File 'lib/praxis/stage.rb', line 8

def before_callbacks
  @before_callbacks
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/praxis/stage.rb', line 6

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/praxis/stage.rb', line 5

def name
  @name
end

#stagesObject (readonly)

Returns the value of attribute stages.



7
8
9
# File 'lib/praxis/stage.rb', line 7

def stages
  @stages
end

Instance Method Details

#after(*stage_path, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/praxis/stage.rb', line 79

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

#applicationObject



11
12
13
# File 'lib/praxis/stage.rb', line 11

def application
  context
end

#before(*stage_path, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/praxis/stage.rb', line 65

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_argsObject



61
62
63
# File 'lib/praxis/stage.rb', line 61

def callback_args
  nil
end

#executeObject



49
50
51
52
53
# File 'lib/praxis/stage.rb', line 49

def execute
  @stages.each do |stage|
    stage.run
  end
end

#execute_callbacks(callbacks) ⇒ Object



55
56
57
58
59
# File 'lib/praxis/stage.rb', line 55

def execute_callbacks(callbacks)
  callbacks.each do |callback|
    callback.call(callback_args, name: name)
  end
end

#runObject



26
27
28
29
30
# File 'lib/praxis/stage.rb', line 26

def run
  execute_callbacks(self.before_callbacks)
  execute
  execute_callbacks(self.after_callbacks)
end

#setup!Object



32
33
34
# File 'lib/praxis/stage.rb', line 32

def setup!
  setup_deferred_callbacks!
end

#setup_deferred_callbacks!Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/praxis/stage.rb', line 36

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