aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRobin Dupret <robin.dupret@gmail.com>2014-08-17 16:31:00 +0200
committerRobin Dupret <robin.dupret@gmail.com>2014-08-18 15:43:49 +0200
commiteb73d7dafa343507a60f765c43c748d6987ec652 (patch)
tree4336d688de6658a22a6a039aa1a4d12585104005 /activesupport
parentb30b99c6158766a6b09e8d3b1f91938fc0884605 (diff)
downloadrails-eb73d7dafa343507a60f765c43c748d6987ec652.tar.gz
rails-eb73d7dafa343507a60f765c43c748d6987ec652.tar.bz2
rails-eb73d7dafa343507a60f765c43c748d6987ec652.zip
Define the Duration#instance_of? method
Since Duration is extending from ProxyObject which extends itself from BasicObject, the Duration object doesn't respond to the #instance_of? method. Thus, the #method_missing hook get triggered, delegating the method to its `value` attribute. However, Rubinius' #eql? definition relies on #instance_of?, thus this will equal to true with a Fixnum (since its `value` attribute is a Fixnum) while it should not. The previous behavior was wrong anyway, no matter the implementation.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md7
-rw-r--r--activesupport/lib/active_support/duration.rb6
-rw-r--r--activesupport/test/core_ext/duration_test.rb5
3 files changed, 16 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 96bce53999..88d6e09d4b 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,6 +1,11 @@
+* Add the `Duration#instance_of?` method that was previously delegated to the
+ internal `value` attribute.
+
+ *Robin Dupret*
+
* Fix rounding errors with #travel_to by resetting the usec on any passed time to zero, so we only travel
with per-second precision, not anything deeper than that.
-
+
*DHH*
* Fix ActiveSupport::TestCase not to order users' test cases by default.
diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb
index 0ae641d05b..084d13a9e3 100644
--- a/activesupport/lib/active_support/duration.rb
+++ b/activesupport/lib/active_support/duration.rb
@@ -35,10 +35,14 @@ module ActiveSupport
end
def is_a?(klass) #:nodoc:
- Duration == klass || value.is_a?(klass)
+ instance_of?(klass) || value.is_a?(klass)
end
alias :kind_of? :is_a?
+ def instance_of?(klass) # :nodoc:
+ Duration == klass
+ end
+
# Returns +true+ if +other+ is also a Duration instance with the
# same +value+, or if <tt>other == value</tt>.
def ==(other)
diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb
index 31af3c4521..330d995b7c 100644
--- a/activesupport/test/core_ext/duration_test.rb
+++ b/activesupport/test/core_ext/duration_test.rb
@@ -41,6 +41,11 @@ class DurationTest < ActiveSupport::TestCase
assert !1.eql?(1.second)
end
+ def test_instance_of
+ assert !1.minute.instance_of?(Fixnum)
+ assert !2.days.instance_of?(Fixnum)
+ end
+
def test_inspect
assert_equal '0 seconds', 0.seconds.inspect
assert_equal '1 month', 1.month.inspect