提交 6c61dbf1 编写于 作者: A Aaron Patterson

Squashed commit of the following:

commit 2683de5da85135e8d9fe48593ff6167db9d64b18
Author: Aaron Patterson <aaron.patterson@gmail.com>
Date:   Fri May 3 11:29:20 2013 -0700

    cannot support infinite ranges right now

commit cebb6acef2c3957f975f6db4afd849e535126253
Author: Aaron Patterson <aaron.patterson@gmail.com>
Date:   Fri May 3 11:26:12 2013 -0700

    reverting infinity comparison

commit 385f7e6b4efd1bf9b89e8d607fcb13e5b03737ea
Author: Aaron Patterson <aaron.patterson@gmail.com>
Date:   Fri May 3 11:23:28 2013 -0700

    Revert "Added ability to compare date/time with infinity"

    This reverts commit 38f28dca.

    Conflicts:
    	activesupport/CHANGELOG.md
    	activesupport/lib/active_support/core_ext/numeric/infinite_comparable.rb
    	activesupport/test/core_ext/date_ext_test.rb
    	activesupport/test/core_ext/date_time_ext_test.rb
    	activesupport/test/core_ext/numeric_ext_test.rb
    	activesupport/test/core_ext/time_ext_test.rb
    	activesupport/test/core_ext/time_with_zone_test.rb

commit 0d799a188dc12b18267fc8421675729917610047
Author: Aaron Patterson <aaron.patterson@gmail.com>
Date:   Fri May 3 11:18:53 2013 -0700

    Revert "Refactor infinite comparable definition a bit"

    This reverts commit dd3360e0.

commit 42dec90e49745bbfae546f0560b8783f6b48b074
Author: Aaron Patterson <aaron.patterson@gmail.com>
Date:   Fri May 3 11:18:47 2013 -0700

    Revert "Require 'active_support/core_ext/module/aliasing' in the infinite_comparable module"

    This reverts commit 7003e71c.
上级 57cfa7a9
......@@ -281,7 +281,6 @@ def test_tsrange_values
tz = ::ActiveRecord::Base.default_timezone
assert_equal Time.send(tz, 2010, 1, 1, 14, 30, 0)..Time.send(tz, 2011, 1, 1, 14, 30, 0), @first_range.ts_range
assert_equal Time.send(tz, 2010, 1, 1, 14, 30, 0)...Time.send(tz, 2011, 1, 1, 14, 30, 0), @second_range.ts_range
assert_equal Time.send(tz, 2010, 1, 1, 14, 30, 0)...Float::INFINITY, @third_range.ts_range
assert_equal(-Float::INFINITY...Float::INFINITY, @fourth_range.ts_range)
assert_equal nil, @empty_range.ts_range
end
......@@ -290,7 +289,6 @@ def test_tstzrange_values
skip "PostgreSQL 9.2 required for range datatypes" unless @connection.supports_ranges?
assert_equal Time.parse('2010-01-01 09:30:00 UTC')..Time.parse('2011-01-01 17:30:00 UTC'), @first_range.tstz_range
assert_equal Time.parse('2010-01-01 09:30:00 UTC')...Time.parse('2011-01-01 17:30:00 UTC'), @second_range.tstz_range
assert_equal Time.parse('2010-01-01 09:30:00 UTC')...Float::INFINITY, @third_range.tstz_range
assert_equal(-Float::INFINITY...Float::INFINITY, @fourth_range.tstz_range)
assert_equal nil, @empty_range.tstz_range
end
......
......@@ -2,5 +2,4 @@
require 'active_support/core_ext/date/calculations'
require 'active_support/core_ext/date/conversions'
require 'active_support/core_ext/date/zones'
require 'active_support/core_ext/date/infinite_comparable'
require 'active_support/core_ext/infinite_comparable'
class Date
include InfiniteComparable
end
......@@ -2,4 +2,3 @@
require 'active_support/core_ext/date_time/calculations'
require 'active_support/core_ext/date_time/conversions'
require 'active_support/core_ext/date_time/zones'
require 'active_support/core_ext/date_time/infinite_comparable'
......@@ -154,4 +154,11 @@ def utc?
def utc_offset
(offset * 86400).to_i
end
# Layers additional behavior on DateTime#<=> so that Time and
# ActiveSupport::TimeWithZone instances can be compared with a DateTime.
def <=>(other)
super other.to_datetime
end
end
require 'active_support/core_ext/infinite_comparable'
class DateTime
include InfiniteComparable
end
require 'active_support/concern'
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/object/try'
module InfiniteComparable
extend ActiveSupport::Concern
included do
alias_method_chain :<=>, :infinity
end
define_method :'<=>_with_infinity' do |other|
if other.class == self.class
public_send :'<=>_without_infinity', other
else
infinite = try(:infinite?)
other_infinite = other.try(:infinite?)
# inf <=> inf
if infinite && other_infinite
infinite <=> other_infinite
# not_inf <=> inf
elsif other_infinite
-other_infinite
# inf <=> not_inf
elsif infinite
infinite
else
conversion = "to_#{self.class.name.downcase}"
other = other.public_send(conversion) if other.respond_to?(conversion)
public_send :'<=>_without_infinity', other
end
end
end
end
require 'active_support/core_ext/numeric/bytes'
require 'active_support/core_ext/numeric/time'
require 'active_support/core_ext/numeric/conversions'
require 'active_support/core_ext/numeric/infinite_comparable'
require 'active_support/core_ext/infinite_comparable'
class Float
include InfiniteComparable
end
class BigDecimal
include InfiniteComparable
end
......@@ -3,4 +3,3 @@
require 'active_support/core_ext/time/conversions'
require 'active_support/core_ext/time/marshal'
require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/time/infinite_comparable'
require 'active_support/core_ext/infinite_comparable'
class Time
include InfiniteComparable
end
......@@ -357,11 +357,6 @@ def test_can_freeze_twice
Date.today.freeze.freeze
end
end
def test_compare_with_infinity
assert_equal(-1, Date.today <=> Float::INFINITY)
assert_equal(1, Date.today <=> -Float::INFINITY)
end
end
class DateExtConversionsTest < ActiveSupport::TestCase
......
......@@ -335,10 +335,3 @@ def with_env_tz(new_tz = 'US/Eastern')
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
class DateTimeExtBehaviorTest < ActiveSupport::TestCase
def test_compare_with_infinity
assert_equal(-1, DateTime.now <=> Float::INFINITY)
assert_equal(1, DateTime.now <=> -Float::INFINITY)
end
end
......@@ -441,57 +441,3 @@ def test_to_s__injected_on_proper_types
assert_equal '1 Million', BigDecimal("1000010").to_s(:human)
end
end
class NumericExtBehaviorTest < ActiveSupport::TestCase
def setup
@inf = BigDecimal.new('Infinity')
end
def test_compare_infinity_with_date
assert_equal(-1, -Float::INFINITY <=> Date.today)
assert_equal(1, Float::INFINITY <=> Date.today)
assert_equal(-1, -@inf <=> Date.today)
assert_equal(1, @inf <=> Date.today)
end
def test_compare_infinty_with_infinty
assert_equal(-1, -Float::INFINITY <=> Float::INFINITY)
assert_equal(1, Float::INFINITY <=> -Float::INFINITY)
assert_equal(0, Float::INFINITY <=> Float::INFINITY)
assert_equal(0, -Float::INFINITY <=> -Float::INFINITY)
assert_equal(-1, -Float::INFINITY <=> BigDecimal::INFINITY)
assert_equal(1, Float::INFINITY <=> -BigDecimal::INFINITY)
assert_equal(0, Float::INFINITY <=> BigDecimal::INFINITY)
assert_equal(0, -Float::INFINITY <=> -BigDecimal::INFINITY)
assert_equal(-1, -BigDecimal::INFINITY <=> Float::INFINITY)
assert_equal(1, BigDecimal::INFINITY <=> -Float::INFINITY)
assert_equal(0, BigDecimal::INFINITY <=> Float::INFINITY)
assert_equal(0, -BigDecimal::INFINITY <=> -Float::INFINITY)
end
def test_compare_infinity_with_time
assert_equal(-1, -Float::INFINITY <=> Time.now)
assert_equal(1, Float::INFINITY <=> Time.now)
assert_equal(-1, -@inf <=> Time.now)
assert_equal(1, @inf <=> Time.now)
end
def test_compare_infinity_with_datetime
assert_equal(-1, -Float::INFINITY <=> DateTime.now)
assert_equal(1, Float::INFINITY <=> DateTime.now)
assert_equal(-1, -@inf <=> DateTime.now)
assert_equal(1, @inf <=> DateTime.now)
end
def test_compare_infinity_with_twz
time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
twz = ActiveSupport::TimeWithZone.new(Time.now, time_zone)
assert_equal(-1, -Float::INFINITY <=> twz)
assert_equal(1, Float::INFINITY <=> twz)
assert_equal(-1, -@inf <=> twz)
assert_equal(1, @inf <=> twz)
end
end
require 'abstract_unit'
require 'active_support/time'
require 'active_support/core_ext/range'
require 'active_support/core_ext/numeric'
class RangeTest < ActiveSupport::TestCase
def test_to_s_from_dates
......@@ -17,7 +16,6 @@ def test_to_s_from_times
def test_date_range
assert_instance_of Range, DateTime.new..DateTime.new
assert_instance_of Range, DateTime::Infinity.new..DateTime::Infinity.new
assert_instance_of Range, DateTime.new..DateTime::Infinity.new
end
def test_overlaps_last_inclusive
......@@ -92,28 +90,4 @@ def test_no_overlaps_on_time
time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
assert !time_range_1.overlaps?(time_range_2)
end
def test_infinite_bounds
time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
time = Time.now
date = Date.today
datetime = DateTime.now
twz = ActiveSupport::TimeWithZone.new(time, time_zone)
infinity1 = Float::INFINITY
infinity2 = BigDecimal.new('Infinity')
[infinity1, infinity2].each do |infinity|
[time, date, datetime, twz].each do |bound|
[time, date, datetime, twz].each do |value|
assert Range.new(bound, infinity).include?(value + 10.years)
assert Range.new(-infinity, bound).include?(value - 10.years)
assert !Range.new(bound, infinity).include?(value - 10.years)
assert !Range.new(-infinity, bound).include?(value + 10.years)
end
end
end
end
end
......@@ -855,10 +855,3 @@ def test_last_quarter_on_31st
assert_equal Time.local(2004, 2, 29), Time.local(2004, 5, 31).last_quarter
end
end
class TimeExtBehaviorTest < ActiveSupport::TestCase
def test_compare_with_infinity
assert_equal(-1, Time.now <=> Float::INFINITY)
assert_equal(1, Time.now <=> -Float::INFINITY)
end
end
......@@ -1118,13 +1118,3 @@ def with_tz_default(tz = nil)
Time.zone = old_tz
end
end
class TimeWithZoneExtBehaviorTest < ActiveSupport::TestCase
def test_compare_with_infinity
time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
twz = ActiveSupport::TimeWithZone.new(Time.now, time_zone)
assert_equal(-1, twz <=> Float::INFINITY)
assert_equal(1, twz <=> -Float::INFINITY)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册