Class: Howl::Router
- Inherits:
-
Object
- Object
- Howl::Router
- Defined in:
- lib/howl-router/router.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_order ⇒ Object
readonly
Returns the value of attribute current_order.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #compile ⇒ Object
- #increment_order ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #recognize(request) ⇒ Object
- #reset! ⇒ Object
Constructor Details
#initialize ⇒ Router
Returns a new instance of Router.
5 6 7 |
# File 'lib/howl-router/router.rb', line 5 def initialize reset! end |
Instance Attribute Details
#current_order ⇒ Object (readonly)
Returns the value of attribute current_order.
3 4 5 |
# File 'lib/howl-router/router.rb', line 3 def current_order @current_order end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
3 4 5 |
# File 'lib/howl-router/router.rb', line 3 def routes @routes end |
Instance Method Details
#compile ⇒ Object
37 38 39 40 |
# File 'lib/howl-router/router.rb', line 37 def compile return if @current_order.zero? @routes.sort!{|a, b| a.order <=> b.order } end |
#increment_order ⇒ Object
33 34 35 |
# File 'lib/howl-router/router.rb', line 33 def increment_order @current_order += 1 end |
#recognize(request) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/howl-router/router.rb', line 9 def recognize(request) path_info, verb, request_params = parse_request(request) ignore_slash_path_info = path_info ignore_slash_path_info = path_info[0..-2] if path_info != "/" and path_info[-1] == "/" matched_routes = scan_routes(path_info, ignore_slash_path_info) raise NotFound if matched_routes.empty? raise_method_not_allowed(request, matched_routes) unless matched_routes.find{|r|r.verb == verb} result = matched_routes.map do |route| next unless verb == route.verb params, matcher = {}, route.matcher match_data = matcher.match(matcher.mustermann? ? ignore_slash_path_info : path_info) if match_data.names.empty? params[:captures] = match_data.captures else params.merge!(match_data.names.inject({}){|result, name| result[name.to_sym] = match_data[name] ? Rack::Utils.unescape(match_data[name]) : nil result }).merge!(request_params){|key, self_value, new_value| self_value || new_value } end [route, params] end.compact result.empty? ? raise_method_not_allowed(request, matched_routes) : result end |
#reset! ⇒ Object
42 43 44 45 |
# File 'lib/howl-router/router.rb', line 42 def reset! @routes = [] @current_order = 0 end |