Class: Reactive::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dispatch(request) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/dispatcher.rb', line 6

def dispatch(request)
  if request == :init
    new.initial_dispatch
  else
    new.dispatch(request)
  end
end

Instance Method Details

#dispatch(request) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/dispatcher.rb', line 15

def dispatch(request)
  controller = recognize(request.params)
  response = Response.new
  controller.process(request, response)

  handle_response(response)
end

#handle_response(response) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dispatcher.rb', line 27

def handle_response(response)
  if response.redirected_to
    # This recursive call is intended, do not refactor in a loop. This way when a redirect cycle occurs, it will end up in a stack overflow
    dispatch(Request.new(response.redirected_to))
  else
    response.result        # This does nothing, Should we treat the result in some ways? We already have Exceptions for handling errors, so... what should the result be?
  end
end

#initial_dispatchObject



36
37
38
39
40
41
42
43
# File 'lib/dispatcher.rb', line 36

def initial_dispatch
  controller_class = ApplicationController
  # controller_class.class_eval { helper :application }             Not need, already done in initializer.rb
  response = Response.new
  controller_class.process(Request.new, response, :perform_init)

  handle_response(response)
end

#recognize(params) ⇒ Object



23
24
25
# File 'lib/dispatcher.rb', line 23

def recognize(params)
  "#{params[:controller].camelize}Controller".constantize
end