diff options
Diffstat (limited to 'activesupport/lib')
12 files changed, 101 insertions, 90 deletions
diff --git a/activesupport/lib/active_support/configurable.rb b/activesupport/lib/active_support/configurable.rb index a8aa53a80f..4fb8c7af3f 100644 --- a/activesupport/lib/active_support/configurable.rb +++ b/activesupport/lib/active_support/configurable.rb @@ -37,29 +37,77 @@ module ActiveSupport yield config end - # Allows you to add shortcut so that you don't have to refer to attribute through config. - # Also look at the example for config to contrast. + # Allows you to add shortcut so that you don't have to refer to attribute + # through config. Also look at the example for config to contrast. + # + # Defines both class and instance config accessors. # # class User # include ActiveSupport::Configurable # config_accessor :allowed_access # end # + # User.allowed_access # => nil + # User.allowed_access = false + # User.allowed_access # => false + # # user = User.new + # user.allowed_access # => false # user.allowed_access = true # user.allowed_access # => true # + # User.allowed_access # => false + # + # The attribute name must be a valid method name in Ruby. + # + # class User + # include ActiveSupport::Configurable + # config_accessor :"1_Badname" + # end + # # => NameError: invalid config attribute name + # + # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>. + # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>. + # + # class User + # include ActiveSupport::Configurable + # config_accessor :allowed_access, instance_reader: false, instance_writer: false + # end + # + # User.allowed_access = false + # User.allowed_access # => false + # + # User.new.allowed_access = true # => NoMethodError + # User.new.allowed_access # => NoMethodError + # + # Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods. + # + # class User + # include ActiveSupport::Configurable + # config_accessor :allowed_access, instance_accessor: false + # end + # + # User.allowed_access = false + # User.allowed_access # => false + # + # User.new.allowed_access = true # => NoMethodError + # User.new.allowed_access # => NoMethodError def config_accessor(*names) options = names.extract_options! names.each do |name| + raise NameError.new('invalid config attribute name') unless name =~ /^[_A-Za-z]\w*$/ + reader, line = "def #{name}; config.#{name}; end", __LINE__ writer, line = "def #{name}=(value); config.#{name} = value; end", __LINE__ singleton_class.class_eval reader, __FILE__, line singleton_class.class_eval writer, __FILE__, line - class_eval reader, __FILE__, line unless options[:instance_reader] == false - class_eval writer, __FILE__, line unless options[:instance_writer] == false + + unless options[:instance_accessor] == false + class_eval reader, __FILE__, line unless options[:instance_reader] == false + class_eval writer, __FILE__, line unless options[:instance_writer] == false + end end end end @@ -79,7 +127,6 @@ module ActiveSupport # # user.config.allowed_access # => true # user.config.level # => 1 - # def config @_config ||= self.class.config.inheritable_copy end diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 19925198c0..13d659f52a 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -39,7 +39,7 @@ class DateTime to_default_s end end - alias_method :to_default_s, :to_s unless (instance_methods(false) & [:to_s, 'to_s']).empty? + alias_method :to_default_s, :to_s if instance_methods(false).include?(:to_s) alias_method :to_s, :to_formatted_s # diff --git a/activesupport/lib/active_support/core_ext/load_error.rb b/activesupport/lib/active_support/core_ext/load_error.rb index 8bdfa0c5bc..fe24f3716d 100644 --- a/activesupport/lib/active_support/core_ext/load_error.rb +++ b/activesupport/lib/active_support/core_ext/load_error.rb @@ -6,12 +6,14 @@ class LoadError /^cannot load such file -- (.+)$/i, ] - def path - @path ||= begin - REGEXPS.find do |regex| - message =~ regex + unless method_defined?(:path) + def path + @path ||= begin + REGEXPS.find do |regex| + message =~ regex + end + $1 end - $1 end end diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb index fbef27c76a..39a1240c61 100644 --- a/activesupport/lib/active_support/core_ext/module/delegation.rb +++ b/activesupport/lib/active_support/core_ext/module/delegation.rb @@ -107,7 +107,6 @@ class Module 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 - to = to.to_s prefix, allow_nil = options.values_at(:prefix, :allow_nil) if prefix == true && to =~ /^[^a-z_]/ @@ -125,8 +124,6 @@ class Module line = line.to_i methods.each do |method| - method = method.to_s - # Attribute writer methods only accept one argument. Makes sure []= # methods still accept two arguments. definition = (method =~ /[^\]]=$/) ? 'arg' : '*args, &block' diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 66f3af7002..c9071a73d8 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -168,8 +168,6 @@ module ActiveSupport #:nodoc: end end - # Use const_missing to autoload associations so we don't have to - # require_association when using single-table inheritance. def const_missing(const_name, nesting = nil) klass_name = name.presence || "Object" @@ -220,11 +218,7 @@ module ActiveSupport #:nodoc: raise ArgumentError, "the file name must be a String -- you passed #{file_name.inspect}" end - Dependencies.depend_on(file_name, false, message) - end - - def require_association(file_name) - Dependencies.associate_with(file_name) + Dependencies.depend_on(file_name, message) end def load_dependency(file) @@ -306,20 +300,15 @@ module ActiveSupport #:nodoc: mechanism == :load end - def depend_on(file_name, swallow_load_errors = false, message = "No such file to load -- %s.rb") + def depend_on(file_name, message = "No such file to load -- %s.rb") path = search_for_file(file_name) require_or_load(path || file_name) rescue LoadError => load_error - unless swallow_load_errors - if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1] - raise LoadError.new(message % file_name).copy_blame!(load_error) - end - raise + if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1] + load_error.message.replace(message % file_name) + load_error.copy_blame!(load_error) end - end - - def associate_with(file_name) - depend_on(file_name, true) + raise end def clear diff --git a/activesupport/lib/active_support/file_update_checker.rb b/activesupport/lib/active_support/file_update_checker.rb index 8860636168..48c39d9370 100644 --- a/activesupport/lib/active_support/file_update_checker.rb +++ b/activesupport/lib/active_support/file_update_checker.rb @@ -35,22 +35,11 @@ module ActiveSupport # have directories as keys and the value is an array of extensions to be # watched under that directory. # - # This method must also receive a block that will be called once a path changes. - # - # == Implementation details - # - # This particular implementation checks for added, updated, and removed - # files. Directories lookup are compiled to a glob for performance. - # Therefore, while someone can add new files to the +files+ array after - # initialization (and parts of Rails do depend on this feature), adding - # new directories after initialization is not supported. - # - # Notice that other objects that implement the FileUpdateChecker API may - # not even allow new files to be added after initialization. If this - # is the case, we recommend freezing the +files+ after initialization to - # avoid changes that won't make effect. + # This method must also receive a block that will be called once a path + # changes. The array of files and list of directories cannot be changed + # after FileUpdateChecker has been initialized. def initialize(files, dirs={}, &block) - @files = files + @files = files.freeze @glob = compile_glob(dirs) @block = block diff --git a/activesupport/lib/active_support/i18n_railtie.rb b/activesupport/lib/active_support/i18n_railtie.rb index bbeb8d82c6..f67b221024 100644 --- a/activesupport/lib/active_support/i18n_railtie.rb +++ b/activesupport/lib/active_support/i18n_railtie.rb @@ -9,25 +9,6 @@ module I18n config.i18n.load_path = [] config.i18n.fallbacks = ActiveSupport::OrderedOptions.new - def self.reloader - @reloader ||= ActiveSupport::FileUpdateChecker.new(reloader_paths){ I18n.reload! } - end - - def self.reloader_paths - @reloader_paths ||= [] - end - - # Add <tt>I18n::Railtie.reloader</tt> to ActionDispatch callbacks. Since, at this - # point, no path was added to the reloader, I18n.reload! is not triggered - # on to_prepare callbacks. This will only happen on the config.after_initialize - # callback below. - initializer "i18n.callbacks" do |app| - app.reloaders << I18n::Railtie.reloader - ActionDispatch::Reloader.to_prepare do - I18n::Railtie.reloader.execute_if_updated - end - end - # Set the i18n configuration after initialization since a lot of # configuration is still usually done in application initializers. config.after_initialize do |app| @@ -63,7 +44,9 @@ module I18n init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks) - reloader_paths.concat I18n.load_path + reloader = ActiveSupport::FileUpdateChecker.new(I18n.load_path.dup){ I18n.reload! } + app.reloaders << reloader + ActionDispatch::Reloader.to_prepare { reloader.execute_if_updated } reloader.execute @i18n_inited = true diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index d2a6e1bd82..bea2ca17f1 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -61,7 +61,7 @@ module ActiveSupport @@flushable_loggers = nil log_subscriber.public_methods(false).each do |event| - next if 'call' == event.to_s + next if :call == event notifier.subscribe("#{event}.#{namespace}", log_subscriber) end @@ -114,7 +114,7 @@ module ActiveSupport # def color(text, color, bold=false) return text unless colorize_logging - color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol) + color = self.class.const_get(color.upcase) if color.is_a?(Symbol) bold = bold ? BOLD : "" "#{bold}#{color}#{text}#{CLEAR}" end diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index 87b1d76026..47336d2143 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -133,7 +133,7 @@ module ActiveSupport #:nodoc: # "ÉL QUE SE ENTERÓ".mb_chars.titleize # => "Él Que Se Enteró" # "日本語".mb_chars.titleize # => "日本語" def titleize - chars(downcase.to_s.gsub(/\b('?[\S])/u) { Unicode.upcase($1)}) + chars(downcase.to_s.gsub(/\b('?\S)/u) { Unicode.upcase($1)}) end alias_method :titlecase, :titleize diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 9a52c916ec..14ceb7072e 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -22,8 +22,7 @@ module ActiveSupport end Assertion = MiniTest::Assertion - alias_method :method_name, :name if method_defined? :name - alias_method :method_name, :__name__ if method_defined? :__name__ + alias_method :method_name, :__name__ $tags = {} def self.for_tag(tag) diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index 517926c74d..a6c57cd0ff 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -1,5 +1,4 @@ require 'fileutils' -require 'rails/version' require 'active_support/concern' require 'active_support/core_ext/class/delegating_attributes' require 'active_support/core_ext/string/inflections' @@ -149,26 +148,20 @@ module ActiveSupport end def environment - unless defined? @env - app = "#{$1}.#{$2}" if File.directory?('.git') && `git branch -v` =~ /^\* (\S+)\s+(\S+)/ - - rails = Rails::VERSION::STRING - if File.directory?('vendor/rails/.git') - Dir.chdir('vendor/rails') do - rails += ".#{$1}.#{$2}" if `git branch -v` =~ /^\* (\S+)\s+(\S+)/ - end - end - - ruby = "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL}" - - @env = [app, rails, ruby, RUBY_PLATFORM] * ',' - end - - @env + @env ||= [].tap do |env| + env << "#{$1}.#{$2}" if File.directory?('.git') && `git branch -v` =~ /^\* (\S+)\s+(\S+)/ + env << rails_version if defined?(Rails::VERSION::STRING) + env << "#{RUBY_ENGINE}-#{RUBY_VERSION}.#{RUBY_PATCHLEVEL}" + env << RUBY_PLATFORM + end.join(',') end protected - HEADER = 'measurement,created_at,app,rails,ruby,platform' + if defined?(Rails::VERSION::STRING) + HEADER = 'measurement,created_at,app,rails,ruby,platform' + else + HEADER = 'measurement,created_at,app,ruby,platform' + end def with_output_file fname = output_filename @@ -186,6 +179,18 @@ module ActiveSupport def output_filename "#{super}.csv" end + + def rails_version + "rails-#{Rails::VERSION::STRING}#{rails_branch}" + end + + def rails_branch + if File.directory?('vendor/rails/.git') + Dir.chdir('vendor/rails') do + ".#{$1}.#{$2}" if `git branch -v` =~ /^\* (\S+)\s+(\S+)/ + end + end + end end module Metrics diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 88e18f6fff..88f9acb588 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -83,7 +83,7 @@ module ActiveSupport if name.is_a?(Module) @backend = name else - require "active_support/xml_mini/#{name.to_s.downcase}" + require "active_support/xml_mini/#{name.downcase}" @backend = ActiveSupport.const_get("XmlMini_#{name}") end end |