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.



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 = []
  @after_callbacks = []
  @deferred_callbacks = Hash.new do |hash, stage|
    hash[stage] = { before: [], after: [] }
  end
  @stages = []
end

Instance Attribute Details

#after_callbacksObject (readonly)

Returns the value of attribute after_callbacks.



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

def after_callbacks
  @after_callbacks
end

#before_callbacksObject (readonly)

Returns the value of attribute before_callbacks.



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

def before_callbacks
  @before_callbacks
end

#contextObject (readonly)

Returns the value of attribute context.



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

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.



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

def stages
  @stages
end

Instance Method Details

#after(*stage_path, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/praxis/stage.rb', line 73

def after(*stage_path, &block)
  if stage_path.any?
    stage_name = stage_path.shift
    stage = stages.find { |s| s.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



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

def application
  context
end

#before(*stage_path, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/praxis/stage.rb', line 59

def before(*stage_path, &block)
  if stage_path.any?
    stage_name = stage_path.shift
    stage = stages.find { |s| s.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



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

def callback_args
  nil
end

#executeObject



45
46
47
# File 'lib/praxis/stage.rb', line 45

def execute
  @stages.each(&:run)
end

#execute_callbacks(callbacks) ⇒ Object



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

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

#runObject



22
23
24
25
26
# File 'lib/praxis/stage.rb', line 22

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

#setup!Object



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

def setup!
  setup_deferred_callbacks!
end

#setup_deferred_callbacks!Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/praxis/stage.rb', line 32

def setup_deferred_callbacks!
  @deferred_callbacks.each_key do |stage_name|
    callbacks = @deferred_callbacks.delete stage_name
    callbacks[:before].each do |(*stage_path, block)|
      before(stage_name, *stage_path, &block)
    end

    callbacks[:after].each do |(*stage_path, block)|
      after(stage_name, *stage_path, &block)
    end
  end
end