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

Returns a new instance of Router.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
# File 'lib/corrupt/router.rb', line 6

def initialize(&block)
  @@routes = []
  yield self if block_given?
end

Class Method Details

.dispatch(path) ⇒ Object

Dispatch a request to a controller and action.



24
25
26
27
28
29
30
31
32
33
# File 'lib/corrupt/router.rb', line 24

def self.dispatch(path)
  route = @@routes.select { |route| route[0] == path }.flatten
  # FIXME: This could probably be handled a little better.
  if route.empty?
    Exceptions.new.four_oh_four
  else
    response = route[1]  # 2nd element is the controller/action hash.
    Corrupt::Controller.const_get(response[:controller]).new.send(response[:action])
  end
end

.routesObject

Return the configured routes.



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

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


14
15
16
# File 'lib/corrupt/router.rb', line 14

def map(path, options)
  @@routes << [path, options]
end