Skip to content

Commit c045eeb

Browse files
Remove deprecation from AS::Deprecation behavior, some minor cleanups
* Refactor log subscriber, use select! to avoid a new object * Remove deprecation messages related to AS::Deprecation behavior This was added about 2 years ago for Rails 3: rails@d4c7d3f * Remove some not used requires * Refactor delegate to avoid string conversions and if statements inside each block
1 parent 2afe12f commit c045eeb

File tree

9 files changed

+36
-71
lines changed

9 files changed

+36
-71
lines changed

activesupport/lib/active_support/basic_object.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ def raise(*args)
1010
::Object.send(:raise, *args)
1111
end
1212
end
13-
1413
end

activesupport/lib/active_support/core_ext/module/delegation.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ def delegate(*methods)
106106
unless options.is_a?(Hash) && to = options[:to]
107107
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
108108
end
109-
prefix, to, allow_nil = options[:prefix], options[:to], options[:allow_nil]
110109

111-
if prefix == true && to.to_s =~ /^[^a-z_]/
110+
to = to.to_s
111+
prefix, allow_nil = options.values_at(:prefix, :allow_nil)
112+
113+
if prefix == true && to =~ /^[^a-z_]/
112114
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
113115
end
114116

@@ -122,18 +124,18 @@ def delegate(*methods)
122124
file, line = caller.first.split(':', 2)
123125
line = line.to_i
124126

125-
methods.each do |method|
126-
method = method.to_s
127-
128-
if allow_nil
127+
if allow_nil
128+
methods.each do |method|
129129
module_eval(<<-EOS, file, line - 2)
130130
def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block)
131131
if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name)
132132
#{to}.__send__(:#{method}, *args, &block) # client.__send__(:name, *args, &block)
133133
end # end
134134
end # end
135135
EOS
136-
else
136+
end
137+
else
138+
methods.each do |method|
137139
exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
138140

139141
module_eval(<<-EOS, file, line - 1)

activesupport/lib/active_support/dependencies.rb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,14 @@ def new_constants
129129

130130
# Add a set of modules to the watch stack, remembering the initial constants
131131
def watch_namespaces(namespaces)
132-
watching = []
133-
namespaces.map do |namespace|
132+
@watching << namespaces.map do |namespace|
134133
module_name = Dependencies.to_constant_name(namespace)
135134
original_constants = Dependencies.qualified_const_defined?(module_name) ?
136135
Inflector.constantize(module_name).local_constants : []
137136

138-
watching << module_name
139137
@stack[module_name] << original_constants
138+
module_name
140139
end
141-
@watching << watching
142140
end
143141

144142
private
@@ -365,7 +363,7 @@ def require_or_load(file_name, const_path = nil)
365363

366364
# Record history *after* loading so first load gets warnings.
367365
history << expanded
368-
return result
366+
result
369367
end
370368

371369
# Is the provided constant path defined?
@@ -434,7 +432,7 @@ def autoload_module!(into, const_name, qualified_name, path_suffix)
434432
mod = Module.new
435433
into.const_set const_name, mod
436434
autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
437-
return mod
435+
mod
438436
end
439437

440438
# Load the file at the provided path. +const_paths+ is a set of qualified
@@ -458,7 +456,7 @@ def load_file(path, const_paths = loadable_constants_for_path(path))
458456
autoloaded_constants.concat newly_defined_paths unless load_once_path?(path)
459457
autoloaded_constants.uniq!
460458
log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty?
461-
return result
459+
result
462460
end
463461

464462
# Return the constant path for the provided parent and constant name.
@@ -505,7 +503,7 @@ def load_missing_constant(from_mod, const_name)
505503

506504
raise NameError,
507505
"uninitialized constant #{qualified_name}",
508-
caller.reject {|l| l.starts_with? __FILE__ }
506+
caller.reject { |l| l.starts_with? __FILE__ }
509507
end
510508

511509
# Remove the constants that have been autoloaded, and those that have been
@@ -543,10 +541,7 @@ def get(key)
543541

544542
def safe_get(key)
545543
key = key.name if key.respond_to?(:name)
546-
@store[key] || begin
547-
klass = Inflector.safe_constantize(key)
548-
@store[key] = klass
549-
end
544+
@store[key] ||= Inflector.safe_constantize(key)
550545
end
551546

552547
def store(klass)
@@ -600,10 +595,10 @@ def will_unload?(const_desc)
600595
def mark_for_unload(const_desc)
601596
name = to_constant_name const_desc
602597
if explicitly_unloadable_constants.include? name
603-
return false
598+
false
604599
else
605600
explicitly_unloadable_constants << name
606-
return true
601+
true
607602
end
608603
end
609604

@@ -631,10 +626,10 @@ def new_constants_in(*descs)
631626
return new_constants unless aborting
632627

633628
log "Error during loading, removing partially loaded constants "
634-
new_constants.each {|c| remove_constant(c) }.clear
629+
new_constants.each { |c| remove_constant(c) }.clear
635630
end
636631

637-
return []
632+
[]
638633
end
639634

640635
# Convert the provided const desc to a qualified constant name (as a string).
@@ -663,7 +658,7 @@ def remove_constant(const) #:nodoc:
663658
constantized.before_remove_const if constantized.respond_to?(:before_remove_const)
664659
parent.instance_eval { remove_const to_remove }
665660

666-
return true
661+
true
667662
end
668663

669664
protected

activesupport/lib/active_support/file_update_checker.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require "active_support/core_ext/array/extract_options"
2-
31
module ActiveSupport
42
# \FileUpdateChecker specifies the API used by Rails to watch files
53
# and control reloading. The API depends on four methods:
@@ -93,20 +91,19 @@ def execute_if_updated
9391

9492
def updated_at #:nodoc:
9593
@updated_at || begin
96-
all = []
97-
all.concat @files.select { |f| File.exists?(f) }
94+
all = @files.select { |f| File.exists?(f) }
9895
all.concat Dir[@glob] if @glob
99-
all.map { |path| File.mtime(path) }.max || Time.at(0)
96+
all.map! { |path| File.mtime(path) }
97+
all.max || Time.at(0)
10098
end
10199
end
102100

103101
def compile_glob(hash) #:nodoc:
104102
hash.freeze # Freeze so changes aren't accidently pushed
105103
return if hash.empty?
106104

107-
globs = []
108-
hash.each do |key, value|
109-
globs << "#{escape(key)}/**/*#{compile_ext(value)}"
105+
globs = hash.map do |key, value|
106+
"#{escape(key)}/**/*#{compile_ext(value)}"
110107
end
111108
"{#{globs.join(",")}}"
112109
end

activesupport/lib/active_support/log_subscriber.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module ActiveSupport
55
# ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
6-
# with the sole purpose of logging them. The log subscriber dispatches notifications to
6+
# with the sole purpose of logging them. The log subscriber dispatches notifications to
77
# a registered object based on its given namespace.
88
#
99
# An example would be Active Record log subscriber responsible for logging queries:
@@ -75,7 +75,8 @@ def flushable_loggers
7575
@@flushable_loggers ||= begin
7676
loggers = log_subscribers.map(&:logger)
7777
loggers.uniq!
78-
loggers.select { |l| l.respond_to?(:flush) }
78+
loggers.select! { |l| l.respond_to?(:flush) }
79+
loggers
7980
end
8081
end
8182

@@ -101,8 +102,7 @@ def call(message, *args)
101102
%w(info debug warn error fatal unknown).each do |level|
102103
class_eval <<-METHOD, __FILE__, __LINE__ + 1
103104
def #{level}(progname = nil, &block)
104-
return unless logger
105-
logger.#{level}(progname, &block)
105+
logger.#{level}(progname, &block) if logger
106106
end
107107
METHOD
108108
end

activesupport/lib/active_support/notifications/fanout.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ def initialize
99
end
1010

1111
def subscribe(pattern = nil, block = Proc.new)
12-
subscriber = Subscriber.new(pattern, block).tap do |s|
13-
@subscribers << s
14-
end
12+
subscriber = Subscriber.new(pattern, block)
13+
@subscribers << subscriber
1514
@listeners_for.clear
1615
subscriber
1716
end
1817

1918
def unsubscribe(subscriber)
20-
@subscribers.reject! {|s| s.matches?(subscriber)}
19+
@subscribers.reject! { |s| s.matches?(subscriber) }
2120
@listeners_for.clear
2221
end
2322

activesupport/lib/active_support/notifications/instrumenter.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'active_support/core_ext/module/delegation'
2-
31
module ActiveSupport
42
module Notifications
53
class Instrumenter

activesupport/lib/active_support/railtie.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,6 @@ class Railtie < Rails::Railtie
88
initializer "active_support.deprecation_behavior" do |app|
99
if deprecation = app.config.active_support.deprecation
1010
ActiveSupport::Deprecation.behavior = deprecation
11-
else
12-
defaults = {"development" => :log,
13-
"production" => :notify,
14-
"test" => :stderr}
15-
16-
env = Rails.env
17-
18-
if defaults.key?(env)
19-
msg = "You did not specify how you would like Rails to report " \
20-
"deprecation notices for your #{env} environment, please " \
21-
"set config.active_support.deprecation to :#{defaults[env]} " \
22-
"at config/environments/#{env}.rb"
23-
24-
warn msg
25-
ActiveSupport::Deprecation.behavior = defaults[env]
26-
else
27-
msg = "You did not specify how you would like Rails to report " \
28-
"deprecation notices for your #{env} environment, please " \
29-
"set config.active_support.deprecation to :log, :notify or " \
30-
":stderr at config/environments/#{env}.rb"
31-
32-
warn msg
33-
ActiveSupport::Deprecation.behavior = :stderr
34-
end
3511
end
3612
end
3713

@@ -42,8 +18,7 @@ class Railtie < Rails::Railtie
4218
zone_default = Time.find_zone!(app.config.time_zone)
4319

4420
unless zone_default
45-
raise \
46-
'Value assigned to config.time_zone not recognized.' +
21+
raise 'Value assigned to config.time_zone not recognized. ' \
4722
'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
4823
end
4924

activesupport/test/file_update_checker_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ def test_should_not_invoke_the_block_if_a_watched_dir_changed_its_glob
8383

8484
def test_should_not_block_if_a_strange_filename_used
8585
FileUtils.mkdir_p("tmp_watcher/valid,yetstrange,path,")
86-
FileUtils.touch(FILES.map { |file_name| "tmp_watcher/valid,yetstrange,path,/#{file_name}" } )
86+
FileUtils.touch(FILES.map { |file_name| "tmp_watcher/valid,yetstrange,path,/#{file_name}" })
8787

8888
test = Thread.new do
89-
ActiveSupport::FileUpdateChecker.new([],"tmp_watcher/valid,yetstrange,path," => :txt){ i += 1 }
89+
ActiveSupport::FileUpdateChecker.new([],"tmp_watcher/valid,yetstrange,path," => :txt) { i += 1 }
9090
Thread.exit
9191
end
9292
test.priority = -1

0 commit comments

Comments
 (0)