Class: RapidRunty::Router::Routes
- Defined in:
- lib/rapid_runty/router/routes.rb
Overview
This class registers all routes defined by the draw method.
Instance Method Summary collapse
- #add(*args) ⇒ Object
-
#draw(&block) ⇒ Object
Provides familiar DSL to defining routes.
- #find_matching_route(path, urls, routes) ⇒ Object
-
#find_route(verb, path) ⇒ Object
Match incoming paths from env to existing routes.
- #root(controller) ⇒ Object
- #select_routes_by_verb(verb, routes) ⇒ Object
Instance Method Details
#add(*args) ⇒ Object
10 11 12 |
# File 'lib/rapid_runty/router/routes.rb', line 10 def add(*args) self << RapidRunty::Router::Route.new(*args) end |
#draw(&block) ⇒ Object
Provides familiar DSL to defining routes
Example:
DemoApplication.routes.draw do
root "demo#index"
get "/demo", to: "demo#index"
get "/demo/new", to: "demo#new"
get "/demo/:id", to: "demo#show"
get "/demo/:id/edit", to: "demo#edit"
post "/demo/", to: "demo#create"
put "/demo/:id", to: "demo#update"
patch "/demo/:id", to: "demo#update"
delete "/demo/:id", to: "demo#destroy"
end
65 66 67 |
# File 'lib/rapid_runty/router/routes.rb', line 65 def draw(&block) instance_eval(&block) end |
#find_matching_route(path, urls, routes) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/rapid_runty/router/routes.rb', line 40 def find_matching_route(path, urls, routes) url, placeholders, controller_action = RapidRunty::Router::Matcher.new.match(path, urls) return nil if url.nil? route = routes.detect { |router| router.path == url }.clone route.placeholders = placeholders route. = controller_action route end |
#find_route(verb, path) ⇒ Object
Match incoming paths from env to existing routes
Example:
RapidRunty::Router::Routes.find_route("GET", "/foo/4") => #<RapidRunty::Router::Route options={"controller"=>"foo", "action"=>"bar"}, path="/foo/:id", placeholders=[], verb=:get
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rapid_runty/router/routes.rb', line 24 def find_route(verb, path) return nil if empty? verb = verb.to_s.downcase.strip.to_sym routes = select { |route| route.verb == verb } urls = select_routes_by_verb(verb, routes) find_matching_route(path, urls, routes) end |
#root(controller) ⇒ Object
69 70 71 |
# File 'lib/rapid_runty/router/routes.rb', line 69 def root(controller) get '/', to: controller end |
#select_routes_by_verb(verb, routes) ⇒ Object
35 36 37 38 |
# File 'lib/rapid_runty/router/routes.rb', line 35 def select_routes_by_verb(verb, routes) urls = routes.map { |route| { url: route.path }.merge route. } urls end |