aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/time
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-01-23 22:22:36 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-01-23 22:22:36 +0000
commitac03ad1f78d88f40924225089a4b4bfebc8c74d8 (patch)
treee5216e3eb9b7eed0299b6d9dd1b432fd4a68b4c7 /activesupport/lib/active_support/core_ext/time
parent213fac6f49532e7ee08348542e80465088b9bf5d (diff)
downloadrails-ac03ad1f78d88f40924225089a4b4bfebc8c74d8.tar.gz
rails-ac03ad1f78d88f40924225089a4b4bfebc8c74d8.tar.bz2
rails-ac03ad1f78d88f40924225089a4b4bfebc8c74d8.zip
Adding Time and DateTime #compare_with_coercion, which layers behavior on #<=> so that any combination of Time, DateTime and ActiveSupport::TimeWithZone instances can be chronologically compared
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8711 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext/time')
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb17
1 files changed, 17 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 7181bac3fd..534571fe61 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -9,8 +9,12 @@ module ActiveSupport #:nodoc:
base.class_eval do
alias_method :plus_without_duration, :+
alias_method :+, :plus_with_duration
+
alias_method :minus_without_duration, :-
alias_method :-, :minus_with_duration
+
+ alias_method :compare_without_coercion, :<=>
+ alias_method :<=>, :compare_with_coercion
end
end
@@ -218,6 +222,19 @@ module ActiveSupport #:nodoc:
minus_without_duration(other)
end
end
+
+ # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances
+ # can be chronologically compared with a Time
+ def compare_with_coercion(other)
+ # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparision
+ other = other.comparable_time if other.respond_to?(:comparable_time)
+ if other.acts_like?(:date)
+ # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=>
+ to_datetime.compare_without_coercion(other)
+ else
+ compare_without_coercion(other)
+ end
+ end
end
end
end