ratalada-contrib 3.1.1 → 3.1.3

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: 0c8ff8bfec43fee16d7f6446cc1ac53f039125bee25e1cc7dd462cb98eeeae89
4
- data.tar.gz: dd36a545190b503028691a3f4fb990dccc35f01a72781ada06c28b4408d31e16
3
+ metadata.gz: bd4a7fbadabdbb7170fe43f3a2c5b95c5c48f3ff3922f85c54fc00e0a4c9adc6
4
+ data.tar.gz: '0100897c369bb0855515dad4ec9b9c169dcf7a35430283cb51fd81ea3941fbf0'
5
5
  SHA512:
6
- metadata.gz: 98cabcf0bd20feadefb01d536414d7715105682779b38b272d5bdab4c61333329a83495df3974b384df84163b0d98d346bcd31fee2d443230ec68a9845adbb40
7
- data.tar.gz: e03df9995ff21f5539b7c1134c50c4170bb8eee9cba7cd9e0a5ee349a3179cd55386e2c310cddd0b8de0ddbe9a9eda47a7833fe3a121358825b458e5e58279ed
6
+ metadata.gz: 0eab446bf859b69de75f4c1ee0963eaff5751c1ab50adefbb0a2df43c959155222f635ffe99e98fac3c686c9008d41a613659212d77245b6922eb58989fff17e
7
+ data.tar.gz: a470f4628f5e8de7ff27fde72d1b03e0a83161ab213df976f2f60d6ab0cf82c364b8e2e2e1796ef054e21b1c3336ea34a9d9a3b3602283d8a4d8d078791ad094
@@ -21,6 +21,34 @@ module Ratalada
21
21
 
22
22
  def route_spelling = SPELLING
23
23
 
24
+ # ONE APP PER ROUTE FILE, NOT ONE FOR THE TREE. `use`, `set`, `before`,
25
+ # `error` and `helpers` are all class-level Sinatra DSL, so evaluating
26
+ # every file into a single Sinatra::Base makes a _layout.rb reach files
27
+ # it does not sit above: an app/(api)/_layout.rb with
28
+ # `before { content_type(:json) }` relabels every HTML page in the tree.
29
+ # A layout is meant to scope those to its own subtree, which it can only
30
+ # do if the files under it have a class of their own.
31
+ #
32
+ # So each route file (with the layouts above it) becomes its own
33
+ # subclass, and they are chained: a Sinatra app used as rack middleware
34
+ # forwards the request on when none of its own routes matched
35
+ # (Base#route_missing), so the first file that spells the path answers
36
+ # and the rest fall through. build_map already orders them
37
+ # most-specific-first.
38
+ #
39
+ # ponytail: one middleware hop per route file, walked per request.
40
+ # Collapse the leaves into one class if a fat tree ever shows up in a
41
+ # profile -- the isolation is the point, not the chain.
42
+ def mount_files(parent, map)
43
+ map.each do |prefix, paths|
44
+ child = Class.new(::Sinatra::Base)
45
+ child.route_prefix = prefix
46
+ paths.each { |path| child.class_eval(File.read(path), path, 1) }
47
+
48
+ parent.use(child)
49
+ end
50
+ end
51
+
24
52
  def route(verb, path, options = {}, &block)
25
53
  unless route_prefix.to_s.empty?
26
54
  path = Mustermann.new(FileBased.join(route_prefix, path), type: :expo)
@@ -28,9 +28,19 @@ module Ratalada
28
28
  spelling = {}
29
29
  end
30
30
 
31
- build_map(directory, **spelling).each do |prefix, paths|
32
- app.route_prefix = prefix
33
- paths.each { |path| app.class_eval(File.read(path), path, 1) }
31
+ map = build_map(directory, **spelling)
32
+
33
+ # An adapter that can give each route file its own class does, so a
34
+ # _layout.rb's `use`, `set`, `before` and `error` reach only the files
35
+ # below it. Without that the whole tree shares one class and a nested
36
+ # layout's filter fires for every route in the app.
37
+ if app.respond_to?(:mount_files)
38
+ app.mount_files(app, map)
39
+ else
40
+ map.each do |prefix, paths|
41
+ app.route_prefix = prefix
42
+ paths.each { |path| app.class_eval(File.read(path), path, 1) }
43
+ end
34
44
  end
35
45
  end
36
46
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ratalada
4
4
  module Contrib
5
- VERSION = "3.1.1"
5
+ VERSION = "3.1.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ratalada-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.1
4
+ version: 3.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan K