Module: Jets::Router
- Extended by:
- Router
- Included in:
- Router
- Defined in:
- lib/jets/router/route.rb,
lib/jets/router.rb,
lib/jets/router/dsl.rb,
lib/jets/router/help.rb,
lib/jets/router/util.rb,
lib/jets/router/scope.rb,
lib/jets/router/state.rb,
lib/jets/router/matcher.rb,
lib/jets/router/route_set.rb,
lib/jets/router/engine_mount.rb
Overview
The Jets::Router::RouteSet mimics the Rails interface. Jets works in a similar way to Rails. For example the url_helpers:
Jets.application.routes.url_helpers
=> Jets.application.routes.named_routes.path_helpers_module
=> Jets::Router::Helpers::NamedRoutes::Generated::MainAppHelpers
Jets makes less use of anonymous modules. It uses generated named modules to help debugging. Whereas Rails:
Rails.application.routes.url_helpers => <Module>
Rails.application.routes.named_routes.path_helpers_module => <Module>
Rails.application.routes.named_routes.url_helpers_module => <Module>
Rails generates an anonymous module for url_helpers that wraps and includes the path_helpers_module and url_helpers_module.
Jets tries to exhibit enough of the same as Rails interface to make it compatible with with ActionController and ActionView components.
Defined Under Namespace
Modules: Compat, Dsl, Resources, Util Classes: EngineMount, Error, Help, Matcher, Route, RouteSet, Scope, State
Constant Summary collapse
- NAMED_ROUTES_METHODS =
%w[index new create show edit update destroy].freeze
- @@drawn_route_set =
nil
Instance Method Summary collapse
-
#all_paths ⇒ Object
Returns all paths including subpaths.
- #all_routes_valid? ⇒ Boolean
-
#check_route_connected_functions ⇒ Object
Checks that all routes are validate and have corresponding lambda functions.
- #clear! ⇒ Object
- #draw ⇒ Object
- #drawn_route_set ⇒ Object
- #find_by_definition(definition) ⇒ Object
- #find_route_by_event(event) ⇒ Object
- #has_controller?(name) ⇒ Boolean
- #invalid_routes ⇒ Object
-
#no_routes? ⇒ Boolean
Filters out internal routes.
- #routes ⇒ Object
-
#to_json ⇒ Object
So we can save state in s3 post deploy.
- #validate_routes! ⇒ Object
Instance Method Details
#all_paths ⇒ Object
Returns all paths including subpaths. Example: Input: [“posts/:id/edit”] Output: [“posts”, “posts/:id”, “posts/:id/edit”]
47 48 49 |
# File 'lib/jets/router.rb', line 47 def all_paths drawn_route_set.all_paths end |
#all_routes_valid? ⇒ Boolean
51 52 53 |
# File 'lib/jets/router.rb', line 51 def all_routes_valid? invalid_routes.empty? end |
#check_route_connected_functions ⇒ Object
Checks that all routes are validate and have corresponding lambda functions
65 66 67 68 69 70 71 72 73 |
# File 'lib/jets/router.rb', line 65 def check_route_connected_functions return true if all_routes_valid? puts "Please double check the routes below map to valid controllers:".color(:red) invalid_routes.each do |route| puts " /#{route.path} => #{route.controller_name}##{route.action_name}" end false end |
#clear! ⇒ Object
23 24 25 |
# File 'lib/jets/router.rb', line 23 def clear! @@drawn_route_set = nil end |
#draw ⇒ Object
11 12 13 |
# File 'lib/jets/router.rb', line 11 def draw drawn_route_set end |
#drawn_route_set ⇒ Object
16 17 18 19 20 21 |
# File 'lib/jets/router.rb', line 16 def drawn_route_set return @@drawn_route_set if @@drawn_route_set route_set = Jets.application.routes @@drawn_route_set = route_set end |
#find_by_definition(definition) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/jets/router.rb', line 80 def find_by_definition(definition) routes.find do |route| route.controller_name == definition.class_name && route.action_name == definition.meth.to_s end end |
#find_route_by_event(event) ⇒ Object
75 76 77 78 |
# File 'lib/jets/router.rb', line 75 def find_route_by_event(event) request = Jets::Controller::Request.new(event: event) Jets::Router::Matcher.new.find_by_request(request) end |
#has_controller?(name) ⇒ Boolean
7 8 9 |
# File 'lib/jets/router.rb', line 7 def has_controller?(name) routes.detect { |r| r.controller_name == name } end |
#invalid_routes ⇒ Object
55 56 57 |
# File 'lib/jets/router.rb', line 55 def invalid_routes routes.select { |r| !r.valid? } end |
#no_routes? ⇒ Boolean
Filters out internal routes
88 89 90 |
# File 'lib/jets/router.rb', line 88 def no_routes? routes.reject(&:internal?).empty? end |
#routes ⇒ Object
27 28 29 |
# File 'lib/jets/router.rb', line 27 def routes drawn_route_set.routes end |
#to_json ⇒ Object
So we can save state in s3 post deploy. Example of structure.
[
{"scope"=>{"options"=>{"as"=>"posts", "path"=>"posts", "param"=>nil, "from"=>"resources"}, "parent"=>{"options"=>{}, "parent"=>nil, "level"=>1}, "level"=>2}, "options"=>{"to"=>"posts#index", "path"=>"posts", "method"=>"get"}, "path"=>"posts", "to"=>"posts#index", "as"=>"posts"},
{"scope"=>{"options"=>{"as"=>"posts", "path"=>"posts", "param"=>nil, "from"=>"resources"}, "parent"=>{"options"=>{}, "parent"=>nil, "level"=>1}, "level"=>2}, "options"=>{"to"=>"posts#new", "path"=>"posts/new", "method"=>"get"}, "path"=>"posts/new", "to"=>"posts#new", "as"=>"new_post"},
...
]
39 40 41 |
# File 'lib/jets/router.rb', line 39 def to_json JSON.dump(routes.map(&:to_h)) end |
#validate_routes! ⇒ Object
59 60 61 62 |
# File 'lib/jets/router.rb', line 59 def validate_routes! return true if Jets.config.cfn.build.routes == "one_apigw_method_for_all_routes" check_route_connected_functions end |