mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #16574 from lsylvester/duration-respond_to
add implementation of respond_to? for ActiveSupport::Duration
This commit is contained in:
commit
c0a4a300f7
3 changed files with 19 additions and 2 deletions
|
@ -7,7 +7,7 @@ module ActiveSupport
|
|||
# Time#advance, respectively. It mainly supports the methods on Numeric.
|
||||
#
|
||||
# 1.month.ago # equivalent to Time.now.advance(months: -1)
|
||||
class Duration < ProxyObject
|
||||
class Duration
|
||||
attr_accessor :value, :parts
|
||||
|
||||
def initialize(value, parts) #:nodoc:
|
||||
|
@ -53,6 +53,10 @@ module ActiveSupport
|
|||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
@value.to_s
|
||||
end
|
||||
|
||||
def eql?(other)
|
||||
other.is_a?(Duration) && self == other
|
||||
end
|
||||
|
@ -89,6 +93,10 @@ module ActiveSupport
|
|||
to_i
|
||||
end
|
||||
|
||||
def respond_to_missing?(method, include_private=false) #:nodoc
|
||||
@value.respond_to?(method, include_private)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def sum(sign, time = ::Time.current) #:nodoc:
|
||||
|
|
|
@ -40,6 +40,10 @@ class DurationTest < ActiveSupport::TestCase
|
|||
assert !(1.day == 'foo')
|
||||
end
|
||||
|
||||
def test_to_s
|
||||
assert_equal "1", 1.second.to_s
|
||||
end
|
||||
|
||||
def test_eql
|
||||
rubinius_skip "Rubinius' #eql? definition relies on #instance_of? " \
|
||||
"which behaves oddly for the sake of backward-compatibility."
|
||||
|
@ -183,4 +187,9 @@ class DurationTest < ActiveSupport::TestCase
|
|||
cased = case 1.day when 1.day then "ok" end
|
||||
assert_equal cased, "ok"
|
||||
end
|
||||
|
||||
def test_respond_to
|
||||
assert_respond_to 1.day, :since
|
||||
assert_respond_to 1.day, :zero?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ require 'active_support/core_ext/object/duplicable'
|
|||
require 'active_support/core_ext/numeric/time'
|
||||
|
||||
class DuplicableTest < ActiveSupport::TestCase
|
||||
RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, 5.seconds, method(:puts)]
|
||||
RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, method(:puts)]
|
||||
ALLOW_DUP = ['1', Object.new, /foo/, [], {}, Time.now, Class.new, Module.new]
|
||||
|
||||
# Needed to support Ruby 1.9.x, as it doesn't allow dup on BigDecimal, instead
|
||||
|
|
Loading…
Reference in a new issue