Class: TGauge::Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



34
35
36
# File 'lib/db/router.rb', line 34

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



32
33
34
# File 'lib/db/router.rb', line 32

def routes
  @routes
end

Instance Method Details

#add_route(pattern, method, controller_class, action_name) ⇒ Object

simply adds a new route to the list of routes



39
40
41
# File 'lib/db/router.rb', line 39

def add_route(pattern, method, controller_class, action_name)
  @routes << Route.new(pattern, method, controller_class, action_name)
end

#draw(&proc) ⇒ Object

evaluate the proc in the context of the instance for syntactic sugar :)



45
46
47
# File 'lib/db/router.rb', line 45

def draw(&proc)
  self.instance_eval(&proc)
end

#match(req) ⇒ Object

should return the route that matches this request



58
59
60
61
62
63
# File 'lib/db/router.rb', line 58

def match(req)
  @routes.each do |route|
    return route if route.matches?(req)
  end
  nil
end

#run(req, res) ⇒ Object

either throw 404 or call run on a matched route



66
67
68
69
70
71
72
73
74
# File 'lib/db/router.rb', line 66

def run(req, res)
  route = match(req)
  if route
    route.run(req, res)
  else
    res.write("Route #{req.path} could not be found.")
    res.status = 404
  end
end