Skip to content

Commit

Permalink
Merge pull request #5466 from carlosantoniodasilva/as-review
Browse files Browse the repository at this point in the history
Remove deprecation from AS::Deprecation behavior, some minor cleanups
  • Loading branch information
josevalim committed Mar 16, 2012
2 parents 2afe12f + c045eeb commit f5b9dd3
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 71 deletions.
1 change: 0 additions & 1 deletion activesupport/lib/active_support/basic_object.rb
Expand Up @@ -10,5 +10,4 @@ def raise(*args)
::Object.send(:raise, *args)
end
end

end
16 changes: 9 additions & 7 deletions activesupport/lib/active_support/core_ext/module/delegation.rb
Expand Up @@ -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

Expand All @@ -122,18 +124,18 @@ 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)
#{to}.__send__(:#{method}, *args, &block) # client.__send__(:name, *args, &block)
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)
Expand Down
29 changes: 12 additions & 17 deletions activesupport/lib/active_support/dependencies.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions activesupport/lib/active_support/file_update_checker.rb
@@ -1,5 +1,3 @@
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:
Expand Down Expand Up @@ -93,20 +91,19 @@ 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

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
Expand Down
8 changes: 4 additions & 4 deletions activesupport/lib/active_support/log_subscriber.rb
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions activesupport/lib/active_support/notifications/fanout.rb
Expand Up @@ -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

Expand Down
@@ -1,5 +1,3 @@
require 'active_support/core_ext/module/delegation'

module ActiveSupport
module Notifications
class Instrumenter
Expand Down
27 changes: 1 addition & 26 deletions activesupport/lib/active_support/railtie.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions activesupport/test/file_update_checker_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit f5b9dd3

Please sign in to comment.