From 0289b0e7dff1c114a090f905406cc212d9e3570e Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 17:31:29 -0500 Subject: Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing --- activesupport/lib/active_support/time_with_zone.rb | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 1c984f07d4..f1a2498298 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -134,7 +134,8 @@ module ActiveSupport # If wrapped #time is a DateTime, use DateTime#since instead of #+ # Otherwise, just pass on to #method_missing def +(other) - time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other) + result = utc.acts_like?(:date) ? utc.since(other) : utc + other + result.in_time_zone(time_zone) end # If a time-like object is passed in, compare it with #utc @@ -144,10 +145,23 @@ module ActiveSupport if other.acts_like?(:time) utc - other else - time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other) + result = utc.acts_like?(:date) ? utc.ago(other) : utc - other + result.in_time_zone(time_zone) end end + def since(other) + utc.since(other).in_time_zone(time_zone) + end + + def ago(other) + utc.ago(other).in_time_zone(time_zone) + end + + def advance(options) + utc.advance(options).in_time_zone(time_zone) + end + def usec time.respond_to?(:usec) ? time.usec : 0 end @@ -208,13 +222,8 @@ module ActiveSupport # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone def method_missing(sym, *args, &block) - if %w(+ - since ago advance).include?(sym.to_s) - result = utc.__send__(sym, *args, &block) - result.acts_like?(:time) ? result.in_time_zone(time_zone) : result - else - result = time.__send__(sym, *args, &block) - result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result - end + result = time.__send__(sym, *args, &block) + result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result end private -- cgit v1.2.3