Class: Bezel::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



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

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

#run(req, res) ⇒ Object



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

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