Class: Router

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.

[View source]

29
30
31
# File 'lib/router.rb', line 29

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.


27
28
29
# File 'lib/router.rb', line 27

def routes
  @routes
end

Instance Method Details

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

[View source]

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

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

#draw(&proc) ⇒ Object

[View source]

37
38
39
# File 'lib/router.rb', line 37

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

#match(req) ⇒ Object

[View source]

48
49
50
51
52
53
# File 'lib/router.rb', line 48

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

#run(req, res) ⇒ Object

[View source]

55
56
57
58
59
# File 'lib/router.rb', line 55

def run(req, res)
  route = match(req)
  return res.status = 404 unless route
  route.run(req,res)
end