Class: Reloj::Router
- Inherits:
-
Object
- Object
- Reloj::Router
- Defined in:
- lib/reloj/core/router.rb
Instance Attribute Summary collapse
-
#helpers ⇒ Object
readonly
Returns the value of attribute helpers.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Class Method Summary collapse
Instance Method Summary collapse
- #add_helper_attributes(pattern, action_name) ⇒ Object
-
#add_route(pattern, method, controller_class, action_name) ⇒ Object
simply adds a new route to the list of routes.
- #draw(&proc) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #match(req) ⇒ Object
- #run(req, res) ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
45 46 47 48 |
# File 'lib/reloj/core/router.rb', line 45 def initialize @routes = [] @paths = [] end |
Instance Attribute Details
#helpers ⇒ Object (readonly)
Returns the value of attribute helpers.
35 36 37 |
# File 'lib/reloj/core/router.rb', line 35 def helpers @helpers end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
35 36 37 |
# File 'lib/reloj/core/router.rb', line 35 def routes @routes end |
Class Method Details
.regex_from_pattern_string(pattern_string) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/reloj/core/router.rb', line 37 def self.regex_from_pattern_string(pattern_string) x = pattern_string.split('/').map do |part| part.gsub(/:(.+)[\/]{,1}/, '(?<\1>\d+)') end.join('/') Regexp.new("^#{x}\/?$") end |
Instance Method Details
#add_helper_attributes(pattern, action_name) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/reloj/core/router.rb', line 50 def add_helper_attributes(pattern, action_name) #need controller name # will get that when passed to controller #need action @paths << pattern end |
#add_route(pattern, method, controller_class, action_name) ⇒ Object
simply adds a new route to the list of routes
58 59 60 61 62 63 |
# File 'lib/reloj/core/router.rb', line 58 def add_route(pattern, method, controller_class, action_name) parsed_pattern = Router.regex_from_pattern_string(pattern.to_s) route = Route.new(parsed_pattern, method, controller_class, action_name) @routes << route add_helper_attributes(pattern, action_name) end |
#draw(&proc) ⇒ Object
65 66 67 |
# File 'lib/reloj/core/router.rb', line 65 def draw(&proc) instance_eval(&proc) end |
#match(req) ⇒ Object
75 76 77 78 |
# File 'lib/reloj/core/router.rb', line 75 def match(req) @routes.each { |route| return route if route.matches?(req) } nil end |
#run(req, res) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/reloj/core/router.rb', line 80 def run(req, res) if match(req) #pass in the helper_attributes here match(req).run(req, res, @paths) else res.status = 404 end end |