Class: Racket::Utils::Routing::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/utils/routing.rb

Overview

Class responsible for dispatching requests to controllers.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, target_info) ⇒ Dispatcher

Constructor

Parameters:

  • env (Hash)
  • target_info (Array)


103
104
105
106
# File 'lib/racket/utils/routing.rb', line 103

def initialize(env, target_info)
  @env = env
  @controller_class, @params, @action = target_info
end

Class Method Details

.extract_target(response) ⇒ Array

Extracts the target class, target params and target action from a list of valid routes.

Parameters:

  • response (HttpRouter::Response)

Returns:

  • (Array)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/racket/utils/routing.rb', line 87

def self.extract_target(response)
  target_klass = response.route.dest
  params = response.param_values.first.reject(&:empty?)
  action =
    if params.empty?
      target_klass.settings.fetch(:default_action)
    else
      params.shift.to_sym
    end
  [target_klass, params, action]
end

Instance Method Details

#dispatchArray

Dispatches request to a controller. This is the default action whenever a matching route for a request is found.

Returns:

  • (Array)

    A racket response triplet



112
113
114
115
116
117
118
# File 'lib/racket/utils/routing.rb', line 112

def dispatch
  # Rewrite PATH_INFO to reflect that we split out the parameters
  update_path_info(@params.length)

  # Call controller
  call_controller(Current.init(RouterParams.new(@action, @params, @env)))
end