Class: Jets::Router::RouteSet
- Inherits:
-
Object
- Object
- Jets::Router::RouteSet
- Extended by:
- Memoist
- Includes:
- Compat::RouteSet, Dsl
- Defined in:
- lib/jets/router/route_set.rb
Defined Under Namespace
Classes: NamedRouteCollection
Constant Summary
Constants included from Dsl
Constants included from Compat::RouteSet
Compat::RouteSet::RESERVED_OPTIONS, Compat::RouteSet::UNKNOWN
Instance Attribute Summary collapse
-
#default_scope ⇒ Object
Returns the value of attribute default_scope.
-
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
-
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
-
#draw_paths ⇒ Object
Returns the value of attribute draw_paths.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#all_paths ⇒ Object
Useful for creating API Gateway Resources.
- #api_mode? ⇒ Boolean
- #append(&block) ⇒ Object
-
#check_collision! ⇒ Object
Validate routes that deployable.
-
#clear! ⇒ Object
Be selective about what to clear.
- #define_mounted_helper(name) ⇒ Object
-
#draw(&block) ⇒ Object
draw is called from config/routes.rb load! is what we just internally to trigger it.
-
#eager_load! ⇒ Object
Rails interface method.
-
#finalize! ⇒ Object
Interface method for plugins.
-
#initialize(engine = Jets.application) ⇒ RouteSet
constructor
A new instance of RouteSet.
-
#load_external_paths ⇒ Object
additonal paths: config/routes/admin.rb.
- #mounted_helpers ⇒ Object
-
#named_routes ⇒ Object
Note: Jets assigns this to a instance varialbe in the initialize method But Jets cannot do this because we currently eager load internally and that routes draw would not be called in proper order.
- #one_apigw_method_for_all_routes_warning(options) ⇒ Object
-
#ordered_routes ⇒ Object
Useful for RouterMatcher.
- #prepend(&block) ⇒ Object
- #routes_call_line ⇒ Object
- #url_helpers(supports_path = true) ⇒ Object
Methods included from Dsl
#collection, #constraints, #create_route, #defaults, #each_resource, #match, #member, #namespace, #normalize_path_to_controller_map_option!, #path, #resource, #resources, #root, #scope, #shallow
Methods included from Dsl::Mount
#mount, #mount_class_at, #mount_engine
Methods included from Compat::RouteSet
#polymorphic_mappings, #url_for
Constructor Details
#initialize(engine = Jets.application) ⇒ RouteSet
Returns a new instance of RouteSet.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jets/router/route_set.rb', line 54 def initialize(engine=Jets.application) @engine = engine @engine_class = engine.class @default_url_options = {} # {host: "localhost"} @routes = [] @draw_paths = [] # not part of clear! addtional paths: config/routes/admin.rb @prepend = [] # not part of clear! Otherwise breaks engines like sprockets-jets @append = [] # not part of clear! @finalized = false @disable_clear_and_finalize = false @default_scope = {} end |
Instance Attribute Details
#default_scope ⇒ Object
Returns the value of attribute default_scope.
52 53 54 |
# File 'lib/jets/router/route_set.rb', line 52 def default_scope @default_scope end |
#default_url_options ⇒ Object
Returns the value of attribute default_url_options.
52 53 54 |
# File 'lib/jets/router/route_set.rb', line 52 def @default_url_options end |
#disable_clear_and_finalize ⇒ Object
Returns the value of attribute disable_clear_and_finalize.
52 53 54 |
# File 'lib/jets/router/route_set.rb', line 52 def disable_clear_and_finalize @disable_clear_and_finalize end |
#draw_paths ⇒ Object
Returns the value of attribute draw_paths.
52 53 54 |
# File 'lib/jets/router/route_set.rb', line 52 def draw_paths @draw_paths end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
51 52 53 |
# File 'lib/jets/router/route_set.rb', line 51 def engine @engine end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
51 52 53 |
# File 'lib/jets/router/route_set.rb', line 51 def routes @routes end |
Instance Method Details
#all_paths ⇒ Object
Useful for creating API Gateway Resources
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/jets/router/route_set.rb', line 181 def all_paths results = [] paths = routes.map(&:path) paths.each do |p| sub_paths = [] parts = p.split('/') until parts.empty? parts.pop sub_path = parts.join('/') sub_paths << sub_path unless sub_path == '' end results += sub_paths end @all_paths = (results + paths).sort.uniq end |
#api_mode? ⇒ Boolean
176 177 178 |
# File 'lib/jets/router/route_set.rb', line 176 def api_mode? Jets.config.mode == 'api' || Jets.config.api_only end |
#append(&block) ⇒ Object
108 109 110 |
# File 'lib/jets/router/route_set.rb', line 108 def append(&block) @append << block end |
#check_collision! ⇒ Object
Validate routes that deployable
145 146 147 148 149 150 151 |
# File 'lib/jets/router/route_set.rb', line 145 def check_collision! return if Jets.config.cfn.build.routes == "one_apigw_method_for_all_routes" paths = self.routes.map(&:path) collision = Jets::Cfn::Resource::ApiGateway::RestApi::Routes::Collision.new collide = collision.collision?(paths) raise collision.exception if collide end |
#clear! ⇒ Object
Be selective about what to clear. Keep other initialized instance variables like @prepend, @append, @default_scope Otherwise can break engines like sprockets-jets that use prepend.
81 82 83 84 85 86 87 |
# File 'lib/jets/router/route_set.rb', line 81 def clear! @finalized = false @routes = [] @scope = Scope.new named_routes.remove_methods! @prepend.each { |blk| instance_eval(&blk) } end |
#define_mounted_helper(name) ⇒ Object
132 133 134 |
# File 'lib/jets/router/route_set.rb', line 132 def define_mounted_helper(name) Helpers::NamedRoutes::Generated.define_mounted_helper(name, self) end |
#draw(&block) ⇒ Object
draw is called from config/routes.rb load! is what we just internally to trigger it
69 70 71 72 73 74 75 76 |
# File 'lib/jets/router/route_set.rb', line 69 def draw(&block) clear! unless @disable_clear_and_finalize scope(default_scope) do instance_eval(&block) end finalize! unless @disable_clear_and_finalize check_collision! end |
#eager_load! ⇒ Object
Rails interface method
117 118 119 |
# File 'lib/jets/router/route_set.rb', line 117 def eager_load! # noop for Jets end |
#finalize! ⇒ Object
Interface method for plugins. Plugins like kingsman use to hook into Jets after routes are loaded.
91 92 93 94 95 96 97 |
# File 'lib/jets/router/route_set.rb', line 91 def finalize! return if @finalized @append.each { |blk| instance_eval(&blk) } load_external_paths named_routes.add_methods! @finalized = true end |
#load_external_paths ⇒ Object
additonal paths: config/routes/admin.rb
100 101 102 103 104 105 106 |
# File 'lib/jets/router/route_set.rb', line 100 def load_external_paths @draw_paths.each do |draw_path| Dir.glob("#{draw_path}/*.rb").each do |route_path| instance_eval(File.read(route_path), route_path.to_s) end end end |
#mounted_helpers ⇒ Object
125 126 127 128 129 130 |
# File 'lib/jets/router/route_set.rb', line 125 def mounted_helpers # Calling url_helpers to create the mounted_helpers proxy methods # IE: main_app and blorgh url_helpers @mounted_helpers ||= Helpers::NamedRoutes::Generated::MountedHelpers end |
#named_routes ⇒ Object
Note: Jets assigns this to a instance varialbe in the initialize method But Jets cannot do this because we currently eager load internally and that routes draw would not be called in proper order.
139 140 141 |
# File 'lib/jets/router/route_set.rb', line 139 def named_routes NamedRouteCollection.new(self) end |
#one_apigw_method_for_all_routes_warning(options) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jets/router/route_set.rb', line 153 def one_apigw_method_for_all_routes_warning() return unless [:authorizer] return unless Jets.config.cfn.build.routes == "one_apigw_method_for_all_routes" return if [:path].starts_with?('*') # *catchall return if [:path] == '' # root puts <<~EOL.color(:yellow) WARNING: Authorizer should not be set at individual routes when using config.cfn.build.routes == "one_apigw_method_for_all_routes" You should only set authorizer for the root route and *catchall route. The root route uses the root authorizer. And all other routes uses the *catchall authorizer. Docs: http://rubyonjets.com/docs/routing/authorizers/one-method/ EOL DslEvaluator.print_code(routes_call_line) if routes_call_line end |
#ordered_routes ⇒ Object
Useful for RouterMatcher
Precedence: Routes with wildcards are considered after routes without wildcards
Routes with fewer captures are ordered first since both /posts/:post_id/comments/new and /posts/:post_id/comments/:id are equally long
Routes with the same amount of captures and wildcards are orderd so that the longest path is considered first since posts/:id and posts/:id/edit can both match.
209 210 211 212 |
# File 'lib/jets/router/route_set.rb', line 209 def ordered_routes length = Proc.new { |r| [r.path.count("*"), r.path.count(":"), r.path.length * -1] } routes.sort_by(&length) end |
#prepend(&block) ⇒ Object
112 113 114 |
# File 'lib/jets/router/route_set.rb', line 112 def prepend(&block) @prepend << block end |
#routes_call_line ⇒ Object
172 173 174 |
# File 'lib/jets/router/route_set.rb', line 172 def routes_call_line caller.find { |l| l.include?('config/routes.rb') } end |
#url_helpers(supports_path = true) ⇒ Object
121 122 123 |
# File 'lib/jets/router/route_set.rb', line 121 def url_helpers(supports_path = true) @url_helpers ||= named_routes.url_helpers_module end |