Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some 1.9 forward compatibility

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7474 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 041b9b8a1c1661f90e8e586fddce981bfdb17f11 1 parent e694114
@jeremy jeremy authored
Showing with 116 additions and 87 deletions.
  1. +7 −5 activesupport/lib/active_support/core_ext/array/conversions.rb
  2. +2 −2 activesupport/lib/active_support/core_ext/class/removal.rb
  3. +10 −8 activesupport/lib/active_support/core_ext/date/calculations.rb
  4. +7 −5 activesupport/lib/active_support/core_ext/date/conversions.rb
  5. +5 −3 activesupport/lib/active_support/core_ext/date_time.rb
  6. +7 −5 activesupport/lib/active_support/core_ext/date_time/conversions.rb
  7. +1 −0  activesupport/lib/active_support/core_ext/enumerable.rb
  8. +2 −2 activesupport/lib/active_support/core_ext/exception.rb
  9. +4 −2 activesupport/lib/active_support/core_ext/float/rounding.rb
  10. +2 −2 activesupport/lib/active_support/core_ext/name_error.rb
  11. +1 −1  activesupport/lib/active_support/core_ext/object/misc.rb
  12. +8 −6 activesupport/lib/active_support/core_ext/range/conversions.rb
  13. +4 −2 activesupport/lib/active_support/core_ext/string.rb
  14. +12 −10 activesupport/lib/active_support/core_ext/symbol.rb
  15. +7 −5 activesupport/lib/active_support/core_ext/time/calculations.rb
  16. +5 −3 activesupport/lib/active_support/core_ext/time/conversions.rb
  17. +8 −6 activesupport/lib/active_support/dependencies.rb
  18. +3 −1 activesupport/lib/active_support/deprecation.rb
  19. +3 −3 activesupport/lib/active_support/inflector.rb
  20. +1 −1  activesupport/lib/active_support/option_merger.rb
  21. +4 −4 activesupport/lib/active_support/vendor/builder/xmlbase.rb
  22. +4 −4 activesupport/test/core_ext/array_ext_test.rb
  23. +6 −4 activesupport/test/core_ext/enumerable_test.rb
  24. +3 −3 activesupport/test/core_ext/file_test.rb
View
12 activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -25,12 +25,14 @@ def to_sentence(options = {})
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
View
4 activesupport/lib/active_support/core_ext/class/removal.rb
@@ -17,8 +17,8 @@ def remove_class(*klasses)
# 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
View
18 activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -4,13 +4,15 @@ module Date #: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 @@ def tomorrow
end
end
end
-end
+end
View
12 activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -11,11 +11,13 @@ module Conversions
: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)
View
8 activesupport/lib/active_support/core_ext/date_time.rb
@@ -3,6 +3,8 @@
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
View
12 activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -3,11 +3,13 @@ module CoreExtensions #: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)
View
1  activesupport/lib/active_support/core_ext/enumerable.rb
@@ -38,6 +38,7 @@ def group_by
#
def sum(identity = 0, &block)
return identity unless size > 0
+
if block_given?
map(&block).sum
else
View
4 activesupport/lib/active_support/core_ext/exception.rb
@@ -8,8 +8,8 @@ def clean_message
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
View
6 activesupport/lib/active_support/core_ext/float/rounding.rb
@@ -3,8 +3,10 @@ module CoreExtensions #: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.
View
4 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 @@ def missing_name?(name)
missing_name == name.to_s
end
end
-end
+end
View
2  activesupport/lib/active_support/core_ext/object/misc.rb
@@ -51,4 +51,4 @@ def with_options(options)
def acts_like?(duck)
respond_to? "acts_like_#{duck}?"
end
-end
+end
View
14 activesupport/lib/active_support/core_ext/range/conversions.rb
@@ -6,16 +6,18 @@ module Conversions
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
+end
View
6 activesupport/lib/active_support/core_ext/string.rb
@@ -2,7 +2,7 @@
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
View
22 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
View
12 activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -4,12 +4,14 @@ module Time #: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
View
8 activesupport/lib/active_support/core_ext/time/conversions.rb
@@ -12,9 +12,11 @@ module Conversions
: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)
View
14 activesupport/lib/active_support/dependencies.rb
@@ -114,7 +114,7 @@ def qualified_const_defined?(path)
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 @@ def remove_constant(const)
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 @@ def remove_constant(const)
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 @@ def log(msg)
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
View
4 activesupport/lib/active_support/deprecation.rb
@@ -147,7 +147,9 @@ def collect_deprecations
# 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
View
6 activesupport/lib/active_support/inflector.rb
@@ -270,9 +270,9 @@ def ordinalize(number)
"#{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
View
2  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)
View
8 activesupport/lib/active_support/vendor/builder/xmlbase.rb
@@ -122,19 +122,19 @@ def _escape_quote(text)
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)
View
8 activesupport/test/core_ext/array_ext_test.rb
@@ -179,15 +179,15 @@ def test_to_with_instruct
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?(%(<count>2</count>)), xml
end
end
View
10 activesupport/test/core_ext/enumerable_test.rb
@@ -8,8 +8,7 @@ def +(p) self.class.new(price + p.price) 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 @@ def test_sums
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
View
6 activesupport/test/core_ext/file_test.rb
@@ -6,9 +6,9 @@ def test_atomic_write_without_errors
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 @@ def test_atomic_write_doesnt_write_when_block_raises
raise "something bad"
end
rescue
- assert !File.exists?(file_name)
+ assert !File.exist?(file_name)
end
def file_name
Please sign in to comment.
Something went wrong with that request. Please try again.