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.



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

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

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



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

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

#draw(&proc) ⇒ Object



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

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

#match(req) ⇒ Object



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



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

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