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 27
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 23
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 31
def self.current(thread: Thread.current, application: Application.instance)
  thread[:praxis_dispatcher] ||= self.new(application: application)
end
new(application: Application.instance) click to toggle source
# File lib/praxis/dispatcher.rb, line 35
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 69
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 64
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 74
def dispatch(controller_class, action, request)
  @controller = controller_class.new(request)
  @action = action
  @request = request

  payload = {request: request, response: nil}

  Notifications.instrument 'praxis.request.all'.freeze, payload do
    begin
      # 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 => e
      @application.error_handler.handle!(request, e)
    end
  end
ensure
  @controller = nil
  @action = nil
  @request = nil
end
reset_cache!() click to toggle source

TODO: fix for multithreaded environments

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

  Praxis::Blueprint.cache = Hash.new do |hash, key|
    hash[key] = Hash.new
  end
end
setup_deferred_callbacks!() click to toggle source
# File lib/praxis/dispatcher.rb, line 52
def setup_deferred_callbacks!
  self.class.deferred_callbacks.each do |stage_name, callbacks|
    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
setup_stages!() click to toggle source
# File lib/praxis/dispatcher.rb, line 41
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 do |s|
    s.setup!
  end
  setup_deferred_callbacks!
end