Skip to content

Commit

Permalink
Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance thro…
Browse files Browse the repository at this point in the history
…ugh method_missing
  • Loading branch information
gbuesing committed Apr 12, 2008
1 parent 7e5aa65 commit 0289b0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
27 changes: 18 additions & 9 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -134,7 +134,8 @@ def eql?(other)
# 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
Expand All @@ -144,10 +145,23 @@ def -(other)
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
Expand Down Expand Up @@ -208,13 +222,8 @@ def respond_to?(sym)

# 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
Expand Down

0 comments on commit 0289b0e

Please sign in to comment.