提交 c045eebd 编写于 作者: C Carlos Antonio da Silva

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:
  https://github.com/rails/rails/commit/d4c7d3fd94e5a885a6366eaeb3b908bb58ffd4db
* Remove some not used requires
* Refactor delegate to avoid string conversions and if statements inside each block
上级 2afe12f0
......@@ -10,5 +10,4 @@ def raise(*args)
::Object.send(:raise, *args)
end
end
end
......@@ -106,9 +106,11 @@ def delegate(*methods)
unless options.is_a?(Hash) && to = options[:to]
raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
end
prefix, to, allow_nil = options[:prefix], options[:to], options[:allow_nil]
if prefix == true && to.to_s =~ /^[^a-z_]/
to = to.to_s
prefix, allow_nil = options.values_at(:prefix, :allow_nil)
if prefix == true && to =~ /^[^a-z_]/
raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
end
......@@ -122,10 +124,8 @@ def delegate(*methods)
file, line = caller.first.split(':', 2)
line = line.to_i
methods.each do |method|
method = method.to_s
if allow_nil
if allow_nil
methods.each do |method|
module_eval(<<-EOS, file, line - 2)
def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &block)
if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name)
......@@ -133,7 +133,9 @@ def #{method_prefix}#{method}(*args, &block) # def customer_name(*args, &
end # end
end # end
EOS
else
end
else
methods.each do |method|
exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
module_eval(<<-EOS, file, line - 1)
......
......@@ -129,16 +129,14 @@ def new_constants
# Add a set of modules to the watch stack, remembering the initial constants
def watch_namespaces(namespaces)
watching = []
namespaces.map do |namespace|
@watching << namespaces.map do |namespace|
module_name = Dependencies.to_constant_name(namespace)
original_constants = Dependencies.qualified_const_defined?(module_name) ?
Inflector.constantize(module_name).local_constants : []
watching << module_name
@stack[module_name] << original_constants
module_name
end
@watching << watching
end
private
......@@ -365,7 +363,7 @@ def require_or_load(file_name, const_path = nil)
# Record history *after* loading so first load gets warnings.
history << expanded
return result
result
end
# Is the provided constant path defined?
......@@ -434,7 +432,7 @@ def autoload_module!(into, const_name, qualified_name, path_suffix)
mod = Module.new
into.const_set const_name, mod
autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
return mod
mod
end
# 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))
autoloaded_constants.concat newly_defined_paths unless load_once_path?(path)
autoloaded_constants.uniq!
log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty?
return result
result
end
# Return the constant path for the provided parent and constant name.
......@@ -505,7 +503,7 @@ def load_missing_constant(from_mod, const_name)
raise NameError,
"uninitialized constant #{qualified_name}",
caller.reject {|l| l.starts_with? __FILE__ }
caller.reject { |l| l.starts_with? __FILE__ }
end
# Remove the constants that have been autoloaded, and those that have been
......@@ -543,10 +541,7 @@ def get(key)
def safe_get(key)
key = key.name if key.respond_to?(:name)
@store[key] || begin
klass = Inflector.safe_constantize(key)
@store[key] = klass
end
@store[key] ||= Inflector.safe_constantize(key)
end
def store(klass)
......@@ -600,10 +595,10 @@ def will_unload?(const_desc)
def mark_for_unload(const_desc)
name = to_constant_name const_desc
if explicitly_unloadable_constants.include? name
return false
false
else
explicitly_unloadable_constants << name
return true
true
end
end
......@@ -631,10 +626,10 @@ def new_constants_in(*descs)
return new_constants unless aborting
log "Error during loading, removing partially loaded constants "
new_constants.each {|c| remove_constant(c) }.clear
new_constants.each { |c| remove_constant(c) }.clear
end
return []
[]
end
# Convert the provided const desc to a qualified constant name (as a string).
......@@ -663,7 +658,7 @@ def remove_constant(const) #:nodoc:
constantized.before_remove_const if constantized.respond_to?(:before_remove_const)
parent.instance_eval { remove_const to_remove }
return true
true
end
protected
......
require "active_support/core_ext/array/extract_options"
module ActiveSupport
# \FileUpdateChecker specifies the API used by Rails to watch files
# and control reloading. The API depends on four methods:
......@@ -93,10 +91,10 @@ def execute_if_updated
def updated_at #:nodoc:
@updated_at || begin
all = []
all.concat @files.select { |f| File.exists?(f) }
all = @files.select { |f| File.exists?(f) }
all.concat Dir[@glob] if @glob
all.map { |path| File.mtime(path) }.max || Time.at(0)
all.map! { |path| File.mtime(path) }
all.max || Time.at(0)
end
end
......@@ -104,9 +102,8 @@ def compile_glob(hash) #:nodoc:
hash.freeze # Freeze so changes aren't accidently pushed
return if hash.empty?
globs = []
hash.each do |key, value|
globs << "#{escape(key)}/**/*#{compile_ext(value)}"
globs = hash.map do |key, value|
"#{escape(key)}/**/*#{compile_ext(value)}"
end
"{#{globs.join(",")}}"
end
......
......@@ -3,7 +3,7 @@
module ActiveSupport
# ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
# with the sole purpose of logging them. The log subscriber dispatches notifications to
# with the sole purpose of logging them. The log subscriber dispatches notifications to
# a registered object based on its given namespace.
#
# An example would be Active Record log subscriber responsible for logging queries:
......@@ -75,7 +75,8 @@ def flushable_loggers
@@flushable_loggers ||= begin
loggers = log_subscribers.map(&:logger)
loggers.uniq!
loggers.select { |l| l.respond_to?(:flush) }
loggers.select! { |l| l.respond_to?(:flush) }
loggers
end
end
......@@ -101,8 +102,7 @@ def call(message, *args)
%w(info debug warn error fatal unknown).each do |level|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{level}(progname = nil, &block)
return unless logger
logger.#{level}(progname, &block)
logger.#{level}(progname, &block) if logger
end
METHOD
end
......
......@@ -9,15 +9,14 @@ def initialize
end
def subscribe(pattern = nil, block = Proc.new)
subscriber = Subscriber.new(pattern, block).tap do |s|
@subscribers << s
end
subscriber = Subscriber.new(pattern, block)
@subscribers << subscriber
@listeners_for.clear
subscriber
end
def unsubscribe(subscriber)
@subscribers.reject! {|s| s.matches?(subscriber)}
@subscribers.reject! { |s| s.matches?(subscriber) }
@listeners_for.clear
end
......
require 'active_support/core_ext/module/delegation'
module ActiveSupport
module Notifications
class Instrumenter
......
......@@ -8,30 +8,6 @@ class Railtie < Rails::Railtie
initializer "active_support.deprecation_behavior" do |app|
if deprecation = app.config.active_support.deprecation
ActiveSupport::Deprecation.behavior = deprecation
else
defaults = {"development" => :log,
"production" => :notify,
"test" => :stderr}
env = Rails.env
if defaults.key?(env)
msg = "You did not specify how you would like Rails to report " \
"deprecation notices for your #{env} environment, please " \
"set config.active_support.deprecation to :#{defaults[env]} " \
"at config/environments/#{env}.rb"
warn msg
ActiveSupport::Deprecation.behavior = defaults[env]
else
msg = "You did not specify how you would like Rails to report " \
"deprecation notices for your #{env} environment, please " \
"set config.active_support.deprecation to :log, :notify or " \
":stderr at config/environments/#{env}.rb"
warn msg
ActiveSupport::Deprecation.behavior = :stderr
end
end
end
......@@ -42,8 +18,7 @@ class Railtie < Rails::Railtie
zone_default = Time.find_zone!(app.config.time_zone)
unless zone_default
raise \
'Value assigned to config.time_zone not recognized.' +
raise 'Value assigned to config.time_zone not recognized. ' \
'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
end
......
......@@ -83,10 +83,10 @@ def test_should_not_invoke_the_block_if_a_watched_dir_changed_its_glob
def test_should_not_block_if_a_strange_filename_used
FileUtils.mkdir_p("tmp_watcher/valid,yetstrange,path,")
FileUtils.touch(FILES.map { |file_name| "tmp_watcher/valid,yetstrange,path,/#{file_name}" } )
FileUtils.touch(FILES.map { |file_name| "tmp_watcher/valid,yetstrange,path,/#{file_name}" })
test = Thread.new do
ActiveSupport::FileUpdateChecker.new([],"tmp_watcher/valid,yetstrange,path," => :txt){ i += 1 }
ActiveSupport::FileUpdateChecker.new([],"tmp_watcher/valid,yetstrange,path," => :txt) { i += 1 }
Thread.exit
end
test.priority = -1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册