aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-01-15 16:47:51 +0000
committerJamis Buck <jamis@37signals.com>2007-01-15 16:47:51 +0000
commit8368e160b4acddf0204a84a9f8d360dcd99fe0ce (patch)
tree0301063ce07f033184d5e5ff554fec7ad25d6b8f /activesupport
parentd1917cfea24af7c6a88975d600867a0ba6dd2d69 (diff)
downloadrails-8368e160b4acddf0204a84a9f8d360dcd99fe0ce.tar.gz
rails-8368e160b4acddf0204a84a9f8d360dcd99fe0ce.tar.bz2
rails-8368e160b4acddf0204a84a9f8d360dcd99fe0ce.zip
Make sure Duration uses duck-typing instead of explicit classes, so apps can pass proxy objects around and have it all "just work"
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5948 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/duration.rb7
1 files changed, 4 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index 66b6941c5c..3696697224 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -65,16 +65,17 @@ module ActiveSupport
def sum(sign, time = ::Time.now) #:nodoc:
parts.inject(time) do |t,(type,number)|
- case t
- when ::Time
+ if t.respond_to?(:sec) # quacks like a Time
if type == :seconds
t + (sign * number)
else
t.advance(type => sign * number)
end
- when ::Date
+ elsif t.respond_to?(:day) # quacks like a Date
raise ArgumentError, "Adding seconds to a Date does not make sense" if type == :seconds
t.advance(type => sign * number)
+ else
+ raise ArgumentError, "expected a time or date, got #{time.inspect}"
end
end
end