class Praxis::Stage

Attributes

after_callbacks[R]
before_callbacks[R]
context[R]
name[R]
stages[R]

Public Class Methods

new(name, context, **_opts) click to toggle source
# 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

Public Instance Methods

after(*stage_path, &block) click to toggle source
# 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
application() click to toggle source
# File lib/praxis/stage.rb, line 7
def application
  context
end
before(*stage_path, &block) click to toggle source
# 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_args() click to toggle source
# File lib/praxis/stage.rb, line 55
def callback_args
  nil
end
execute() click to toggle source
# File lib/praxis/stage.rb, line 45
def execute
  @stages.each(&:run)
end
execute_callbacks(callbacks) click to toggle source
# File lib/praxis/stage.rb, line 49
def execute_callbacks(callbacks)
  callbacks.each do |callback|
    callback.call(callback_args, name: name)
  end
end
run() click to toggle source
# File lib/praxis/stage.rb, line 22
def run
  execute_callbacks(before_callbacks)
  execute
  execute_callbacks(after_callbacks)
end
setup!() click to toggle source
# File lib/praxis/stage.rb, line 28
def setup!
  setup_deferred_callbacks!
end
setup_deferred_callbacks!() click to toggle source
# 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