Some 1.9 forward compatibility

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7474 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-09-14 00:34:43 +00:00
parent e6941149ab
commit 041b9b8a1c
24 changed files with 116 additions and 87 deletions

View File

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

View File

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

View File

@@ -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
end

View File

@@ -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)

View File

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

View File

@@ -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)

View File

@@ -38,6 +38,7 @@ module Enumerable
#
def sum(identity = 0, &block)
return identity unless size > 0
if block_given?
map(&block).sum
else

View File

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

View File

@@ -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.

View File

@@ -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
end

View File

@@ -51,4 +51,4 @@ class Object
def acts_like?(duck)
respond_to? "acts_like_#{duck}?"
end
end
end

View File

@@ -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
end

View File

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

View File

@@ -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 File

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

View File

@@ -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)

View File

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

View File

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

View File

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

View File

@@ -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 File

@@ -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)

View File

@@ -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?(%(<count>2</count>)), xml
end
end

View File

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

View File

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