Class: Reactive::Dispatcher::Base

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

Constant Summary collapse

@@dispatchers =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#requestObject

Returns the value of attribute request.



14
15
16
# File 'lib/reactive-core/dispatcher.rb', line 14

def request
  @request
end

#responseObject

Returns the value of attribute response.



14
15
16
# File 'lib/reactive-core/dispatcher.rb', line 14

def response
  @response
end

Class Method Details

.default_dispatcherObject



33
34
35
# File 'lib/reactive-core/dispatcher.rb', line 33

def default_dispatcher
  @@default_dispatcher.is_a?(Symbol) ? @@dispatchers[@@default_dispatcher] : @@default_dispatcher
end

.dispatch(request) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/reactive-core/dispatcher.rb', line 18

def dispatch(request)
  name, dispatcher = @@dispatchers.find {|name, item| item.candidate?(request)}
  unless dispatcher ||= default_dispatcher
    msg = "No dispatcher found for request #{request.inspect} (did you specify an initial_request in config.rb?)\nYou may choose a default dispatcher in config.rb with: config.default_dispatcher = :the_one"
    logger.error msg
    raise NoAvailableDispatcher, msg
  end
  dispatcher.instance_for(request).dispatch(request)
=begin
  # choose the matching registered dispatcher
  #TODO: the request has to pass in a "candidate" method in each registered dispatcher. The first that replies YES will be used for that
  # request. So a Route mechanism may be setup up and thus the dispatcher chooser may be route oriented.
=end
end

.register(name, klass) ⇒ Object



37
38
39
# File 'lib/reactive-core/dispatcher.rb', line 37

def register(name, klass)
  @@dispatchers[name.to_sym] = klass
end

Instance Method Details

#dispatch(request) ⇒ Object

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/reactive-core/dispatcher.rb', line 47

def dispatch(request)
  raise NotImplementedError
end

#handle_responseObject



51
52
53
54
55
56
57
58
59
# File 'lib/reactive-core/dispatcher.rb', line 51

def handle_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
    self.class.dispatch(Request.new(response.redirected_to))
  else
    # handle the response,
    OutputHandler::Base.process(request, response)
  end
end

#log_processing(caption = nil) ⇒ Object



42
43
44
45
# File 'lib/reactive-core/dispatcher.rb', line 42

def log_processing(caption = nil)
  caption ||= "#{request.params.inspect}"
  logger.info "\n\nDispatching #{caption} at #{Time.now.to_s(:db)}"
end