From 041b9b8a1c1661f90e8e586fddce981bfdb17f11 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 14 Sep 2007 00:34:43 +0000 Subject: Some 1.9 forward compatibility git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7474 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../active_support/core_ext/array/conversions.rb | 12 +++++++----- .../lib/active_support/core_ext/class/removal.rb | 4 ++-- .../active_support/core_ext/date/calculations.rb | 18 ++++++++++-------- .../active_support/core_ext/date/conversions.rb | 12 +++++++----- .../lib/active_support/core_ext/date_time.rb | 8 +++++--- .../core_ext/date_time/conversions.rb | 12 +++++++----- .../lib/active_support/core_ext/enumerable.rb | 1 + .../lib/active_support/core_ext/exception.rb | 4 ++-- .../lib/active_support/core_ext/float/rounding.rb | 6 ++++-- .../lib/active_support/core_ext/name_error.rb | 4 ++-- .../lib/active_support/core_ext/object/misc.rb | 2 +- .../active_support/core_ext/range/conversions.rb | 14 ++++++++------ .../lib/active_support/core_ext/string.rb | 6 ++++-- .../lib/active_support/core_ext/symbol.rb | 22 ++++++++++++---------- .../active_support/core_ext/time/calculations.rb | 12 +++++++----- .../active_support/core_ext/time/conversions.rb | 8 +++++--- activesupport/lib/active_support/dependencies.rb | 14 ++++++++------ activesupport/lib/active_support/deprecation.rb | 4 +++- activesupport/lib/active_support/inflector.rb | 6 +++--- activesupport/lib/active_support/option_merger.rb | 2 +- .../lib/active_support/vendor/builder/xmlbase.rb | 8 ++++---- activesupport/test/core_ext/array_ext_test.rb | 8 ++++---- activesupport/test/core_ext/enumerable_test.rb | 10 ++++++---- activesupport/test/core_ext/file_test.rb | 6 +++--- 24 files changed, 116 insertions(+), 87 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 426e53ea1c..8205843a2b 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -25,12 +25,14 @@ module ActiveSupport #:nodoc: def to_param join '/' end - - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + + def self.included(base) #:nodoc: + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end - + def to_formatted_s(format = :default) case format when :db diff --git a/activesupport/lib/active_support/core_ext/class/removal.rb b/activesupport/lib/active_support/core_ext/class/removal.rb index b217c1957c..0c70e7181a 100644 --- a/activesupport/lib/active_support/core_ext/class/removal.rb +++ b/activesupport/lib/active_support/core_ext/class/removal.rb @@ -17,8 +17,8 @@ class Class #:nodoc: # Skip this class if it does not match the current one bound to this name next unless parent.const_defined?(basename) && klass = parent.const_get(basename) - - parent.send :remove_const, basename unless parent == klass + + parent.instance_eval { remove_const basename } unless parent == klass end end end diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index c28d0c876b..37f5d3b544 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -4,13 +4,15 @@ module ActiveSupport #:nodoc: # Enables the use of time calculations within Time itself module Calculations def self.included(base) #:nodoc: - base.send(:extend, ClassMethods) - - base.send(:alias_method, :plus_without_duration, :+) - base.send(:alias_method, :+, :plus_with_duration) - - base.send(:alias_method, :minus_without_duration, :-) - base.send(:alias_method, :-, :minus_with_duration) + base.extend ClassMethods + + base.instance_eval do + alias_method :plus_without_duration, :+ + alias_method :+, :plus_with_duration + + alias_method :minus_without_duration, :- + alias_method :-, :minus_with_duration + end end module ClassMethods @@ -199,4 +201,4 @@ module ActiveSupport #:nodoc: end end end -end \ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index 6e864aee5e..3491dec25a 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -11,11 +11,13 @@ module ActiveSupport #:nodoc: :rfc822 => "%e %b %Y" } - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) - klass.send(:alias_method, :default_inspect, :inspect) - klass.send(:alias_method, :inspect, :readable_inspect) + def self.included(base) #:nodoc: + base.instance_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + alias_method :default_inspect, :inspect + alias_method :inspect, :readable_inspect + end end def to_formatted_s(format = :default) diff --git a/activesupport/lib/active_support/core_ext/date_time.rb b/activesupport/lib/active_support/core_ext/date_time.rb index c99d1283fd..7ce1b78090 100644 --- a/activesupport/lib/active_support/core_ext/date_time.rb +++ b/activesupport/lib/active_support/core_ext/date_time.rb @@ -3,6 +3,8 @@ require "#{File.dirname(__FILE__)}/time/behavior" require "#{File.dirname(__FILE__)}/date_time/calculations" require "#{File.dirname(__FILE__)}/date_time/conversions" -DateTime.send(:include, ActiveSupport::CoreExtensions::Time::Behavior) -DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Calculations) -DateTime.send(:include, ActiveSupport::CoreExtensions::DateTime::Conversions) +class DateTime + include ActiveSupport::CoreExtensions::Time::Behavior + include ActiveSupport::CoreExtensions::DateTime::Calculations + include ActiveSupport::CoreExtensions::DateTime::Conversions +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 ce0bfd8e0a..91ed0e94e9 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -3,11 +3,13 @@ module ActiveSupport #:nodoc: module DateTime #:nodoc: # Getting datetimes in different convenient string representations and other objects module Conversions - def self.included(klass) - klass.send(:alias_method, :to_datetime_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) - klass.send(:alias_method, :default_inspect, :inspect) - klass.send(:alias_method, :inspect, :readable_inspect) + def self.included(base) + base.class_eval do + alias_method :to_datetime_default_s, :to_s + alias_method :to_s, :to_formatted_s + alias_method :default_inspect, :inspect + alias_method :inspect, :readable_inspect + end end def to_formatted_s(format = :default) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 93372bb4d5..f35c8f86c5 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -38,6 +38,7 @@ module Enumerable # def sum(identity = 0, &block) return identity unless size > 0 + if block_given? map(&block).sum else diff --git a/activesupport/lib/active_support/core_ext/exception.rb b/activesupport/lib/active_support/core_ext/exception.rb index ec15a915be..14cd577066 100644 --- a/activesupport/lib/active_support/core_ext/exception.rb +++ b/activesupport/lib/active_support/core_ext/exception.rb @@ -8,8 +8,8 @@ class Exception # :nodoc: def clean_backtrace backtrace.collect do |line| - Pathname.clean_within(TraceSubstitutions.inject(line) do |line, (regexp, sub)| - line.gsub regexp, sub + Pathname.clean_within(TraceSubstitutions.inject(line) do |result, (regexp, sub)| + result.gsub regexp, sub end) end end diff --git a/activesupport/lib/active_support/core_ext/float/rounding.rb b/activesupport/lib/active_support/core_ext/float/rounding.rb index 989b1f8f80..062d466838 100644 --- a/activesupport/lib/active_support/core_ext/float/rounding.rb +++ b/activesupport/lib/active_support/core_ext/float/rounding.rb @@ -3,8 +3,10 @@ module ActiveSupport #:nodoc: module Float #:nodoc: module Rounding def self.included(base) #:nodoc: - base.send(:alias_method, :round_without_precision, :round) - base.send(:alias_method, :round, :round_with_precision) + base.class_eval do + alias_method :round_without_precision, :round + alias_method :round, :round_with_precision + end end # Rounds the float with the specified precision. diff --git a/activesupport/lib/active_support/core_ext/name_error.rb b/activesupport/lib/active_support/core_ext/name_error.rb index 4af8f22821..49176c12d9 100644 --- a/activesupport/lib/active_support/core_ext/name_error.rb +++ b/activesupport/lib/active_support/core_ext/name_error.rb @@ -1,5 +1,5 @@ # Add a +missing_name+ method to NameError instances. -class NameError < StandardError #:nodoc: +class NameError #:nodoc: # Add a method to obtain the missing name from a NameError. def missing_name $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message @@ -14,4 +14,4 @@ class NameError < StandardError #:nodoc: missing_name == name.to_s end end -end \ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index ea893c54b8..b93db689a7 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -51,4 +51,4 @@ class Object def acts_like?(duck) respond_to? "acts_like_#{duck}?" end -end \ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb index 677ba639ec..77e86a6ae6 100644 --- a/activesupport/lib/active_support/core_ext/range/conversions.rb +++ b/activesupport/lib/active_support/core_ext/range/conversions.rb @@ -6,16 +6,18 @@ module ActiveSupport #:nodoc: DATE_FORMATS = { :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" } } - - def self.included(klass) #:nodoc: - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + + def self.included(base) #:nodoc: + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end - + def to_formatted_s(format = :default) DATE_FORMATS[format] ? DATE_FORMATS[format].call(first, last) : to_default_s end end end end -end \ No newline at end of file +end diff --git a/activesupport/lib/active_support/core_ext/string.rb b/activesupport/lib/active_support/core_ext/string.rb index b9f075522f..ef2f334fe2 100644 --- a/activesupport/lib/active_support/core_ext/string.rb +++ b/activesupport/lib/active_support/core_ext/string.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/string/inflections' require File.dirname(__FILE__) + '/string/conversions' require File.dirname(__FILE__) + '/string/access' require File.dirname(__FILE__) + '/string/starts_ends_with' -require File.dirname(__FILE__) + '/string/iterators' +require File.dirname(__FILE__) + '/string/iterators' unless 'test'.respond_to?(:each_char) require File.dirname(__FILE__) + '/string/unicode' class String #:nodoc: @@ -10,6 +10,8 @@ class String #:nodoc: include ActiveSupport::CoreExtensions::String::Conversions include ActiveSupport::CoreExtensions::String::Inflections include ActiveSupport::CoreExtensions::String::StartsEndsWith - include ActiveSupport::CoreExtensions::String::Iterators + if defined? ActiveSupport::CoreExtensions::String::Iterators + include ActiveSupport::CoreExtensions::String::Iterators + end include ActiveSupport::CoreExtensions::String::Unicode end diff --git a/activesupport/lib/active_support/core_ext/symbol.rb b/activesupport/lib/active_support/core_ext/symbol.rb index a3dc794db4..54f541ad9a 100644 --- a/activesupport/lib/active_support/core_ext/symbol.rb +++ b/activesupport/lib/active_support/core_ext/symbol.rb @@ -1,12 +1,14 @@ -class Symbol - # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: - # - # # The same as people.collect { |p| p.name } - # people.collect(&:name) - # - # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } - # people.select(&:manager?).collect(&:salary) - def to_proc - Proc.new { |*args| args.shift.__send__(self, *args) } +unless :test.respond_to?(:to_proc) + class Symbol + # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: + # + # # The same as people.collect { |p| p.name } + # people.collect(&:name) + # + # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } + # people.select(&:manager?).collect(&:salary) + def to_proc + Proc.new { |*args| args.shift.__send__(self, *args) } + end end end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 507bb5f180..e5302f990b 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -4,12 +4,14 @@ module ActiveSupport #:nodoc: # Enables the use of time calculations within Time itself module Calculations def self.included(base) #:nodoc: - base.extend(ClassMethods) + base.extend ClassMethods - base.send(:alias_method, :plus_without_duration, :+) - base.send(:alias_method, :+, :plus_with_duration) - base.send(:alias_method, :minus_without_duration, :-) - base.send(:alias_method, :-, :minus_with_duration) + base.class_eval do + alias_method :plus_without_duration, :+ + alias_method :+, :plus_with_duration + alias_method :minus_without_duration, :- + alias_method :-, :minus_with_duration + end end module ClassMethods diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index 7829c2e581..742f71e0a6 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -12,9 +12,11 @@ module ActiveSupport #:nodoc: :rfc822 => "%a, %d %b %Y %H:%M:%S %z" } - def self.included(klass) - klass.send(:alias_method, :to_default_s, :to_s) - klass.send(:alias_method, :to_s, :to_formatted_s) + def self.included(base) + base.class_eval do + alias_method :to_default_s, :to_s + alias_method :to_s, :to_formatted_s + end end def to_formatted_s(format = :default) diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index eee2e46865..076df426d7 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -114,7 +114,7 @@ module Dependencies #:nodoc: raise NameError, "#{path.inspect} is not a valid constant name!" unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path - names = path.split('::') + names = path.to_s.split('::') names.shift if names.first.empty? # We can't use defined? because it will invoke const_missing for the parent @@ -411,7 +411,7 @@ protected return false unless qualified_const_defined? const const = $1 if /\A::(.*)\Z/ =~ const.to_s - names = const.split('::') + names = const.to_s.split('::') if names.size == 1 # It's under Object parent = Object else @@ -419,7 +419,7 @@ protected end log "removing constant #{const}" - parent.send :remove_const, names.last + parent.instance_eval { remove_const names.last } return true end @@ -438,9 +438,11 @@ protected end -Object.send(:define_method, :require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) -Object.send(:define_method, :require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) -Object.send(:define_method, :require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) +Object.instance_eval do + define_method(:require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) + define_method(:require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) + define_method(:require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) +end class Module #:nodoc: # Rename the original handler so we can chain it to the new one diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index 2baa667c03..c9f1884da3 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -147,7 +147,9 @@ module ActiveSupport # Stand-in for @request, @attributes, @params, etc which emits deprecation # warnings on any method call (except #inspect). class DeprecatedInstanceVariableProxy #:nodoc: - instance_methods.each { |m| undef_method m unless m =~ /^__/ } + silence_warnings do + instance_methods.each { |m| undef_method m unless m =~ /^__/ } + end def initialize(instance, method, var = "@#{method}") @instance, @method, @var = instance, method, var diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index db6e44e91b..39edf64117 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -270,9 +270,9 @@ module Inflector "#{number}th" else case number.to_i % 10 - when 1: "#{number}st" - when 2: "#{number}nd" - when 3: "#{number}rd" + when 1; "#{number}st" + when 2; "#{number}nd" + when 3; "#{number}rd" else "#{number}th" end end diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index e266d156ce..29119d0dca 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -1,7 +1,7 @@ module ActiveSupport class OptionMerger #:nodoc: instance_methods.each do |method| - undef_method(method) if method !~ /^(__|instance_eval|class)/ + undef_method(method) if method !~ /^(__|instance_eval|class|object_id)/ end def initialize(context, options) diff --git a/activesupport/lib/active_support/vendor/builder/xmlbase.rb b/activesupport/lib/active_support/vendor/builder/xmlbase.rb index b0485636ea..57c8ddfc30 100644 --- a/activesupport/lib/active_support/vendor/builder/xmlbase.rb +++ b/activesupport/lib/active_support/vendor/builder/xmlbase.rb @@ -122,19 +122,19 @@ module Builder end def _capture_outer_self(block) - @self = eval("self", block) + @self = eval('self', block.instance_eval { binding }) end - + def _newline return if @indent == 0 text! "\n" end - + def _indent return if @indent == 0 || @level == 0 text!(" " * (@level * @indent)) end - + def _nested_structures(block) @level += 1 block.call(self) diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 6552ffcbd8..8c2edfc457 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -179,15 +179,15 @@ class ArrayToXmlTests < Test::Unit::TestCase assert_match(/^<\?xml [^>]*/, xml) assert_equal 0, xml.rindex(/<\?xml /) end - + def test_to_xml_with_block xml = [ { :name => "David", :age => 26, :age_in_millis => 820497600000 }, { :name => "Jason", :age => 31, :age_in_millis => BigDecimal.new('1.0') } - ].to_xml(:skip_instruct => true, :indent => 0) do |xml| - xml.count 2 + ].to_xml(:skip_instruct => true, :indent => 0) do |builder| + builder.count 2 end - + assert xml.include?(%(2)), xml end end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 0590846b7b..55fdcf32ce 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -8,8 +8,7 @@ end class EnumerableTests < Test::Unit::TestCase def test_group_by names = %w(marcel sam david jeremy) - klass = Class.new - klass.send(:attr_accessor, :name) + klass = Struct.new(:name) objects = (1..50).inject([]) do |people,| p = klass.new p.name = names.sort_by { rand }.first @@ -38,10 +37,13 @@ class EnumerableTests < Test::Unit::TestCase end def test_nil_sums - assert_raise(TypeError) { [5, 15, nil].sum } + expected_raise = RUBY_VERSION < '1.9.0' ? TypeError : NoMethodError + + assert_raise(expected_raise) { [5, 15, nil].sum } payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ] - assert_raise(TypeError) { payments.sum(&:price) } + assert_raise(expected_raise) { payments.sum(&:price) } + assert_equal 60, payments.sum { |p| p.price.to_i * 2 } end diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb index 77930cd3e9..1102538876 100644 --- a/activesupport/test/core_ext/file_test.rb +++ b/activesupport/test/core_ext/file_test.rb @@ -6,9 +6,9 @@ class AtomicWriteTest < Test::Unit::TestCase contents = "Atomic Text" File.atomic_write(file_name, Dir.pwd) do |file| file.write(contents) - assert !File.exists?(file_name) + assert !File.exist?(file_name) end - assert File.exists?(file_name) + assert File.exist?(file_name) assert_equal contents, File.read(file_name) ensure File.unlink(file_name) rescue nil @@ -20,7 +20,7 @@ class AtomicWriteTest < Test::Unit::TestCase raise "something bad" end rescue - assert !File.exists?(file_name) + assert !File.exist?(file_name) end def file_name -- cgit v1.2.3