Class: Corrupt::Router

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

Overview

Handles URL dispatching for the application. Routes are stored in a class variable, accessible by Router.routes.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Router

The Router is normally used like this:

Corrupt::Router.new do |route|
  route.map '/path', :controller => 'Kickass', :action => 'take_names'
end

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
# File 'lib/corrupt/router.rb', line 10

def initialize(&block)
  @@routes = {}
  yield self if block_given?
end

Class Method Details

.dispatch(path) ⇒ Object

Dispatch an incoming request path to a controller and action.



28
29
30
31
32
33
34
35
# File 'lib/corrupt/router.rb', line 28

def self.dispatch(path)
  response = routes[path]
  if response
    Corrupt::Controller.const_get(response[:controller]).new.send(response[:action])
  else
    Exceptions.new.four_oh_four
  end
end

.routesObject

Return the configured routes.



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

def self.routes
  @@routes
end

Instance Method Details

#map(path, options = {}) ⇒ Object

Maps incoming URLs to a controller and action. TODO: Maybe change the routes storage to a hash like:

@@routes[path]  # => options


18
19
20
# File 'lib/corrupt/router.rb', line 18

def map(path, options = {})
  @@routes[path] = options
end