padrino-core 0.18.5 → 0.18.7

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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/padrino-core/application/authenticity_token.rb +1 -1
  3. data/lib/padrino-core/application/routing.rb +38 -14
  4. data/lib/padrino-core/filter.rb +2 -2
  5. data/lib/padrino-core/loader.rb +1 -1
  6. data/lib/padrino-core/logger.rb +6 -1
  7. data/lib/padrino-core/path_router/compiler.rb +21 -3
  8. data/lib/padrino-core/path_router/matcher.rb +3 -0
  9. data/lib/padrino-core/path_router/route.rb +2 -2
  10. data/lib/padrino-core/path_router.rb +3 -3
  11. data/lib/padrino-core/reloader/storage.rb +55 -22
  12. data/lib/padrino-core/reloader.rb +62 -21
  13. data/lib/padrino-core/version.rb +1 -1
  14. data/test/fixtures/apps/concerned/models/branded_item.rb +5 -0
  15. data/test/fixtures/apps/concerned/models/mixins/branded.rb +13 -0
  16. data/test/fixtures/apps/excluded_constants/app.rb +7 -0
  17. data/test/fixtures/apps/excluded_constants/helpers/excluded_constants_helpers.rb +7 -0
  18. data/test/fixtures/apps/excluded_constants/support/excluded_constants_settings.rb +8 -0
  19. data/test/fixtures/apps/loaded_excluded/app.rb +7 -0
  20. data/test/fixtures/apps/loaded_excluded/helpers/loaded_excluded_helpers.rb +7 -0
  21. data/test/fixtures/apps/loaded_excluded/support/loaded_excluded_classes.rb +15 -0
  22. data/test/fixtures/dependencies/dbadmin/child.rb +2 -0
  23. data/test/fixtures/dependencies/dbadmin/parent.rb +2 -0
  24. data/test/test_csrf_protection.rb +13 -0
  25. data/test/test_dependencies.rb +16 -0
  26. data/test/test_filters.rb +48 -0
  27. data/test/test_logger.rb +39 -0
  28. data/test/test_reloader_external.rb +9 -0
  29. data/test/test_reloader_simple.rb +281 -0
  30. data/test/test_reloader_storage.rb +101 -0
  31. data/test/test_reloader_system.rb +86 -0
  32. data/test/test_routing.rb +312 -2
  33. metadata +14 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be05253b26516488a094d26e9867de40bf05e08fef5d5522bd0cd0d17cd31c9b
4
- data.tar.gz: 7c6cd5ead1fd9ffac74c8c2dad9d74d64156af2c9ae04bf33bbbc5a0e6a8d177
3
+ metadata.gz: 9b5b8184c5fd07158687b3ee195f5d7c23052dd64fe5be9b19a3abeca6484144
4
+ data.tar.gz: 70086f0b6fe227c80a98c1ba6838c5d86d09dec514ab5db32b98e0db8aed8c1e
5
5
  SHA512:
6
- metadata.gz: 62df53f97d43739feaf1a71c3b09f27f39f28972ad242f321a60f41327ec3fb2dc854b75f7b109fe02cb3389a1ee10e13d20f6e9a01ce424dd02ed06c5309581
7
- data.tar.gz: fcac6597b21ff08a3c7c20d35ddfeaeb1b7e9ac5dbfd5e83ab9bd0a9d1c705f3c3474da8daaa12d6519c9e9c1227bbcf66057f801719748db3e74fa859ce3e1b
6
+ metadata.gz: c3acd5bc7d41c6445224ccb44f71d0d3ad739d22f2b5cdad4bc7efb4d6c206551a9ee6289efc41c43f4d2ea410069799c054916e2b7188f796924475c16133b4
7
+ data.tar.gz: 9603d7794a74800e082573f4919c0b1e4385169728614ea3052cc870c2723521d0d3360044708e26dbf03d7f0d0c393ba99629fba3d6aa5b3d07e15ff36d4415
@@ -36,7 +36,7 @@ module Padrino
36
36
  private
37
37
 
38
38
  def excepted?(form)
39
- @except.any? { |path| path.is_a?(Regexp) ? path.match?(form) : path == form }
39
+ @except.any? { |path| path.is_a?(Regexp) ? Routing.path_match?(path, form) : path == form }
40
40
  end
41
41
  end
42
42
  end
@@ -105,16 +105,31 @@ module Padrino
105
105
  [route_path, decode_path(path), decode_path(route_path)].map(&:scrub).uniq
106
106
  end
107
107
 
108
+ ##
109
+ # Whether +pattern+ (a Regexp, String or anything with ===) matches +path+, a
110
+ # decoded form of a request path. A Regexp fixed to an encoding the path can't be
111
+ # read in (/n with non-ASCII bytes) doesn't match it, as in Sinatra.
112
+ #
113
+ # @api private
114
+ def path_match?(pattern, path)
115
+ pattern === path
116
+ rescue Encoding::CompatibilityError
117
+ false
118
+ end
119
+
108
120
  ##
109
121
  # Whether a provides condition for +types+ accepts +format+, a Symbol
110
- # decoded as route params receive it.
122
+ # decoded as route params receive it. :any accepts only the name of a
123
+ # file extension with a registered MIME type, which the route answers with.
111
124
  #
112
125
  # @example
113
126
  # Padrino::Routing.provided_format?(%i[html json], :json) # => true
127
+ # Padrino::Routing.provided_format?([:any], :nosuchext) # => false
128
+ # Padrino::Routing.provided_format?([:any], :'text/plain') # => false
114
129
  #
115
130
  # @api private
116
131
  def provided_format?(types, format)
117
- !([:any, format] & types).empty?
132
+ types.include?(format) || (types.include?(:any) && !::Rack::Mime.mime_type(".#{format}", nil).nil?)
118
133
  end
119
134
 
120
135
  ##
@@ -422,10 +437,10 @@ module Padrino
422
437
  # Recognize a given path.
423
438
  #
424
439
  # @param [String] path
425
- # Path+Query to parse
440
+ # The path to recognize, percent-encoded or not, without a query string.
426
441
  #
427
- # @return [Symbol, Hash]
428
- # Returns controller and query params.
442
+ # @return [Array(Symbol, Hash), nil]
443
+ # Returns controller and query params, or nil when no route matches the path.
429
444
  #
430
445
  # @example Giving a controller like:
431
446
  # controller :foo do
@@ -441,8 +456,7 @@ module Padrino
441
456
  # # => [:foo_bar, { id: :mine }]
442
457
  #
443
458
  def recognize_path(path)
444
- responses = @router.recognize_path(path)
445
- [responses[0], responses[1]]
459
+ @router.recognize_path(path)
446
460
  end
447
461
 
448
462
  ##
@@ -626,6 +640,7 @@ module Padrino
626
640
  # get :list, provides: [:js, :json] # => "/list.{!format,js|json}"
627
641
  # get :list, provides: [:html, :js, :json] # => "/list(.{!format,js|json})"
628
642
  # get :list, priority: :low # Defers route to be last
643
+ # get :index, host: 'admin.example.com' # Only for that host (a String, Regexp or Array)
629
644
  # get /pattern/, name: :foo, generate_with: '/foo' # Generates :foo as /foo
630
645
  def route(verb, path, *args, &block)
631
646
  options =
@@ -870,7 +885,7 @@ module Padrino
870
885
  def provides_format(*types)
871
886
  mime_types = types.map { |type| mime_type(CONTENT_TYPE_ALIASES[type] || type) }
872
887
  condition do
873
- return provides_format?(types, params[:format].to_sym) if params[:format]
888
+ return provides_format?(types, params[:format].to_s.scrub.to_sym) if params[:format]
874
889
 
875
890
  accepts = request.accept.map(&:to_str)
876
891
  # Per rfc2616-sec14:
@@ -881,7 +896,7 @@ module Padrino
881
896
 
882
897
  accepts = accepts.empty? ? mime_types.slice(0, 1) : (accepts & mime_types)
883
898
 
884
- type = accepts.first && mime_symbol(accepts.first)
899
+ type = accepts.first && (mime_symbol(accepts.first) || accepts.first)
885
900
  type ||= catch_all && types.first
886
901
 
887
902
  accept_format = CONTENT_TYPE_ALIASES[type] || type
@@ -908,7 +923,7 @@ module Padrino
908
923
  mime_types = types.map { |type| mime_type(CONTENT_TYPE_ALIASES[type] || type) }
909
924
  condition do
910
925
  halt 406 unless mime_types.include?(request.media_type)
911
- content_type(mime_symbol(request.media_type))
926
+ content_type(mime_symbol(request.media_type) || request.media_type)
912
927
  end
913
928
  end
914
929
 
@@ -1062,8 +1077,7 @@ module Padrino
1062
1077
  private
1063
1078
 
1064
1079
  def provides_any?(formats)
1065
- accepted_format = formats.first
1066
- type = accepted_format ? mime_symbol(accepted_format) : :html
1080
+ type = formats.filter_map { |format| mime_symbol(format) || (:html if ::Rack::Mime.match?('text/html', format)) }.first || :html
1067
1081
  content_type(CONTENT_TYPE_ALIASES[type] || type)
1068
1082
  end
1069
1083
 
@@ -1077,7 +1091,8 @@ module Padrino
1077
1091
  end
1078
1092
 
1079
1093
  def mime_symbol(media_type)
1080
- ::Rack::Mime::MIME_TYPES.key(media_type).sub(/\./, '').to_sym
1094
+ extension = ::Rack::Mime::MIME_TYPES.key(media_type)
1095
+ extension&.sub(/\./, '')&.to_sym
1081
1096
  end
1082
1097
 
1083
1098
  def filter!(type, base = settings)
@@ -1121,7 +1136,10 @@ module Padrino
1121
1136
  first_time = true
1122
1137
 
1123
1138
  routes = base.compiled_router.call(@request) do |route, params|
1124
- next if route.user_agent && route.user_agent !~ @request.user_agent
1139
+ # ceiling: a route skipped here for its host or agent still counts for the 405 check below;
1140
+ # drop those routes from +routes+ before the check to answer 404 instead.
1141
+ next if route.host && !host_matches?(route.host)
1142
+ next if route.user_agent && !(route.user_agent === @request.user_agent)
1125
1143
  original_params, parent_layout = @params.dup, @layout
1126
1144
  returned_pass_block = invoke_route(route, params, first_time)
1127
1145
  pass_block = returned_pass_block if returned_pass_block
@@ -1182,6 +1200,12 @@ module Padrino
1182
1200
  end
1183
1201
  end
1184
1202
 
1203
+ # A route's +host:+ is a String (compared ignoring case), a Regexp or an Array of them.
1204
+ def host_matches?(hosts)
1205
+ host = @request.host.to_s
1206
+ Array(hosts).any? { |pattern| pattern.is_a?(String) ? pattern.casecmp?(host) : pattern === host }
1207
+ end
1208
+
1185
1209
  def captures_from_params(params)
1186
1210
  if params[:captures].instance_of?(Array) && !params[:captures].empty?
1187
1211
  params.delete(:captures)
@@ -59,9 +59,9 @@ module Padrino
59
59
  # gives for it, matches. An :except filter is skipped only if every one of them matches
60
60
  # exactly, and none has a newline. +info+ is the path_entry of path_info, when the caller has it.
61
61
  def match_path?(argument, request, settings, paths, info = nil)
62
- return paths.all? { |path| !path.include?("\n") && argument === path } unless @mode
62
+ return paths.all? { |path| !path.include?("\n") && Routing.path_match?(argument, path) } unless @mode
63
63
 
64
- filter_paths(request, settings, paths, info || path_entry(request)).any? { |form| argument === form }
64
+ filter_paths(request, settings, paths, info || path_entry(request)).any? { |form| Routing.path_match?(argument, form) }
65
65
  end
66
66
 
67
67
  # +paths+ and the forms route_forms gives for each, once per request for each route run and
@@ -154,7 +154,7 @@ module Padrino
154
154
  files.delete(file)
155
155
  loaded = true
156
156
  rescue NameError, LoadError => error
157
- raise if Reloader.exclude.any? { |path| file.start_with?(path) } || options[:cyclic] == false
157
+ raise if Reloader.send(:path_excluded?, file) || options[:cyclic] == false
158
158
  logger.devel "Cyclic dependency reload for #{error.class}: #{error.message}"
159
159
  rescue Exception => fatal
160
160
  break
@@ -358,8 +358,13 @@ module Padrino
358
358
  }
359
359
  Config.merge!(PADRINO_LOGGER) if PADRINO_LOGGER
360
360
 
361
+ # Built when this file loads, so threads that log for the first time at once build each
362
+ # class's mutex one at a time and share it.
363
+ MUTEX_INIT = Mutex.new # @private
364
+ private_constant :MUTEX_INIT
365
+
361
366
  def self.mutex
362
- @_mutex ||= Mutex.new
367
+ (@_mutex if defined?(@_mutex)) || MUTEX_INIT.synchronize { @_mutex ||= Mutex.new }
363
368
  end
364
369
 
365
370
  def self.logger
@@ -20,7 +20,7 @@ module Padrino
20
20
  return if compiled?
21
21
  @routes.each_with_index do |route, index|
22
22
  route.index = index
23
- route.regexp = /\A(?<_#{index}>#{route.matcher.to_regexp})\z/
23
+ route.regexp = /\A(?:#{route.matcher.to_regexp})\z/
24
24
  end
25
25
  index_by_prefix!
26
26
  @compiled = true
@@ -69,6 +69,17 @@ module Padrino
69
69
  rotation { |offset| match?(offset, pattern) }
70
70
  end
71
71
 
72
+ ##
73
+ # Finds the first route that matches PATH_INFO, and returns it with its params, read from
74
+ # the path as the route matched it. Returns nil when no route matches.
75
+ #
76
+ def recognize_path(path_info)
77
+ compile! unless compiled?
78
+ pattern = decode_pattern(path_info)
79
+ route = match?(0, pattern)
80
+ [route, route.params_for(pattern, {})] if route
81
+ end
82
+
72
83
  private
73
84
 
74
85
  ##
@@ -92,13 +103,20 @@ module Padrino
92
103
  route = routes[candidates[at]]
93
104
  regexp = route.regexp
94
105
  return route if regexp.match?(path) || (trimmed && regexp.match?(trimmed))
106
+ rescue Encoding::CompatibilityError
107
+ # A Regexp fixed to another encoding (/n with non-ASCII bytes) matches no path it can't
108
+ # read, as in Sinatra (see Routing.path_match?). The same holds in the loop below.
95
109
  end
96
110
  return
97
111
  end
98
112
  while (route = routes[offset])
99
113
  regexp = route.regexp
100
- # A route added after compiling has no regexp and matches nothing.
101
- return route if regexp && (regexp.match?(path) || (trimmed && regexp.match?(trimmed)))
114
+ begin
115
+ # A route added after compiling has no regexp and matches nothing.
116
+ return route if regexp && (regexp.match?(path) || (trimmed && regexp.match?(trimmed)))
117
+ rescue Encoding::CompatibilityError
118
+ nil
119
+ end
102
120
  offset += 1
103
121
  end
104
122
  end
@@ -26,6 +26,9 @@ module Padrino
26
26
  elsif pattern != '/' && pattern.end_with?('/')
27
27
  whole_regexp.match(pattern[0..-2])
28
28
  end
29
+ rescue Encoding::CompatibilityError
30
+ # A Regexp fixed to another encoding doesn't match (see Routing.path_match?).
31
+ nil
29
32
  end
30
33
 
31
34
  # The handler, anchored to the whole path as PathRouter::Compiler matches routes
@@ -37,8 +37,8 @@ module Padrino
37
37
  ##
38
38
  # The accessors will be used in other classes
39
39
  #
40
- attr_accessor :action, :cache, :cache_key, :cache_expires,
41
- :parent, :use_layout, :controller, :user_agent, :path_for_generation, :default_values,
40
+ attr_accessor :action, :cache, :cache_key, :cache_expires, :cache_lookup,
41
+ :parent, :use_layout, :controller, :host, :user_agent, :path_for_generation, :default_values,
42
42
  :provided_formats
43
43
 
44
44
  ##
@@ -79,12 +79,12 @@ module Padrino
79
79
  end
80
80
 
81
81
  ##
82
- # Recognizes route and expanded params from a path.
82
+ # Recognizes route and expanded params from a path, or returns nil when no route matches it.
83
83
  #
84
84
  def recognize_path(path_info)
85
85
  prepare! unless prepared?
86
- route = @engine.find_by_pattern(path_info).first
87
- [route.name, route.params_for(path_info, {})]
86
+ route, params = @engine.recognize_path(path_info)
87
+ [route.name, params] if route
88
88
  end
89
89
 
90
90
  ##
@@ -23,8 +23,8 @@ module Padrino
23
23
  reset_concerns(name)
24
24
  @old_entries ||= {}
25
25
  @old_entries[name] = {
26
- constants: object_classes,
27
- features: old_features = Set.new($LOADED_FEATURES.dup)
26
+ constants: all_classes,
27
+ features: old_features = $LOADED_FEATURES.dup
28
28
  }
29
29
  features = file && file[:features] || []
30
30
  features.each { |feature| Reloader.safe_load(feature, force: true) }
@@ -34,7 +34,7 @@ module Padrino
34
34
  def commit(name)
35
35
  entry = {
36
36
  constants: new_classes(@old_entries[name][:constants]),
37
- features: Set.new($LOADED_FEATURES) - @old_entries[name][:features] - [name]
37
+ features: Set.new($LOADED_FEATURES - @old_entries[name][:features] - [name])
38
38
  }
39
39
  files[name] = entry
40
40
  @old_entries.delete(name)
@@ -68,40 +68,73 @@ module Padrino
68
68
 
69
69
  ObjectSpace.each_object(::Module) do |mod|
70
70
  # Module#=== doesn't call mod, which can be a DeprecatedConstantProxy that warns on any call.
71
- next unless ::ActiveSupport::Concern === mod && mod.name
72
- next unless Object.const_source_location(mod.name)&.first == file
73
- Reloader.reset_concern(mod) if Object.const_get(mod.name).equal?(mod)
71
+ next unless ::ActiveSupport::Concern === mod && (name = Reloader.send(:constant_name, mod))
72
+ next unless Object.const_source_location(name)&.first == file
73
+ Reloader.reset_concern(mod) if Object.const_get(name).equal?(mod)
74
74
  rescue NameError
75
75
  next
76
76
  end
77
77
  end
78
78
 
79
79
  ##
80
- # Returns all the classes in the object space.
80
+ # Returns the classes in the object space (without singleton classes when it walks the class tree).
81
81
  #
82
- def object_classes
83
- klasses = Set.new
84
-
85
- ObjectSpace.each_object(::Class).each do |klass|
86
- if block_given?
87
- if (filtered_class = yield(klass))
88
- klasses << filtered_class
89
- end
90
- else
91
- klasses << klass
82
+ def object_classes(&block)
83
+ klasses = all_classes
84
+ klasses = klasses.filter_map(&block) if block
85
+ Set.new(klasses)
86
+ end
87
+
88
+ ##
89
+ # Follows a method past the versions that a library prepended or wrote in Ruby, to the
90
+ # one Class defines natively, or nil when there is none.
91
+ #
92
+ def native_class_method(method)
93
+ method = method.super_method while method && (method.owner != ::Class || method.source_location)
94
+ method
95
+ end
96
+
97
+ # Ruby's own Class#subclasses (Ruby 3.1+), so a version that ActiveSupport prepends to hide
98
+ # reloaded classes is bypassed. CRuby only: other engines keep the heap scan until their
99
+ # subclass lists are known to be complete.
100
+ SUBCLASSES = if RUBY_ENGINE == 'ruby' && ::Class.method_defined?(:subclasses)
101
+ native_class_method(::Class.instance_method(:subclasses))
102
+ end
103
+ private_constant :SUBCLASSES
104
+
105
+ ##
106
+ # Scans the heap for classes, where the class tree walk isn't used.
107
+ #
108
+ def heap_classes
109
+ ObjectSpace.each_object(::Class).to_a
110
+ end
111
+
112
+ if SUBCLASSES
113
+ ##
114
+ # Walks the class tree down from BasicObject, which is several times faster than
115
+ # scanning the heap. It leaves out singleton classes, which have no constant to remove.
116
+ #
117
+ def all_classes
118
+ classes = [::BasicObject]
119
+ index = 0
120
+ while (klass = classes[index])
121
+ classes.concat(SUBCLASSES.bind_call(klass))
122
+ index += 1
92
123
  end
124
+ classes
93
125
  end
94
-
95
- klasses
126
+ else
127
+ alias all_classes heap_classes
96
128
  end
97
129
 
98
130
  ##
99
131
  # Returns a list of object space classes that are not included in "snapshot".
100
132
  #
101
133
  def new_classes(snapshot)
102
- object_classes do |klass|
103
- snapshot.include?(klass) ? nil : klass
104
- end
134
+ classes = all_classes
135
+ # The snapshot keeps its classes alive, so the same count means no class is new.
136
+ return Set.new if classes.size == snapshot.size
137
+ Set.new(classes - snapshot.to_a)
105
138
  end
106
139
  end
107
140
  end
@@ -142,13 +142,14 @@ module Padrino
142
142
  # Removes the specified class and constant.
143
143
  #
144
144
  def remove_constant(const)
145
+ name = constant_name(const) || return
145
146
  return if constant_excluded?(const)
146
- base, _, object = const.to_s.rpartition('::')
147
+ base, _, object = name.rpartition('::')
147
148
  base = base.empty? ? Object : Inflections.constantize(base)
148
149
  return if base.autoload?(object, false) || !base.const_defined?(object, false)
149
150
  return unless base.const_get(object, false).equal?(const)
150
151
  base.send :remove_const, object
151
- logger.devel "Removed constant #{const} from #{base}"
152
+ logger.devel "Removed constant #{name} from #{constant_name(base)}"
152
153
  rescue NameError
153
154
  # no const to remove
154
155
  end
@@ -162,7 +163,7 @@ module Padrino
162
163
  %i[@_included_block @_prepended_block].each do |ivar|
163
164
  concern.remove_instance_variable(ivar) if concern.instance_variable_defined?(ivar)
164
165
  end
165
- logger.devel "Reset concern #{concern}"
166
+ logger.devel "Reset concern #{constant_name(concern)}"
166
167
  end
167
168
 
168
169
  ##
@@ -291,7 +292,7 @@ module Padrino
291
292
  def rotation
292
293
  files_for_rotation.each do |file|
293
294
  file = File.expand_path(file)
294
- next if Reloader.exclude.any? { |base| file.start_with?(base) } || !File.file?(file)
295
+ next if path_excluded?(file) || !File.file?(file)
295
296
  yield file
296
297
  end
297
298
  nil
@@ -317,7 +318,21 @@ module Padrino
317
318
  # Tells if a feature should be excluded from Reloader tracking.
318
319
  #
319
320
  def feature_excluded?(file)
320
- !file.start_with?(Padrino.root) || exclude.any? { |excluded_path| file.start_with?(excluded_path) }
321
+ !path_inside?(file, Padrino.root) || path_excluded?(file)
322
+ end
323
+
324
+ ##
325
+ # Tells if the file is in one of the excluded paths, or is one of them.
326
+ #
327
+ def path_excluded?(file)
328
+ exclude.any? { |excluded_path| path_inside?(file, excluded_path) }
329
+ end
330
+
331
+ ##
332
+ # Tells if the file is the path itself or inside it, so /app-gem isn't inside /app.
333
+ #
334
+ def path_inside?(file, path)
335
+ file == path || file.start_with?(File.join(path, ''))
321
336
  end
322
337
 
323
338
  ##
@@ -333,34 +348,60 @@ module Padrino
333
348
  end
334
349
 
335
350
  ##
336
- # Tells if a constant is defined only outside of Padrino project path.
337
- # If a constant has any methods defined inside of the project path it's
338
- # considered internal and will be included in further testing.
339
- # A constant without methods (an exception class, say) is judged by
340
- # where its name was defined instead.
351
+ # Tells if a constant belongs to code the reloader doesn't track, so it's kept on reload.
352
+ # Tracked code is in Padrino.root, outside the excluded paths or in an excluded file
353
+ # that was run with +load+ (which runs it again, so its classes are removed).
354
+ #
355
+ # A constant whose name was first defined in untracked code is kept, even when tracked
356
+ # files add methods to it: the untracked file isn't run again, so a removed class would
357
+ # come back without its methods. A tracked file that reopens it runs again on reload,
358
+ # so its edited and added methods apply, but the methods it deletes or renames stay
359
+ # until the app restarts. Ruby records only the first definition of a name, so a class
360
+ # that a tracked file defines before the untracked file that owns it counts as tracked,
361
+ # and a name first defined by eval without a file name, or from a relative path, counts
362
+ # as untracked wherever it runs. A kept class keeps its superclass, so a project file that
363
+ # gives it one that was reloaded raises a superclass mismatch until the app restarts.
364
+ #
365
+ # Otherwise a constant with a method that has no Ruby source (Struct and Data members,
366
+ # an alias of a built-in method) is removed, unless its name has no Ruby source line
367
+ # (C or Java code defined it).
368
+ # Any other constant is kept only when it has methods and all of them were defined in
369
+ # untracked code.
341
370
  #
342
371
  def external_constant?(const)
372
+ file, line = constant_location(const)
373
+ return true if file && untracked_source?(file)
343
374
  sources = object_sources(const)
344
- sources = [constant_source(const)].compact if sources.empty?
345
- # consider methodless constants of unknown origin not external
375
+ # C extensions define their constants at line 0 of the library file.
376
+ return line.to_i < 1 if sources.delete?(nil)
377
+ # consider methodless constants named in tracked code, or of unknown origin, not external
346
378
  return false if sources.empty?
347
- sources.none? { |source| source.start_with?(Padrino.root) }
379
+ sources.all? { |source| untracked_source?(source) }
348
380
  end
349
381
 
350
382
  ##
351
- # Returns the file in which the constant's name was defined, if known.
383
+ # Tells if a file holds code the reloader doesn't track: it's outside Padrino.root,
384
+ # or in an excluded path and was required (a file run with +load+ runs again).
352
385
  #
353
- def constant_source(const)
386
+ def untracked_source?(file)
387
+ !path_inside?(file, Padrino.root) || (path_excluded?(file) && $LOADED_FEATURES.include?(file))
388
+ end
389
+
390
+ ##
391
+ # Returns the file and line where the constant's name was defined, if known.
392
+ #
393
+ def constant_location(const)
354
394
  base, _, object = constant_name(const).to_s.rpartition('::')
355
395
  return if object.empty?
356
396
  base = base.empty? ? Object : Inflections.constantize(base)
357
- base.const_source_location(object)&.first
358
- rescue NameError
397
+ base.const_source_location(object)
398
+ rescue NameError, ArgumentError # JRuby's Java packages raise ArgumentError
359
399
  nil
360
400
  end
361
401
 
362
402
  ##
363
- # Gets all the sources in which target class is defined.
403
+ # Gets all the sources in which target class is defined, with nil for
404
+ # a method that has no Ruby source.
364
405
  #
365
406
  # Note: Method#source_location is for Ruby 1.9.3+ only.
366
407
  #
@@ -370,14 +411,14 @@ module Padrino
370
411
  next unless method_name.is_a?(Symbol)
371
412
 
372
413
  method_object = target.method(method_name)
373
- sources << method_object.source_location.first if method_object.owner == target.singleton_class
414
+ sources << method_object.source_location&.first if method_object.owner == target.singleton_class
374
415
  end
375
416
 
376
417
  target.instance_methods.each do |method_name|
377
418
  next unless method_name.is_a?(Symbol)
378
419
 
379
420
  method_object = target.instance_method(method_name)
380
- sources << method_object.source_location.first if method_object.owner == target
421
+ sources << method_object.source_location&.first if method_object.owner == target
381
422
  end
382
423
  sources
383
424
  end
@@ -395,7 +436,7 @@ module Padrino
395
436
  #
396
437
  def reloadable_apps
397
438
  Padrino.mounted_apps.select do |app|
398
- next unless app.app_file.start_with?(Padrino.root)
439
+ next unless path_inside?(app.app_file, Padrino.root)
399
440
  app.app_obj.respond_to?(:reload) && app.app_obj.reload?
400
441
  end
401
442
  end
@@ -6,7 +6,7 @@
6
6
  #
7
7
  module Padrino
8
8
  # The version constant for the current version of Padrino.
9
- VERSION = '0.18.5' unless defined?(Padrino::VERSION)
9
+ VERSION = '0.18.7' unless defined?(Padrino::VERSION)
10
10
 
11
11
  #
12
12
  # The current Padrino version.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class BrandedItem
4
+ include Branded
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Branded
4
+ extend ActiveSupport::Concern
5
+
6
+ def self.name
7
+ 'Brand'
8
+ end
9
+
10
+ included do
11
+ define_method(:brand) { 'b1' }
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ PADRINO_ROOT = __dir__ unless defined? PADRINO_ROOT
2
+
3
+ class SystemExcludedConstantsDemo < Padrino::Application
4
+ set :reload, true
5
+ end
6
+
7
+ Padrino.load!
@@ -0,0 +1,7 @@
1
+ require_relative '../support/excluded_constants_settings'
2
+
3
+ class ExcludedConstantsAppSettings
4
+ def self.value
5
+ 1
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class ExcludedConstantsSettings
2
+ def self.value
3
+ 1
4
+ end
5
+ end
6
+
7
+ class ExcludedConstantsError < StandardError
8
+ end
@@ -0,0 +1,7 @@
1
+ PADRINO_ROOT = __dir__ unless defined? PADRINO_ROOT
2
+
3
+ class SystemLoadedExcludedDemo < Padrino::Application
4
+ set :reload, true
5
+ end
6
+
7
+ Padrino.load!
@@ -0,0 +1,7 @@
1
+ class LoadedExcludedBase
2
+ def self.value
3
+ 1
4
+ end
5
+ end
6
+
7
+ load File.expand_path('../support/loaded_excluded_classes.rb', __dir__)
@@ -0,0 +1,15 @@
1
+ class LoadedExcludedThing < LoadedExcludedBase
2
+ def self.other_value
3
+ 2
4
+ end
5
+ end
6
+
7
+ class LoadedExcludedPoint < Struct.new(:x, :y)
8
+ def sum
9
+ x + y
10
+ end
11
+ end
12
+
13
+ class LoadedExcludedLabel
14
+ alias label inspect
15
+ end
@@ -0,0 +1,2 @@
1
+ class DbadminChild < DbadminParent
2
+ end
@@ -0,0 +1,2 @@
1
+ class DbadminParent
2
+ end