class Praxis::Dispatcher

Attributes

deferred_callbacks[R]
action[R]
application[R]
controller[R]
request[R]

Public Class Methods

after(*_stage_path, **conditions, &block) click to toggle source
# File lib/praxis/dispatcher.rb, line 25
def self.after(*_stage_path, **conditions, &block)
  @deferred_callbacks[:after] << [conditions, block]
end
before(*_stage_path, **conditions, &block) click to toggle source
# File lib/praxis/dispatcher.rb, line 21
def self.before(*_stage_path, **conditions, &block)
  @deferred_callbacks[:before] << [conditions, block]
end
current(thread: Thread.current, application: Application.instance) click to toggle source
# File lib/praxis/dispatcher.rb, line 29
def self.current(thread: Thread.current, application: Application.instance)
  thread[:praxis_dispatcher] ||= new(application: application)
end
new(application: Application.instance) click to toggle source
# File lib/praxis/dispatcher.rb, line 33
def initialize(application: Application.instance)
  @stages = []
  @application = application
  setup_stages!
end

Public Instance Methods

after(*stage_path, &block) click to toggle source
# File lib/praxis/dispatcher.rb, line 65
def after(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.after(*stage_path, &block)
end
before(*stage_path, &block) click to toggle source
# File lib/praxis/dispatcher.rb, line 60
def before(*stage_path, &block)
  stage_name = stage_path.shift
  stages.find { |stage| stage.name == stage_name }.before(*stage_path, &block)
end
dispatch(controller_class, action, request) click to toggle source
# File lib/praxis/dispatcher.rb, line 70
def dispatch(controller_class, action, request)
  @controller = controller_class.new(request)
  request.action = action
  @action = action
  @request = request

  payload = { request: request, response: nil, controller: @controller }

  instrumented_dispatch(payload)
ensure
  @controller = nil
  @action = nil
  @request = nil
end
instrumented_dispatch(payload) click to toggle source
# File lib/praxis/dispatcher.rb, line 85
def instrumented_dispatch(payload)
  Notifications.instrument 'praxis.request.all', payload do
    # the response stage must be the final stage in the list
    *stages, response_stage = @stages

    stages.each do |stage|
      result = stage.run
      case result
      when Response
        controller.response = result
        break
      end
    end

    response_stage.run

    payload[:response] = controller.response
    controller.response.finish
  rescue StandardError => e
    @application.error_handler.handle!(request, e)
  end
end
reset_cache!() click to toggle source

TODO: fix for multithreaded environments

# File lib/praxis/dispatcher.rb, line 109
def reset_cache!
  return unless Praxis::Blueprint.caching_enabled?

  Praxis::Blueprint.cache = Hash.new do |hash, key|
    hash[key] = {}
  end
end
setup_deferred_callbacks!() click to toggle source
# File lib/praxis/dispatcher.rb, line 48
def setup_deferred_callbacks!
  self.class.deferred_callbacks.each do |stage_name, callbacks|
    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
setup_stages!() click to toggle source
# File lib/praxis/dispatcher.rb, line 39
def setup_stages!
  @stages << RequestStages::LoadRequest.new(:load_request, self)
  @stages << RequestStages::Validate.new(:validate, self)
  @stages << RequestStages::Action.new(:action, self)
  @stages << RequestStages::Response.new(:response, self)
  @stages.each(&:setup!)
  setup_deferred_callbacks!
end