Class: Tennpipes::PathRouter::Router
- Inherits:
-
Object
- Object
- Tennpipes::PathRouter::Router
- Defined in:
- lib/tennpipes-base/path_router.rb
Instance Attribute Summary collapse
-
#current_order ⇒ Object
readonly
Returns the value of attribute current_order.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add(verb, path, options = {}, &block) ⇒ Object
Adds a new route to routes.
-
#call(request, &block) ⇒ Object
Returns all routes which are matched with the condition.
-
#increment_order ⇒ Object
Increments the order.
-
#initialize ⇒ Router
constructor
Constructs an instance of PathRouter::Router.
-
#path(name, *args) ⇒ Object
Finds a path which is matched with conditions from arguments.
-
#prepare! ⇒ Object
Constructs an instance of PathRouter::Compiler, and sorts all routes by using the order.
-
#recognize(request_or_env) ⇒ Object
Returns all routes which are matched with the condition without block.
-
#recognize_path(path_info) ⇒ Object
Recognizes route and expanded params from a path.
-
#reset! ⇒ Object
Resets all routes, current order and preparation.
Constructor Details
#initialize ⇒ Router
Constructs an instance of PathRouter::Router.
24 25 26 |
# File 'lib/tennpipes-base/path_router.rb', line 24 def initialize reset! end |
Instance Attribute Details
#current_order ⇒ Object (readonly)
Returns the value of attribute current_order.
19 20 21 |
# File 'lib/tennpipes-base/path_router.rb', line 19 def current_order @current_order end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
19 20 21 |
# File 'lib/tennpipes-base/path_router.rb', line 19 def engine @engine end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
19 20 21 |
# File 'lib/tennpipes-base/path_router.rb', line 19 def routes @routes end |
Instance Method Details
#add(verb, path, options = {}, &block) ⇒ Object
Adds a new route to routes.
31 32 33 34 35 36 |
# File 'lib/tennpipes-base/path_router.rb', line 31 def add(verb, path, = {}, &block) route = Route.new(path, verb, , &block) route.router = self @routes << route route end |
#call(request, &block) ⇒ Object
Returns all routes which are matched with the condition
41 42 43 44 |
# File 'lib/tennpipes-base/path_router.rb', line 41 def call(request, &block) prepare! unless prepared? @engine.call_by_request(request, &block) end |
#increment_order ⇒ Object
Increments the order.
94 95 96 |
# File 'lib/tennpipes-base/path_router.rb', line 94 def increment_order @current_order += 1 end |
#path(name, *args) ⇒ Object
Finds a path which is matched with conditions from arguments
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/tennpipes-base/path_router.rb', line 57 def path(name, *args) params = args. @routes.each do |route| next unless route.name == name matcher = route.matcher = params.dup if !args.empty? && matcher.mustermann? matcher.names.each_with_index do |matcher_name, index| [matcher_name.to_sym] ||= args[index] end end return matcher.mustermann? ? matcher.() : route.path_for_generation end fail InvalidRouteException end |
#prepare! ⇒ Object
Constructs an instance of PathRouter::Compiler, and sorts all routes by using the order.
102 103 104 105 106 107 |
# File 'lib/tennpipes-base/path_router.rb', line 102 def prepare! @engine = Compiler.new(@routes) @prepared = true return if @current_order.zero? @routes.sort_by!(&:order) end |
#recognize(request_or_env) ⇒ Object
Returns all routes which are matched with the condition without block
49 50 51 52 |
# File 'lib/tennpipes-base/path_router.rb', line 49 def recognize(request_or_env) prepare! unless prepared? @engine.find_by(request_or_env) end |
#recognize_path(path_info) ⇒ Object
Recognizes route and expanded params from a path.
76 77 78 79 80 |
# File 'lib/tennpipes-base/path_router.rb', line 76 def recognize_path(path_info) prepare! unless prepared? route = @engine.find_by_pattern(path_info).first [route.name, route.params_for(path_info, {})] end |
#reset! ⇒ Object
Resets all routes, current order and preparation.
85 86 87 88 89 |
# File 'lib/tennpipes-base/path_router.rb', line 85 def reset! @routes = [] @current_order = 0 @prepared = nil end |