Class: Waves::Dispatchers::Default

Inherits:
Base show all
Defined in:
lib/dispatchers/default.rb

Overview

Waves::Dispatchers::Default matches requests against an application’s mappings to determine what main action to take, as well as what before, after, always, and exception-handling blocks must run.

The default dispatcher also attempts to set the content type based on the file extension used in the request URL. It does this using the class defined in the current configuration’s mime_types attribute, which defaults to Mongrel’s MIME type handler.

You can write your own dispatcher and use it in your application’s configuration file. By default, they use the default dispatcher, like this:

application do
  use Rack::ShowExceptions
  run Waves::Dispatchers::Default.new
end

Instance Method Summary collapse

Methods inherited from Base

#call, #deferred?

Instance Method Details

#safe(request) ⇒ Object

All dispatchers using the Dispatchers::Base to provide thread-safety, logging, etc. must provide a safe method to handle creating a response from a request. Takes a Waves::Request and returns a Waves::Reponse



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dispatchers/default.rb', line 29

def safe( request  )

  response = request.response

  Waves::Application.instance.reload if Waves::Application.instance.debug?
  response.content_type = Waves::Application.instance.config.mime_types[ request.path ] || 'text/html'

  mapping = Waves::Application.instance.mapping[ request ]

  begin

    mapping[:before].each do | block, args |
      ResponseProxy.new(request).instance_exec(*args,&block)
    end

    request.not_found unless mapping[:action]

    block, args = mapping[:action]
    response.write( ResponseProxy.new(request).instance_exec(*args, &block) )
    
    mapping[:after].each do | block, args |
      ResponseProxy.new(request).instance_exec(*args,&block)
    end

  rescue Exception => e
    
    handler = mapping[:handlers].detect do | exception, block, args |
      e.is_a? exception
    end
    if handler
      ResponseProxy.new(request).instance_exec(*handler[2], &handler[1])
    else
      raise e
    end
    
  ensure
    mapping[:always].each do | block, args |
      begin
        ResponseProxy.new(request).instance_exec(*args,&block) 
      rescue Exception => e
        Waves::Logger.info e.to_s
      end
    end
    
  end

end