diff options
author | gbuesing <gbuesing@gmail.com> | 2008-04-12 17:31:29 -0500 |
---|---|---|
committer | gbuesing <gbuesing@gmail.com> | 2008-04-12 17:31:29 -0500 |
commit | 0289b0e7dff1c114a090f905406cc212d9e3570e (patch) | |
tree | eb90f4386c06c059fdfb3cfcdb7fedbae6dc2378 /activesupport | |
parent | 7e5aa6569bc041238aafa5a400758c0a7536da93 (diff) | |
download | rails-0289b0e7dff1c114a090f905406cc212d9e3570e.tar.gz rails-0289b0e7dff1c114a090f905406cc212d9e3570e.tar.bz2 rails-0289b0e7dff1c114a090f905406cc212d9e3570e.zip |
Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/time_with_zone.rb | 27 |
2 files changed, 20 insertions, 9 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 0627c89c53..c28d630f5d 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing] + * TimeWithZone respects config.active_support.use_standard_json_time_format [Geoff Buesing] * Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. [rick] 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 |