roda 3.47.0 → 3.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cad702e3b962a1e1b64e631ef77ea1ccca4eb1c630f1ac2e73b5c169b505744
4
- data.tar.gz: f6576f0bd8b1a921435121ce9043a04f04f5560eca7a2eb93f5f39500a62dc1b
3
+ metadata.gz: 34691daba87a890b955df8a9c89ac11e3b5b2ce3ac4726b5d4dbd526aaa5c8ce
4
+ data.tar.gz: e2d8399a4a17a432d39e531ef25db62028622bd258194e930ef3d2b55a722bb8
5
5
  SHA512:
6
- metadata.gz: 7def8965cb5ce9b12c96d16f2edb4e4428b41367b91ebfb20132332b87b04fceba29fdb537ea91c25eda7cc72d07ba3d329d12c073746309b125275d8cc616ad
7
- data.tar.gz: 7acab06b22c80a697e4e250b857544aa337fcdf9a1c2065adae61cac4d1d17171ea374300104855e192fc4700d39d5c8fb9ebe7e8ee2ebc5d00fd6776c7a9f64
6
+ metadata.gz: 728e35715a74a04b373b3fa36fe8a7b81298de6d6c159de0ad95e654346e8c55d593d9ad4f067a0d8e1a569434085b333549efcc0467b28158332493b2ab8087
7
+ data.tar.gz: ee4d3b86e0b15dae0126b6ad032cfbe3055e9a1010f3cd0cf2d847cfb12ca4910b61f0030baa3e94fac8ac52304ca60a2b7825f2080cf28d51579dfd3d8cefa6
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ = 3.48.0 (2021-09-13)
2
+
3
+ * Extract named_routes plugin from multi_route plugin (jeremyevans)
4
+
1
5
  = 3.47.0 (2021-08-13)
2
6
 
3
7
  * Automatically optimize remaining r.on calls with a single argument (jeremyevans)
@@ -0,0 +1,10 @@
1
+ = New Features
2
+
3
+ * A named_routes plugin has been added, for defining named route
4
+ blocks that you can dispatch to with r.route. This feature was
5
+ previously available as part of the multi_route plugin, but there
6
+ are cases where the r.route method and support for named routes is
7
+ helpful even when the multi_route plugin is not used (such as when
8
+ the hash_routes plugin is used instead of the multi_route plugin).
9
+ The multi_route plugin now depends on the named_routes plugin, so
10
+ this change should not cause any backwards compatibility issues.
@@ -3,15 +3,10 @@
3
3
  #
4
4
  class Roda
5
5
  module RodaPlugins
6
- # The multi_route plugin allows for multiple named routes, which the
7
- # main route block can dispatch to by name at any point by calling +route+.
8
- # If the named route doesn't handle the request, execution will continue,
9
- # and if the named route does handle the request, the response returned by
10
- # the named route will be returned.
11
- #
12
- # In addition, this plugin adds the +r.multi_route+ method, which will check
13
- # if the first segment in the path matches a named route, and dispatch
14
- # to that named route.
6
+ # The multi_route plugin builds on the named_routes plugin and allows for
7
+ # dispatching to multiple named routes # by calling the +r.multi_route+ method,
8
+ # which will check # if the first segment in the path matches a named route,
9
+ # and dispatch to that named route.
15
10
  #
16
11
  # The hash_routes plugin offers a +r.hash_routes+ method that is similar to
17
12
  # and performs better than the +r.multi_route+ method, and it is recommended
@@ -35,23 +30,14 @@ class Roda
35
30
  #
36
31
  # route do |r|
37
32
  # r.multi_route
38
- #
39
- # # or
40
- #
41
- # r.on "foo" do
42
- # r.route 'foo'
43
- # end
44
- #
45
- # r.on "bar" do
46
- # r.route 'bar'
47
- # end
48
33
  # end
49
34
  #
50
- # Note that in multi-threaded code, you should not attempt to add a
51
- # named route after accepting requests.
35
+ # Note that only named routes with string names will be dispatched to by the
36
+ # +r.multi_route+ method. Named routes with other names can be dispatched to
37
+ # using the named_routes plugin API, but will not be automatically dispatched
38
+ # to by +r.multi_route+.
52
39
  #
53
- # If you want to use the +r.multi_route+ method, use string names for the
54
- # named routes. Also, you can provide a block to +r.multi_route+ that is
40
+ # You can provide a block to +r.multi_route+ that is
55
41
  # called if the route matches but the named route did not handle the
56
42
  # request:
57
43
  #
@@ -62,20 +48,10 @@ class Roda
62
48
  # If a block is not provided to multi_route, the return value of the named
63
49
  # route block will be used.
64
50
  #
65
- # == Routing Files
66
- #
67
- # The convention when using the multi_route plugin is to have a single
68
- # named route per file, and these routing files should be stored in
69
- # a routes subdirectory in your application. So for the above example, you
70
- # would use the following files:
71
- #
72
- # routes/bar.rb
73
- # routes/foo.rb
74
- #
75
51
  # == Namespace Support
76
52
  #
77
53
  # The multi_route plugin also has support for namespaces, allowing you to
78
- # use r.multi_route at multiple levels in your routing tree. Example:
54
+ # use +r.multi_route+ at multiple levels in your routing tree. Example:
79
55
  #
80
56
  # route('foo') do |r|
81
57
  # r.multi_route('foo')
@@ -103,81 +79,38 @@ class Roda
103
79
  #
104
80
  # route do |r|
105
81
  # r.multi_route
106
- #
107
- # # or
108
- #
109
- # r.on "foo" do
110
- # r.on("baz"){r.route("baz", "foo")}
111
- # r.on("quux"){r.route("quux", "foo")}
112
- # end
113
- #
114
- # r.on "bar" do
115
- # r.on("baz"){r.route("baz", "bar")}
116
- # r.on("quux"){r.route("quux", "bar")}
117
- # end
118
82
  # end
119
- #
120
- # === Routing Files
121
- #
122
- # The convention when using namespaces with the multi_route plugin is to
123
- # store the routing files in subdirectories per namespace. So for the
124
- # above example, you would have the following routing files:
125
- #
126
- # routes/bar.rb
127
- # routes/bar/baz.rb
128
- # routes/bar/quux.rb
129
- # routes/foo.rb
130
- # routes/foo/baz.rb
131
- # routes/foo/quux.rb
132
83
  module MultiRoute
84
+ def self.load_dependencies(app)
85
+ app.plugin :named_routes
86
+ end
87
+
133
88
  # Initialize storage for the named routes.
134
89
  def self.configure(app)
135
- app.opts[:namespaced_routes] ||= {}
136
90
  app::RodaRequest.instance_variable_set(:@namespaced_route_regexps, {})
137
91
  end
138
92
 
139
93
  module ClassMethods
140
- # Freeze the namespaced routes and setup the regexp matchers so that there can be no thread safety issues at runtime.
94
+ # Freeze the multi_route regexp matchers so that there can be no thread safety issues at runtime.
141
95
  def freeze
142
- opts[:namespaced_routes].freeze.each do |k,v|
143
- v.freeze
96
+ super
97
+ opts[:namespaced_routes].each_key do |k|
144
98
  self::RodaRequest.named_route_regexp(k)
145
99
  end
146
100
  self::RodaRequest.instance_variable_get(:@namespaced_route_regexps).freeze
147
- super
148
101
  end
149
102
 
150
103
  # Copy the named routes into the subclass when inheriting.
151
104
  def inherited(subclass)
152
105
  super
153
- nsr = subclass.opts[:namespaced_routes]
154
- opts[:namespaced_routes].each{|k, v| nsr[k] = v.dup}
155
106
  subclass::RodaRequest.instance_variable_set(:@namespaced_route_regexps, {})
156
107
  end
157
108
 
158
- # The names for the currently stored named routes
159
- def named_routes(namespace=nil)
160
- unless routes = opts[:namespaced_routes][namespace]
161
- raise RodaError, "unsupported multi_route namespace used: #{namespace.inspect}"
162
- end
163
- routes.keys
164
- end
165
-
166
- # Return the named route with the given name.
167
- def named_route(name, namespace=nil)
168
- opts[:namespaced_routes][namespace][name]
169
- end
170
-
171
- # If the given route has a name, treat it as a named route and
172
- # store the route block. Otherwise, this is the main route, so
173
- # call super.
109
+ # Clear the multi_route regexp matcher for the namespace.
174
110
  def route(name=nil, namespace=nil, &block)
111
+ super
175
112
  if name
176
- routes = opts[:namespaced_routes][namespace] ||= {}
177
- routes[name] = define_roda_method(routes[name] || "multi_route_#{namespace}_#{name}", 1, &convert_route_block(block))
178
113
  self::RodaRequest.clear_named_route_regexp!(namespace)
179
- else
180
- super(&block)
181
114
  end
182
115
  end
183
116
  end
@@ -206,19 +139,14 @@ class Roda
206
139
  # is given, yield to the block.
207
140
  def multi_route(namespace=nil)
208
141
  on self.class.named_route_regexp(namespace) do |section|
209
- r = route(section, namespace)
142
+ res = route(section, namespace)
210
143
  if block_given?
211
144
  yield
212
145
  else
213
- r
146
+ res
214
147
  end
215
148
  end
216
149
  end
217
-
218
- # Dispatch to the named route with the given name.
219
- def route(name, namespace=nil)
220
- scope.send(roda_class.named_route(name, namespace), self)
221
- end
222
150
  end
223
151
  end
224
152
 
@@ -0,0 +1,160 @@
1
+ # frozen-string-literal: true
2
+
3
+ #
4
+ class Roda
5
+ module RodaPlugins
6
+ # The named_routes plugin allows for multiple named routes, which the
7
+ # main route block can dispatch to by name at any point by calling +r.route+.
8
+ # If the named route doesn't handle the request, execution will continue,
9
+ # and if the named route does handle the request, the response returned by
10
+ # the named route will be returned.
11
+ #
12
+ # Example:
13
+ #
14
+ # plugin :multi_route
15
+ #
16
+ # route('foo') do |r|
17
+ # r.is 'bar' do
18
+ # '/foo/bar'
19
+ # end
20
+ # end
21
+ #
22
+ # route('bar') do |r|
23
+ # r.is 'foo' do
24
+ # '/bar/foo'
25
+ # end
26
+ # end
27
+ #
28
+ # route do |r|
29
+ # r.on "foo" do
30
+ # r.route 'foo'
31
+ # end
32
+ #
33
+ # r.on "bar" do
34
+ # r.route 'bar'
35
+ # end
36
+ # end
37
+ #
38
+ # Note that in multi-threaded code, you should not attempt to add a
39
+ # named route after accepting requests.
40
+ #
41
+ # == Routing Files
42
+ #
43
+ # The convention when using the named_routes plugin is to have a single
44
+ # named route per file, and these routing files should be stored in
45
+ # a routes subdirectory in your application. So for the above example, you
46
+ # would use the following files:
47
+ #
48
+ # routes/bar.rb
49
+ # routes/foo.rb
50
+ #
51
+ # == Namespace Support
52
+ #
53
+ # The named_routes plugin also has support for namespaces, allowing you to
54
+ # use +r.route+ at multiple levels in your routing tree. Example:
55
+ #
56
+ # route('foo') do |r|
57
+ # r.on("baz"){r.route("baz", "foo")}
58
+ # r.on("quux"){r.route("quux", "foo")}
59
+ # end
60
+ #
61
+ # route('bar') do |r|
62
+ # r.on("baz"){r.route("baz", "bar")}
63
+ # r.on("quux"){r.route("quux", "bar")}
64
+ # end
65
+ #
66
+ # route('baz', 'foo') do |r|
67
+ # # handles /foo/baz prefix
68
+ # end
69
+ #
70
+ # route('quux', 'foo') do |r|
71
+ # # handles /foo/quux prefix
72
+ # end
73
+ #
74
+ # route('baz', 'bar') do |r|
75
+ # # handles /bar/baz prefix
76
+ # end
77
+ #
78
+ # route('quux', 'bar') do |r|
79
+ # # handles /bar/quux prefix
80
+ # end
81
+ #
82
+ # route do |r|
83
+ # r.on "foo" do
84
+ # r.route("foo")
85
+ # end
86
+ #
87
+ # r.on "bar" do
88
+ # r.route("bar")
89
+ # end
90
+ # end
91
+ #
92
+ # === Routing Files
93
+ #
94
+ # The convention when using namespaces with the multi_route plugin is to
95
+ # store the routing files in subdirectories per namespace. So for the
96
+ # above example, you would have the following routing files:
97
+ #
98
+ # routes/bar.rb
99
+ # routes/bar/baz.rb
100
+ # routes/bar/quux.rb
101
+ # routes/foo.rb
102
+ # routes/foo/baz.rb
103
+ # routes/foo/quux.rb
104
+ module NamedRoutes
105
+ # Initialize storage for the named routes.
106
+ def self.configure(app)
107
+ app.opts[:namespaced_routes] ||= {}
108
+ end
109
+
110
+ module ClassMethods
111
+ # Freeze the namespaced routes so that there can be no thread safety issues at runtime.
112
+ def freeze
113
+ opts[:namespaced_routes].freeze.each_value(&:freeze)
114
+ super
115
+ end
116
+
117
+ # Copy the named routes into the subclass when inheriting.
118
+ def inherited(subclass)
119
+ super
120
+ nsr = subclass.opts[:namespaced_routes]
121
+ opts[:namespaced_routes].each{|k, v| nsr[k] = v.dup}
122
+ end
123
+
124
+ # The names for the currently stored named routes
125
+ def named_routes(namespace=nil)
126
+ unless routes = opts[:namespaced_routes][namespace]
127
+ raise RodaError, "unsupported multi_route namespace used: #{namespace.inspect}"
128
+ end
129
+ routes.keys
130
+ end
131
+
132
+ # Return the named route with the given name.
133
+ def named_route(name, namespace=nil)
134
+ opts[:namespaced_routes][namespace][name]
135
+ end
136
+
137
+ # If the given route has a name, treat it as a named route and
138
+ # store the route block. Otherwise, this is the main route, so
139
+ # call super.
140
+ def route(name=nil, namespace=nil, &block)
141
+ if name
142
+ routes = opts[:namespaced_routes][namespace] ||= {}
143
+ routes[name] = define_roda_method(routes[name] || "multi_route_#{namespace}_#{name}", 1, &convert_route_block(block))
144
+ else
145
+ super(&block)
146
+ end
147
+ end
148
+ end
149
+
150
+ module RequestMethods
151
+ # Dispatch to the named route with the given name.
152
+ def route(name, namespace=nil)
153
+ scope.send(roda_class.named_route(name, namespace), self)
154
+ end
155
+ end
156
+ end
157
+
158
+ register_plugin(:named_routes, NamedRoutes)
159
+ end
160
+ end
data/lib/roda/version.rb CHANGED
@@ -4,7 +4,7 @@ class Roda
4
4
  RodaMajorVersion = 3
5
5
 
6
6
  # The minor version of Roda, updated for new feature releases of Roda.
7
- RodaMinorVersion = 47
7
+ RodaMinorVersion = 48
8
8
 
9
9
  # The patch version of Roda, updated only for bug fixes from the last
10
10
  # feature release.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.47.0
4
+ version: 3.48.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Evans
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-13 00:00:00.000000000 Z
11
+ date: 2021-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -218,6 +218,7 @@ extra_rdoc_files:
218
218
  - doc/release_notes/3.45.0.txt
219
219
  - doc/release_notes/3.46.0.txt
220
220
  - doc/release_notes/3.47.0.txt
221
+ - doc/release_notes/3.48.0.txt
221
222
  - doc/release_notes/3.5.0.txt
222
223
  - doc/release_notes/3.6.0.txt
223
224
  - doc/release_notes/3.7.0.txt
@@ -272,6 +273,7 @@ files:
272
273
  - doc/release_notes/3.45.0.txt
273
274
  - doc/release_notes/3.46.0.txt
274
275
  - doc/release_notes/3.47.0.txt
276
+ - doc/release_notes/3.48.0.txt
275
277
  - doc/release_notes/3.5.0.txt
276
278
  - doc/release_notes/3.6.0.txt
277
279
  - doc/release_notes/3.7.0.txt
@@ -339,6 +341,7 @@ files:
339
341
  - lib/roda/plugins/multi_run.rb
340
342
  - lib/roda/plugins/multi_view.rb
341
343
  - lib/roda/plugins/multibyte_string_matcher.rb
344
+ - lib/roda/plugins/named_routes.rb
342
345
  - lib/roda/plugins/named_templates.rb
343
346
  - lib/roda/plugins/not_allowed.rb
344
347
  - lib/roda/plugins/not_found.rb