roda 3.108.0 → 3.110.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.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/lib/roda/cache.rb +0 -2
  3. data/lib/roda/opts.rb +22 -0
  4. data/lib/roda/plugins/Integer_matcher_max.rb +1 -1
  5. data/lib/roda/plugins/_add_script_name.rb +14 -0
  6. data/lib/roda/plugins/_json.rb +22 -0
  7. data/lib/roda/plugins/_symbol_class_matchers.rb +8 -10
  8. data/lib/roda/plugins/additional_render_engines.rb +5 -1
  9. data/lib/roda/plugins/additional_view_directories.rb +7 -3
  10. data/lib/roda/plugins/assets.rb +179 -151
  11. data/lib/roda/plugins/assets_preloading.rb +26 -10
  12. data/lib/roda/plugins/autoload_hash_branches.rb +9 -3
  13. data/lib/roda/plugins/autoload_named_routes.rb +9 -3
  14. data/lib/roda/plugins/backtracking_array.rb +2 -2
  15. data/lib/roda/plugins/branch_locals.rb +4 -4
  16. data/lib/roda/plugins/break.rb +2 -2
  17. data/lib/roda/plugins/caching.rb +27 -17
  18. data/lib/roda/plugins/capture_erb.rb +19 -12
  19. data/lib/roda/plugins/chunked.rb +42 -34
  20. data/lib/roda/plugins/class_level_routing.rb +10 -5
  21. data/lib/roda/plugins/class_matchers.rb +14 -7
  22. data/lib/roda/plugins/common_logger.rb +27 -5
  23. data/lib/roda/plugins/conditional_sessions.rb +4 -4
  24. data/lib/roda/plugins/content_for.rb +12 -7
  25. data/lib/roda/plugins/content_security_policy.rb +17 -14
  26. data/lib/roda/plugins/cookie_flags.rb +17 -7
  27. data/lib/roda/plugins/cookies.rb +12 -6
  28. data/lib/roda/plugins/csrf.rb +2 -1
  29. data/lib/roda/plugins/custom_block_results.rb +11 -10
  30. data/lib/roda/plugins/custom_matchers.rb +4 -2
  31. data/lib/roda/plugins/default_headers.rb +4 -7
  32. data/lib/roda/plugins/default_status.rb +1 -0
  33. data/lib/roda/plugins/delay_build.rb +1 -1
  34. data/lib/roda/plugins/delegate.rb +7 -6
  35. data/lib/roda/plugins/direct_call.rb +5 -1
  36. data/lib/roda/plugins/disallow_file_uploads.rb +2 -3
  37. data/lib/roda/plugins/drop_body.rb +3 -6
  38. data/lib/roda/plugins/each_part.rb +23 -29
  39. data/lib/roda/plugins/early_hints.rb +4 -4
  40. data/lib/roda/plugins/empty_root.rb +5 -5
  41. data/lib/roda/plugins/environments.rb +11 -15
  42. data/lib/roda/plugins/erb_h.rb +4 -4
  43. data/lib/roda/plugins/error_email.rb +23 -20
  44. data/lib/roda/plugins/error_handler.rb +28 -34
  45. data/lib/roda/plugins/error_mail.rb +31 -16
  46. data/lib/roda/plugins/exception_page.rb +41 -35
  47. data/lib/roda/plugins/filter_common_logger.rb +6 -4
  48. data/lib/roda/plugins/flash.rb +16 -13
  49. data/lib/roda/plugins/freeze_request_response.rb +24 -0
  50. data/lib/roda/plugins/hash_branch_view_subdir.rb +6 -4
  51. data/lib/roda/plugins/hash_branches.rb +7 -5
  52. data/lib/roda/plugins/hash_paths.rb +7 -5
  53. data/lib/roda/plugins/hash_public.rb +11 -7
  54. data/lib/roda/plugins/hash_public_cache.rb +9 -7
  55. data/lib/roda/plugins/hash_routes.rb +6 -4
  56. data/lib/roda/plugins/heartbeat.rb +6 -2
  57. data/lib/roda/plugins/hmac_paths.rb +9 -3
  58. data/lib/roda/plugins/hooks.rb +13 -10
  59. data/lib/roda/plugins/host_authorization.rb +7 -2
  60. data/lib/roda/plugins/host_routing.rb +7 -2
  61. data/lib/roda/plugins/invalid_request_body.rb +20 -4
  62. data/lib/roda/plugins/ip_from_header.rb +5 -1
  63. data/lib/roda/plugins/json.rb +10 -8
  64. data/lib/roda/plugins/json_parser.rb +26 -12
  65. data/lib/roda/plugins/mail_processor.rb +9 -6
  66. data/lib/roda/plugins/mailer.rb +5 -3
  67. data/lib/roda/plugins/match_affix.rb +6 -1
  68. data/lib/roda/plugins/match_hook_args.rb +7 -5
  69. data/lib/roda/plugins/middleware.rb +13 -7
  70. data/lib/roda/plugins/module_include.rb +1 -1
  71. data/lib/roda/plugins/multi_public.rb +6 -2
  72. data/lib/roda/plugins/multi_route.rb +1 -1
  73. data/lib/roda/plugins/multi_run.rb +13 -17
  74. data/lib/roda/plugins/named_routes.rb +8 -6
  75. data/lib/roda/plugins/named_templates.rb +5 -3
  76. data/lib/roda/plugins/path.rb +16 -14
  77. data/lib/roda/plugins/path_rewriter.rb +8 -5
  78. data/lib/roda/plugins/permissions_policy.rb +21 -19
  79. data/lib/roda/plugins/public.rb +22 -10
  80. data/lib/roda/plugins/recheck_precompiled_assets.rb +30 -8
  81. data/lib/roda/plugins/redirect_http_to_https.rb +6 -3
  82. data/lib/roda/plugins/render.rb +37 -49
  83. data/lib/roda/plugins/render_coverage.rb +6 -3
  84. data/lib/roda/plugins/render_locals.rb +13 -7
  85. data/lib/roda/plugins/request_aref.rb +1 -1
  86. data/lib/roda/plugins/response_content_type.rb +7 -3
  87. data/lib/roda/plugins/route_csrf.rb +7 -2
  88. data/lib/roda/plugins/run_append_slash.rb +5 -1
  89. data/lib/roda/plugins/sec_fetch_site_csrf.rb +6 -2
  90. data/lib/roda/plugins/sessions.rb +164 -139
  91. data/lib/roda/plugins/shape_friendly.rb +3 -1
  92. data/lib/roda/plugins/sinatra_helpers.rb +7 -3
  93. data/lib/roda/plugins/status_handler.rb +5 -3
  94. data/lib/roda/plugins/strip_path_prefix.rb +4 -2
  95. data/lib/roda/plugins/symbol_matchers.rb +11 -6
  96. data/lib/roda/plugins/symbol_views.rb +1 -1
  97. data/lib/roda/plugins/timestamp_public.rb +8 -4
  98. data/lib/roda/plugins/type_routing.rb +9 -5
  99. data/lib/roda/plugins/typecast_params.rb +2 -2
  100. data/lib/roda/plugins/typecast_params_sized_integers.rb +1 -1
  101. data/lib/roda/plugins/unescape_path.rb +22 -4
  102. data/lib/roda/plugins/url_escape.rb +2 -14
  103. data/lib/roda/plugins.rb +34 -0
  104. data/lib/roda/request.rb +8 -7
  105. data/lib/roda/response.rb +9 -7
  106. data/lib/roda/version.rb +1 -1
  107. data/lib/roda.rb +37 -23
  108. metadata +7 -3
@@ -53,10 +53,13 @@ class Roda
53
53
  end
54
54
 
55
55
  module ClassMethods
56
+ RodaPlugins.opt_attr_reader(self, :remaining_path_rewrites)
57
+ RodaPlugins.opt_attr_reader(self, :path_info_rewrites)
58
+
56
59
  # Freeze the path rewrite metadata.
57
60
  def freeze
58
- opts[:remaining_path_rewrites].freeze
59
- opts[:path_info_rewrites].freeze
61
+ remaining_path_rewrites.freeze
62
+ path_info_rewrites.freeze
60
63
  super
61
64
  end
62
65
 
@@ -75,7 +78,7 @@ class Roda
75
78
  end
76
79
 
77
80
  was = /\A#{Regexp.escape(was)}/ unless was.is_a?(Regexp)
78
- array = @opts[opts[:path_info] ? :path_info_rewrites : :remaining_path_rewrites]
81
+ array = opts[:path_info] ? path_info_rewrites : remaining_path_rewrites
79
82
  array << [was, is.dup.freeze].freeze
80
83
  end
81
84
  end
@@ -85,10 +88,10 @@ class Roda
85
88
  def initialize(scope, env)
86
89
  path_info = env['PATH_INFO']
87
90
 
88
- rewrite_path(scope.class.opts[:path_info_rewrites], path_info)
91
+ rewrite_path(scope.class.path_info_rewrites, path_info)
89
92
  super
90
93
  remaining_path = @remaining_path = @remaining_path.dup
91
- rewrite_path(scope.class.opts[:remaining_path_rewrites], remaining_path)
94
+ rewrite_path(scope.class.remaining_path_rewrites, remaining_path)
92
95
  end
93
96
 
94
97
  private
@@ -34,13 +34,13 @@ class Roda
34
34
  # # ...
35
35
  # end
36
36
  #
37
- # You can use the :default plugin option to set the default for all settings.
37
+ # You can use the +:default+ plugin argument to set the default for all settings.
38
38
  # For example, to disallow all access for each setting by default:
39
39
  #
40
40
  # plugin :permissions_policy, default: :none
41
41
  #
42
42
  # The following methods are available for configuring the permissions policy,
43
- # which specify the setting (substituting _ with -):
43
+ # which specify the setting (substituting +_+ with +-+):
44
44
  #
45
45
  # * accelerometer
46
46
  # * ambient_light_sensor
@@ -73,12 +73,12 @@ class Roda
73
73
  # All of these methods support any number of arguments, and each argument should
74
74
  # be one of the following values:
75
75
  #
76
- # :all :: Grants permission to all domains (must be only argument)
77
- # :none :: Does not allow permission at all (must be only argument)
78
- # :self :: Allows feature in current document and any nested browsing contexts
79
- # that use the same domain as the current document.
80
- # :src :: Allows feature in current document and any nested browsing contexts
81
- # that use the same domain as the src of the iframe.
76
+ # +:all+ :: Grants permission to all domains (must be only argument)
77
+ # +:none+ :: Does not allow permission at all (must be only argument)
78
+ # +:self+ :: Allows feature in current document and any nested browsing contexts
79
+ # that use the same domain as the current document.
80
+ # +:src+ :: Allows feature in current document and any nested browsing contexts
81
+ # that use the same domain as the src of the iframe.
82
82
  # String :: Specifies origin domain where access is allowed
83
83
  #
84
84
  # When calling a method with no arguments, the setting is removed from the policy instead
@@ -88,7 +88,7 @@ class Roda
88
88
  # Calling the method overrides any previous setting. Each of the methods has +add_*+ and
89
89
  # +get_*+ methods defined. The +add_*+ method appends to any existing setting, and the +get_*+ method
90
90
  # returns the current value for the setting (this will be +:all+ if all domains are allowed, or
91
- # any array of strings/:self/:src).
91
+ # any array of strings/+:self+/+:src+).
92
92
  #
93
93
  # permissions_policy.fullscreen :self, 'https://example.com'
94
94
  # # fullscreen (self "https://example.com")
@@ -99,7 +99,7 @@ class Roda
99
99
  # permissions_policy.get_fullscreen
100
100
  # # => [:self, "https://example.com", "https://*.example.com"]
101
101
  #
102
- # The clear method can be used to remove all settings from the policy. Empty policies
102
+ # The +clear+ method can be used to remove all settings from the policy. Empty policies
103
103
  # do not set any headers. You can use +response.skip_permissions_policy!+ to skip
104
104
  # setting a policy. This is faster than calling +permissions_policy.clear+, since
105
105
  # it does not duplicate the default policy.
@@ -175,16 +175,13 @@ class Roda
175
175
  end
176
176
  end
177
177
 
178
- def initialize
179
- clear
180
- end
181
-
182
178
  # Clear all settings, useful to remove any inherited settings.
183
179
  def clear
184
180
  @opts = {}
185
181
  @report_only = nil
186
182
  @header_value = nil
187
183
  end
184
+ alias initialize clear
188
185
 
189
186
  # Do not allow future modifications to any settings.
190
187
  def freeze
@@ -279,14 +276,15 @@ class Roda
279
276
  end
280
277
 
281
278
  # Yield the current Permissions Policy to the block.
282
- def self.configure(app, opts=OPTS)
283
- policy = app.opts[:permissions_policy] = if policy = app.opts[:permissions_policy]
279
+ def self.configure(app, opts=(no_positional_opts = true; OPTS), default: opts[:default], **kw)
280
+ RodaPlugins.check_keywords("the permissions_policy plugin", no_positional_opts, opts, kw)
281
+ policy = app.opts[:permissions_policy] = if policy = app.permissions_policy
284
282
  policy.dup
285
283
  else
286
284
  Policy.new
287
285
  end
288
286
 
289
- if default = opts[:default]
287
+ if default
290
288
  SUPPORTED_SETTINGS.each do |setting|
291
289
  policy.send(setting.tr('-', '_'), *default)
292
290
  end
@@ -296,6 +294,10 @@ class Roda
296
294
  policy.freeze
297
295
  end
298
296
 
297
+ module ClassMethods
298
+ RodaPlugins.opt_attr_reader(self, :permissions_policy)
299
+ end
300
+
299
301
  module InstanceMethods
300
302
  # If a block is given, yield the current permission policy. Returns the
301
303
  # current permissions policy.
@@ -315,7 +317,7 @@ class Roda
315
317
 
316
318
  # The current permissions policy to be used for this response.
317
319
  def permissions_policy
318
- @permissions_policy ||= roda_class.opts[:permissions_policy].dup
320
+ @permissions_policy ||= roda_class.permissions_policy.dup
319
321
  end
320
322
 
321
323
  # Do not set a permissions policy header for this response.
@@ -329,7 +331,7 @@ class Roda
329
331
  def set_default_headers
330
332
  super
331
333
  unless @skip_permissions_policy
332
- (@permissions_policy || roda_class.opts[:permissions_policy]).set_header(headers)
334
+ (@permissions_policy || roda_class.permissions_policy).set_header(headers)
333
335
  end
334
336
  end
335
337
  end
@@ -69,19 +69,15 @@ class Roda
69
69
  # :zstd :: Whether to serve already zstd-compressed files with a .zst extension
70
70
  # for clients supporting "zstd" transfer encoding.
71
71
  def self.configure(app, opts={})
72
- if opts[:root]
73
- app.opts[:public_root] = app.expand_path(opts[:root])
74
- elsif !app.opts[:public_root]
75
- app.opts[:public_root] = app.expand_path("public")
76
- end
77
- app.opts[:public_server] = RACK_FILES.new(app.opts[:public_root], opts[:headers]||{}, opts[:default_mime] || 'text/plain')
72
+ app.opts[:public_root] = app.expand_path(opts[:root] || app.public_root || "public")
73
+ app.opts[:public_server] = RACK_FILES.new(app.public_root, opts[:headers]||{}, opts[:default_mime] || 'text/plain')
78
74
 
79
75
  unless encodings = opts[:encodings]
80
76
  if ENCODING_MAP.any?{|k,| opts.has_key?(k)}
81
77
  encodings = ENCODING_MAP.map{|k, v| [v, ENCODING_EXTENSIONS[v]] if opts[k]}.compact
82
78
  end
83
79
  end
84
- encodings = (encodings || app.opts[:public_encodings] || EMPTY_ARRAY).map(&:dup).freeze
80
+ encodings = (encodings || app.public_encodings || EMPTY_ARRAY).map(&:dup).freeze
85
81
  encodings.each do |a|
86
82
  a << /\b#{a[0]}\b/
87
83
  end
@@ -89,10 +85,16 @@ class Roda
89
85
  app.opts[:public_encodings] = encodings
90
86
  end
91
87
 
88
+ module ClassMethods
89
+ RodaPlugins.opt_attr_reader(self, :public_root)
90
+ RodaPlugins.opt_attr_reader(self, :public_server)
91
+ RodaPlugins.opt_attr_reader(self, :public_encodings)
92
+ end
93
+
92
94
  module RequestMethods
93
95
  # Serve files from the public directory if the file exists and this is a GET request.
94
96
  def public
95
- public_serve_with(roda_class.opts[:public_server])
97
+ public_serve_with(roda_class.public_server)
96
98
  end
97
99
 
98
100
  private
@@ -124,11 +126,10 @@ class Roda
124
126
  path = PARSER.unescape(real_remaining_path)
125
127
  return if path.include?("\0")
126
128
 
127
- roda_opts = roda_class.opts
128
129
  path = ::File.join(server.root, *public_path_segments(path))
129
130
 
130
131
  if accept_encoding = env['HTTP_ACCEPT_ENCODING']
131
- roda_opts[:public_encodings].each do |enc, ext, regexp|
132
+ roda_class.public_encodings.each do |enc, ext, regexp|
132
133
  if regexp.send(MATCH_METHOD, accept_encoding)
133
134
  public_serve_compressed(server, path, ext, enc)
134
135
  end
@@ -139,10 +140,20 @@ class Roda
139
140
  s, h, b = public_serve(server, path)
140
141
  headers = response.headers
141
142
  headers.replace(h)
143
+ public_add_vary(headers)
142
144
  halt [s, headers, b]
143
145
  end
144
146
  end
145
147
 
148
+ # When encodings are used, the response depends on the Accept-Encoding
149
+ # request header, so add it to the Vary header for caches.
150
+ def public_add_vary(headers)
151
+ unless roda_class.public_encodings.empty?
152
+ headers[RodaResponseHeaders::VARY] = (vary = headers[RodaResponseHeaders::VARY]) ? "#{vary}, Accept-Encoding" : 'Accept-Encoding'
153
+ end
154
+ nil
155
+ end
156
+
146
157
  # Serve the compressed file if it exists. This should only
147
158
  # be called if the client will accept the related encoding.
148
159
  def public_serve_compressed(server, path, suffix, encoding)
@@ -157,6 +168,7 @@ class Roda
157
168
  headers[RodaResponseHeaders::CONTENT_TYPE] = ::Rack::Mime.mime_type(::File.extname(path), 'text/plain')
158
169
  headers[RodaResponseHeaders::CONTENT_ENCODING] = encoding
159
170
  end
171
+ public_add_vary(headers)
160
172
 
161
173
  halt [s, headers, b]
162
174
  end
@@ -48,27 +48,37 @@ class Roda
48
48
  end
49
49
 
50
50
  def self.load_dependencies(app)
51
- unless app.respond_to?(:assets_opts) && app.assets_opts[:precompiled]
51
+ unless app.respond_to?(:assets_config) && app.assets_config.precompiled
52
52
  raise RodaError, "must load assets plugin with precompiled option before loading recheck_precompiled_assets plugin"
53
53
  end
54
+ # Set mutex before overriding a method that uses it
55
+ app::RodaRequest.instance_variable_set(:@_precompiled_assets_mutex, Mutex.new)
54
56
  end
55
57
 
56
58
  def self.configure(app)
57
- precompiled_file = app.assets_opts[:precompiled]
59
+ precompiled_file = app.assets_config.precompiled
58
60
  prev_mtime = ::File.mtime(precompiled_file)
61
+
62
+ request_class = app::RodaRequest
63
+ mutex = request_class.instance_variable_get(:@_precompiled_assets_mutex)
64
+ # Unset the assets matchers, in case a frozen value is already set
65
+ request_class.instance_variable_set(:@assets_matchers, nil)
66
+ # Ensure the assets matchers is set and an unfrozen array, and get the underlying value
67
+ request_class.assets_matchers
68
+ assets_matchers = request_class.instance_variable_get(:@assets_matchers)
69
+
59
70
  app.instance_exec do
60
- opts[:assets] = opts[:assets].merge(:compiled=>_compiled_assets_initial_hash.replace(assets_opts[:compiled])).freeze
71
+ _update_compiled_assets(_compiled_assets_initial_hash.replace(assets_config.compiled))
61
72
 
62
73
  define_singleton_method(:recheck_precompiled_assets) do
63
74
  new_mtime = ::File.mtime(precompiled_file)
64
75
  if new_mtime != prev_mtime
65
76
  prev_mtime = new_mtime
66
- assets_opts[:compiled].replace(_precompiled_asset_metadata(precompiled_file))
77
+ assets_config.compiled.replace(_precompiled_asset_metadata(precompiled_file))
67
78
 
68
- # Unset the cached asset matchers, so new ones will be generated.
69
- # This is needed in case the new precompiled metadata uses
70
- # different files.
71
- app::RodaRequest.instance_variable_set(:@assets_matchers, nil)
79
+ # Replace the asset matchers, in case the new precompiled metadata uses different files.
80
+ new_assets_matchers = request_class.send(:_build_assets_matchers)
81
+ mutex.synchronize{assets_matchers.replace(new_assets_matchers)}
72
82
  end
73
83
  end
74
84
  singleton_class.send(:alias_method, :recheck_precompiled_assets, :recheck_precompiled_assets)
@@ -91,8 +101,20 @@ class Roda
91
101
  end
92
102
 
93
103
  module RequestClassMethods
104
+ # Synchronize access and return a dup, so that replacements of the
105
+ # assets matchers at runtime doesn't cause thread safety issues.
106
+ def assets_matchers
107
+ @_precompiled_assets_mutex.synchronize{super.dup}
108
+ end
109
+
94
110
  private
95
111
 
112
+ # Use a unfrozen array for the assets matchers, so they can be replaced
113
+ # at runtime.
114
+ def _build_assets_matchers
115
+ super.dup
116
+ end
117
+
96
118
  # Use a regexp that matches any digest. When the precompiled asset metadata
97
119
  # file is updated, this allows requests for a previous precompiled asset to
98
120
  # still work.
@@ -53,13 +53,16 @@ class Roda
53
53
  # uses a hash that redirects GET and HEAD requests with a 301 status,
54
54
  # and other request methods with a 307 status.
55
55
  def self.configure(app, opts=OPTS)
56
- previous = app.opts[:redirect_http_to_https] || DEFAULTS
57
- opts = app.opts[:redirect_http_to_https] = previous.merge(opts)
56
+ opts = app.opts[:redirect_http_to_https] = (app.redirect_http_to_https_opts || DEFAULTS).merge(opts)
58
57
  opts[:port_string] = opts[:port] ? ":#{opts[:port]}".freeze : "".freeze
59
58
  opts[:prefix] = opts[:host] ? "https://#{opts[:host]}#{opts[:port_string]}".freeze : nil
60
59
  opts.freeze
61
60
  end
62
61
 
62
+ module ClassMethods
63
+ RodaPlugins.opt_attr_reader(self, :redirect_http_to_https, name: :redirect_http_to_https_opts)
64
+ end
65
+
63
66
  module RequestMethods
64
67
  # Redirect HTTP requests to HTTPS. While this doesn't secure the
65
68
  # current request, it makes it more likely that the browser will submit
@@ -67,7 +70,7 @@ class Roda
67
70
  def redirect_http_to_https
68
71
  return if ssl?
69
72
 
70
- opts = roda_class.opts[:redirect_http_to_https]
73
+ opts = roda_class.redirect_http_to_https_opts
71
74
 
72
75
  res = response
73
76
 
@@ -323,15 +323,15 @@ class Roda
323
323
 
324
324
  # Setup default rendering options. See Render for details.
325
325
  def self.configure(app, opts=OPTS)
326
- if app.opts[:render]
327
- orig_cache = app.opts[:render][:cache]
328
- orig_method_cache = app.opts[:render][:template_method_cache]
329
- opts = app.opts[:render][:orig_opts].merge(opts)
326
+ if app.render_opts
327
+ orig_cache = app.render_opts[:cache]
328
+ orig_method_cache = app.render_opts[:template_method_cache]
329
+ opts = app.render_opts[:orig_opts].merge(opts)
330
330
  end
331
331
  app.opts[:render] = opts.dup
332
- app.opts[:render][:orig_opts] = opts
332
+ app.render_opts[:orig_opts] = opts
333
333
 
334
- opts = app.opts[:render]
334
+ opts = app.render_opts
335
335
  opts[:engine] = (opts[:engine] || "erb").dup.freeze
336
336
  opts[:views] = app.expand_path(opts[:views]||"views").freeze
337
337
  opts[:check_paths] = true unless opts.has_key?(:check_paths)
@@ -580,18 +580,18 @@ class Roda
580
580
  # affecting the parent class.
581
581
  def inherited(subclass)
582
582
  super
583
- opts = subclass.opts[:render] = subclass.opts[:render].dup
583
+ opts = subclass.opts[:render] = subclass.render_opts.dup
584
584
  if COMPILED_METHOD_SUPPORT
585
585
  opts[:template_method_cache] = (opts[:cache_class] || RodaCache).new
586
+ compiled_templates_module = Module.new
587
+ subclass.send(:include, compiled_templates_module)
588
+ subclass.const_set(:RodaCompiledTemplates, compiled_templates_module)
586
589
  end
587
590
  opts[:cache] = opts[:cache].dup
588
591
  opts.freeze
589
592
  end
590
593
 
591
- # Return the render options for this class.
592
- def render_opts
593
- opts[:render]
594
- end
594
+ RodaPlugins.opt_attr_reader(self, :render, name: :render_opts)
595
595
 
596
596
  private
597
597
 
@@ -603,13 +603,13 @@ class Roda
603
603
  # in order for the precompile_templates plugin to work correctly.
604
604
  instance.send(:retrieve_template, instance.send(:view_layout_opts, OPTS))
605
605
 
606
- if COMPILED_METHOD_SUPPORT && (layout_template = render_opts[:optimize_layout]) && !opts[:render][:optimized_layout_method_created]
606
+ if COMPILED_METHOD_SUPPORT && (layout_template = render_opts[:optimize_layout]) && !render_opts[:optimized_layout_method_created]
607
607
  instance.send(:retrieve_template, :template=>layout_template, :cache_key=>nil, :template_method_cache_key => :_roda_layout)
608
- layout_method = opts[:render][:template_method_cache][:_roda_layout]
608
+ layout_method = render_opts[:template_method_cache][:_roda_layout]
609
609
  define_method(:_layout_method){layout_method}
610
610
  private :_layout_method
611
611
  alias_method(:_layout_method, :_layout_method)
612
- opts[:render] = opts[:render].merge(:optimized_layout_method_created=>true)
612
+ opts[:render] = render_opts.merge(:optimized_layout_method_created=>true)
613
613
  end
614
614
  end
615
615
  end
@@ -650,7 +650,7 @@ class Roda
650
650
  # If we have an optimized template method but no optimized layout method, create the
651
651
  # optimized layout method if possible and use it. If you can't create the optimized
652
652
  # layout method, fall through to the slower approach.
653
- if layout_template = self.class.opts[:render][:optimize_layout]
653
+ if layout_template = self.class.render_opts[:optimize_layout]
654
654
  retrieve_template(:template=>layout_template, :cache_key=>nil, :template_method_cache_key => :_roda_layout)
655
655
  if layout_method = _layout_method
656
656
  return _call_optimized_template_method(layout_method, OPTS){content}
@@ -695,7 +695,7 @@ class Roda
695
695
 
696
696
  # Return a symbol containing the optimized layout method
697
697
  def _layout_method
698
- self.class.opts[:render][:template_method_cache][:_roda_layout]
698
+ self.class.render_opts[:template_method_cache][:_roda_layout]
699
699
  end
700
700
 
701
701
  # Use an optimized render path for templates with a hash of locals. Returns the result
@@ -766,37 +766,25 @@ class Roda
766
766
  end
767
767
 
768
768
  if RUBY_VERSION >= '3'
769
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
770
- def _call_optimized_template_method((meth, fixed_locals), locals, &block)
771
- if fixed_locals
772
- send(meth, **locals, &block)
773
- else
774
- send(meth, locals, &block)
775
- end
769
+ def _call_optimized_template_method((meth, fixed_locals), locals, &block)
770
+ if fixed_locals
771
+ send(meth, **locals, &block)
772
+ else
773
+ send(meth, locals, &block)
776
774
  end
777
- RUBY
775
+ end
778
776
  # simplecov:disable
779
- elsif RUBY_VERSION >= '2'
780
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
781
- def _call_optimized_template_method((meth, fixed_locals), locals, &block)
782
- if fixed_locals
783
- if locals.empty?
784
- send(meth, &block)
785
- else
786
- send(meth, **locals, &block)
787
- end
777
+ else
778
+ def _call_optimized_template_method((meth, fixed_locals), locals, &block)
779
+ if fixed_locals
780
+ if locals.empty?
781
+ send(meth, &block)
788
782
  else
789
- send(meth, locals, &block)
783
+ send(meth, **locals, &block)
790
784
  end
785
+ else
786
+ send(meth, locals, &block)
791
787
  end
792
- RUBY
793
- else
794
- # Call the optimized template method. This is designed to be used with the
795
- # method cache, which caches the method name and whether the method uses
796
- # fixed locals. Methods with fixed locals need to be called with a keyword
797
- # splat.
798
- def _call_optimized_template_method((meth, fixed_locals), locals, &block)
799
- send(meth, locals, &block)
800
788
  end
801
789
  end
802
790
  # simplecov:enable
@@ -844,7 +832,7 @@ class Roda
844
832
  # Given the template name and options, set the template class, template path/content,
845
833
  # template block, and locals to use for the render in the passed options.
846
834
  def find_template(opts)
847
- render_opts = self.class.opts[:render]
835
+ render_opts = self.class.render_opts
848
836
  engine_override = opts[:engine]
849
837
  engine = opts[:engine] ||= render_opts[:engine]
850
838
  if content = opts[:inline]
@@ -908,7 +896,7 @@ class Roda
908
896
  end
909
897
  cached_template(opts) do
910
898
  opts = found_template_opts || find_template(opts)
911
- render_opts = self.class.opts[:render]
899
+ render_opts = self.class.render_opts
912
900
  template_opts = render_opts[:template_opts]
913
901
  if engine_opts = render_opts[:engine_opts][opts[:engine]]
914
902
  template_opts = template_opts.merge(engine_opts)
@@ -993,14 +981,14 @@ class Roda
993
981
  end
994
982
 
995
983
  module AssumeFixedLocalsInstanceMethods
984
+ private
985
+
996
986
  # simplecov:disable
997
987
  if RUBY_VERSION >= '3.0'
998
988
  # simplecov:enable
999
- class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
1000
- def _call_optimized_template_method((meth,_), locals, &block)
1001
- send(meth, **locals, &block)
1002
- end
1003
- RUBY
989
+ def _call_optimized_template_method((meth,_), locals, &block)
990
+ send(meth, **locals, &block)
991
+ end
1004
992
  end
1005
993
  end
1006
994
  end
@@ -42,11 +42,14 @@ class Roda
42
42
  # strip.
43
43
  def self.configure(app, opts=OPTS)
44
44
  app.opts[:render_coverage_strip_paths] = opts[:strip_paths].map{|f| File.expand_path(f)} if opts.has_key?(:strip_paths)
45
- coverage_dir = app.opts[:render_coverage_dir] = opts[:dir] || app.opts[:render_coverage_dir] || 'coverage/views'
45
+ coverage_dir = app.opts[:render_coverage_dir] = opts[:dir] || app.render_coverage_dir || 'coverage/views'
46
46
  Dir.mkdir(coverage_dir) unless File.directory?(coverage_dir)
47
47
  end
48
48
 
49
49
  module ClassMethods
50
+ RodaPlugins.opt_attr_reader(self, :render_coverage_strip_paths)
51
+ RodaPlugins.opt_attr_reader(self, :render_coverage_dir)
52
+
50
53
  # Set a compiled path on the created template, if the path for
51
54
  # the template is in one of the allowed_views.
52
55
  def create_template(opts, template_opts)
@@ -54,10 +57,10 @@ class Roda
54
57
 
55
58
  path = File.expand_path(opts[:path])
56
59
  compiled_path = nil
57
- (self.opts[:render_coverage_strip_paths] || render_opts[:allowed_paths]).each do |dir|
60
+ (render_coverage_strip_paths || render_opts[:allowed_paths]).each do |dir|
58
61
  dir += "/" unless dir.end_with?("/")
59
62
  if path.start_with?(dir)
60
- compiled_path = File.join(self.opts[:render_coverage_dir], path[dir.length, 10000000].tr('/', '-'))
63
+ compiled_path = File.join(render_coverage_dir, path[dir.length, 10000000].tr('/', '-'))
61
64
  break
62
65
  end
63
66
  end
@@ -32,14 +32,20 @@ class Roda
32
32
  end
33
33
 
34
34
  def self.configure(app, opts=OPTS)
35
- app.opts[:render_locals] = (app.opts[:render_locals] || {}).merge(opts[:render]||{}).freeze
36
- app.opts[:layout_locals] = (app.opts[:layout_locals] || {}).merge(opts[:layout]||{}).freeze
35
+ app.opts[:render_locals] = (app.render_locals || {}).merge(opts[:render]||{}).freeze
36
+ app.opts[:layout_locals] = (app.layout_locals || {}).merge(opts[:layout]||{}).freeze
37
37
  if opts.has_key?(:merge)
38
38
  app.opts[:merge_locals] = opts[:merge]
39
- app.opts[:layout_locals] = app.opts[:render_locals].merge(app.opts[:layout_locals]).freeze
39
+ app.opts[:layout_locals] = app.render_locals.merge(app.layout_locals).freeze
40
40
  end
41
41
  end
42
42
 
43
+ module ClassMethods
44
+ RodaPlugins.opt_attr_reader(self, :render_locals)
45
+ RodaPlugins.opt_attr_reader(self, :layout_locals)
46
+ RodaPlugins.opt_attr_reader(self, :merge_locals, name: :merge_locals?)
47
+ end
48
+
43
49
  module InstanceMethods
44
50
  private
45
51
 
@@ -56,11 +62,11 @@ class Roda
56
62
  end
57
63
 
58
64
  def render_locals
59
- opts[:render_locals]
65
+ self.class.render_locals
60
66
  end
61
67
 
62
68
  def layout_locals
63
- opts[:layout_locals]
69
+ self.class.layout_locals
64
70
  end
65
71
 
66
72
  # If this isn't the layout template, then use the plugin's render locals as the default locals.
@@ -70,7 +76,7 @@ class Roda
70
76
 
71
77
  plugin_locals = render_locals
72
78
  if locals = opts[:locals]
73
- plugin_locals = Hash[plugin_locals].merge!(locals)
79
+ plugin_locals = plugin_locals.merge(locals)
74
80
  end
75
81
  opts[:locals] = plugin_locals
76
82
  opts
@@ -79,7 +85,7 @@ class Roda
79
85
  # If using a layout, then use the plugin's layout locals as the default locals.
80
86
  def view_layout_opts(opts)
81
87
  if layout_opts = super
82
- merge_locals = layout_opts.has_key?(:merge_locals) ? layout_opts[:merge_locals] : self.opts[:merge_locals]
88
+ merge_locals = layout_opts.fetch(:merge_locals){self.class.merge_locals?}
83
89
 
84
90
  locals = {}
85
91
  locals.merge!(layout_locals)
@@ -20,7 +20,7 @@ class Roda
20
20
  def self.configure(app, setting)
21
21
  case setting
22
22
  when :allow, :raise, :warn
23
- app::RodaRequest.class_eval do
23
+ app::RodaRequest.class_exec do
24
24
  alias_method(:[], :"request_aref_#{setting}")
25
25
  alias_method(:[]=, :"request_aset_#{setting}")
26
26
  public :[], :[]=
@@ -50,12 +50,16 @@ class Roda
50
50
  else
51
51
  mime_types.dup
52
52
  end
53
- app.opts[:repsonse_content_types] = mime_types.freeze
53
+ app.opts[:response_content_types] = mime_types.freeze
54
54
  else
55
- app.opts[:repsonse_content_types] ||= {}
55
+ app.opts[:response_content_types] ||= {}
56
56
  end
57
57
  end
58
58
 
59
+ module ClassMethods
60
+ RodaPlugins.opt_attr_reader(self, :response_content_types)
61
+ end
62
+
59
63
  module ResponseMethods
60
64
  # Return the content-type of the response. Will be nil if it has
61
65
  # not yet been explicitly set.
@@ -68,7 +72,7 @@ class Roda
68
72
  # type with the given file extension. If the symbol is not
69
73
  # a recognized mime type, raises KeyError.
70
74
  def content_type=(mime_type)
71
- mime_type = roda_class.opts[:repsonse_content_types].fetch(mime_type) if mime_type.is_a?(Symbol)
75
+ mime_type = roda_class.response_content_types.fetch(mime_type) if mime_type.is_a?(Symbol)
72
76
  @headers[RodaResponseHeaders::CONTENT_TYPE] = mime_type
73
77
  end
74
78
  end
@@ -196,7 +196,7 @@ class Roda
196
196
  end
197
197
 
198
198
  def self.configure(app, opts=OPTS, &block)
199
- options = app.opts[:route_csrf] = (app.opts[:route_csrf] || DEFAULTS).merge(opts)
199
+ options = app.opts[:route_csrf] = (app.route_csrf_opts || DEFAULTS).merge(opts)
200
200
  if block || opts[:csrf_failure].is_a?(Proc)
201
201
  if block && opts[:csrf_failure]
202
202
  raise RodaError, "Cannot specify both route_csrf plugin block and :csrf_failure option"
@@ -212,6 +212,10 @@ class Roda
212
212
  options.freeze
213
213
  end
214
214
 
215
+ module ClassMethods
216
+ RodaPlugins.opt_attr_reader(self, :route_csrf, name: :route_csrf_opts)
217
+ end
218
+
215
219
  module InstanceMethods
216
220
  # Check that the submitted CSRF token is valid, if the request requires a CSRF token.
217
221
  # If the CSRF token is valid or the request does not require a CSRF token, return nil.
@@ -358,6 +362,7 @@ class Roda
358
362
  end
359
363
 
360
364
  if (rack_csrf_key = opts[:upgrade_from_rack_csrf_key]) && (rack_csrf_value = session[rack_csrf_key]) && csrf_compare(rack_csrf_value, encoded_token)
365
+ # RODA4: Remove :upgrade_from_rack_csrf_key support
361
366
  return
362
367
  end
363
368
 
@@ -392,7 +397,7 @@ class Roda
392
397
 
393
398
  # Helper for getting the plugin options.
394
399
  def csrf_options
395
- opts[:route_csrf]
400
+ self.class.route_csrf_opts
396
401
  end
397
402
 
398
403
  # Perform a constant-time comparison of the two strings, returning true if they match and false otherwise.