Module: Kawaii::RoutingMethods
- Included in:
- Base, RouteContext
- Defined in:
- lib/kawaii/routing_methods.rb
Overview
Core route-building and matching.
These functions can be used both in a class inheriting from Base and in file scope.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) context(path, &block)
Create a context for route nesting.
-
- (Object) delete(path, &block) { ... }
Route handler for delete HTTP method.
-
- (Object) get(path, &block) { ... }
Route handler for get HTTP method.
-
- (Object) head(path, &block) { ... }
Route handler for head HTTP method.
-
- (Object) link(path, &block) { ... }
Route handler for link HTTP method.
-
- (Route) match(env)
Tries to match against a Rack environment.
-
- (Array<String>) methods_used
Returns a list of HTTP methods used by the routes (incl. nested routes).
-
- (Object) options(path, &block) { ... }
Route handler for options HTTP method.
-
- (Object) patch(path, &block) { ... }
Route handler for patch HTTP method.
-
- (Object) post(path, &block) { ... }
Route handler for post HTTP method.
-
- (Object) put(path, &block) { ... }
Route handler for put HTTP method.
-
- (Object) route(path, controller)
Insert routes corresponding to REST actions (similar to Rails `resource`).
-
- (Object) trace(path, &block) { ... }
Route handler for trace HTTP method.
-
- (Object) unlink(path, &block) { ... }
Route handler for unlink HTTP method.
Class Method Details
+ (Object) add_http_method(meth)
Supported HTTP verbs based on github.com/rack/rack/blob/master/lib/rack.rb#L48
27 28 29 30 31 32 |
# File 'lib/kawaii/routing_methods.rb', line 27 def self.add_http_method(meth) define_method(meth) do |path, mapping = nil, &block| handler = RouteMapping.new(mapping, &block).resolve add_route!(meth.to_s.upcase, Route.new(self, path, &handler)) end end |
Instance Method Details
- (Object) context(path, &block)
Create a context for route nesting.
@param path [String, Regexp, Matcher] any path specification which can
be consumed by {Matcher.compile}
@param block the route handler
@yield to the given block
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kawaii/routing_methods.rb', line 80 def context(path, &block) ctx = RouteContext.new(self, path) # @todo Is there a better way to keep ordering of routes? # An alternative would be to enter each route in a context only once # (with 'prefix' based on containing contexts). # On the other hand, we're only doing that when compiling routes, further # processing is faster this way. ctx.instance_eval(&block) ctx.methods_used.each do |meth| add_route!(meth, ctx) end end |
- (Object) delete(path, &block) { ... }
Route handler for delete HTTP method
38 |
# File 'lib/kawaii/routing_methods.rb', line 38 add_http_method :delete |
- (Object) get(path, &block) { ... }
Route handler for get HTTP method
34 |
# File 'lib/kawaii/routing_methods.rb', line 34 add_http_method :get |
- (Object) head(path, &block) { ... }
Route handler for head HTTP method
39 |
# File 'lib/kawaii/routing_methods.rb', line 39 add_http_method :head |
- (Object) link(path, &block) { ... }
Route handler for link HTTP method
41 |
# File 'lib/kawaii/routing_methods.rb', line 41 add_http_method :link |
- (Route) match(env)
Tries to match against a Rack environment.
96 97 98 99 100 101 |
# File 'lib/kawaii/routing_methods.rb', line 96 def match(env) routes[env[Rack::REQUEST_METHOD]] .lazy # Lazy to avoid unnecessary calls to #match. .map { |r| r.match(env) } .find { |r| !r.nil? } end |
- (Array<String>) methods_used
Returns a list of HTTP methods used by the routes (incl. nested routes).
105 106 107 |
# File 'lib/kawaii/routing_methods.rb', line 105 def methods_used routes.keys end |
- (Object) options(path, &block) { ... }
Route handler for options HTTP method
40 |
# File 'lib/kawaii/routing_methods.rb', line 40 add_http_method :options |
- (Object) patch(path, &block) { ... }
Route handler for patch HTTP method
37 |
# File 'lib/kawaii/routing_methods.rb', line 37 add_http_method :patch |
- (Object) post(path, &block) { ... }
Route handler for post HTTP method
35 |
# File 'lib/kawaii/routing_methods.rb', line 35 add_http_method :post |
- (Object) put(path, &block) { ... }
Route handler for put HTTP method
36 |
# File 'lib/kawaii/routing_methods.rb', line 36 add_http_method :put |
- (Object) route(path, controller)
Insert routes corresponding to REST actions (similar to Rails `resource`).
60 61 62 63 64 65 66 |
# File 'lib/kawaii/routing_methods.rb', line 60 def route(path, controller) get(File.join(path, '?'), "#{controller}#index") get(File.join(path, '/:id/?'), "#{controller}#show") post(File.join(path, '?'), "#{controller}#create") patch(File.join(path, '/:id/?'), "#{controller}#update") delete(File.join(path, '/:id/?'), "#{controller}#destroy") end |
- (Object) trace(path, &block) { ... }
Route handler for trace HTTP method
43 |
# File 'lib/kawaii/routing_methods.rb', line 43 add_http_method :trace |
- (Object) unlink(path, &block) { ... }
Route handler for unlink HTTP method
42 |
# File 'lib/kawaii/routing_methods.rb', line 42 add_http_method :unlink |