2012-12-07 14:13:56 -05:00
|
|
|
require 'active_support/proxy_object'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/array/conversions'
|
2010-01-01 14:57:07 -05:00
|
|
|
require 'active_support/core_ext/object/acts_like'
|
2008-11-23 19:10:20 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
module ActiveSupport
|
2010-08-14 01:13:00 -04:00
|
|
|
# Provides accurate date and time measurements using Date#advance and
|
2010-07-23 16:22:17 -04:00
|
|
|
# Time#advance, respectively. It mainly supports the methods on Numeric.
|
2007-01-15 01:54:50 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# 1.month.ago # equivalent to Time.now.advance(months: -1)
|
2012-12-07 14:13:56 -05:00
|
|
|
class Duration < ProxyObject
|
2007-01-15 01:54:50 -05:00
|
|
|
attr_accessor :value, :parts
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def initialize(value, parts) #:nodoc:
|
|
|
|
@value, @parts = value, parts
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Adds another Duration or a Numeric to this Duration. Numeric values
|
|
|
|
# are treated as seconds.
|
|
|
|
def +(other)
|
|
|
|
if Duration === other
|
|
|
|
Duration.new(value + other.value, @parts + other.parts)
|
|
|
|
else
|
|
|
|
Duration.new(value + other, @parts + [[:seconds, other]])
|
|
|
|
end
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Subtracts another Duration or a Numeric from this Duration. Numeric
|
|
|
|
# values are treated as seconds.
|
|
|
|
def -(other)
|
|
|
|
self + (-other)
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def -@ #:nodoc:
|
|
|
|
Duration.new(-value, parts.map { |type,number| [type, -number] })
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def is_a?(klass) #:nodoc:
|
2010-01-07 03:05:06 -05:00
|
|
|
Duration == klass || value.is_a?(klass)
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2010-05-19 23:43:39 -04:00
|
|
|
alias :kind_of? :is_a?
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Returns +true+ if +other+ is also a Duration instance with the
|
|
|
|
# same +value+, or if <tt>other == value</tt>.
|
2007-12-14 21:27:41 -05:00
|
|
|
def ==(other)
|
|
|
|
if Duration === other
|
|
|
|
other.value == value
|
|
|
|
else
|
|
|
|
other == value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-08 19:24:35 -04:00
|
|
|
def eql?(other)
|
|
|
|
other.is_a?(Duration) && self == other
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def self.===(other) #:nodoc:
|
2010-01-07 03:05:06 -05:00
|
|
|
other.is_a?(Duration)
|
2010-01-07 19:43:25 -05:00
|
|
|
rescue ::NoMethodError
|
2010-01-07 03:05:06 -05:00
|
|
|
false
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Calculates a new Time or Date that is as far in the future
|
|
|
|
# as this Duration represents.
|
2008-04-20 22:57:04 -04:00
|
|
|
def since(time = ::Time.current)
|
2007-01-15 01:54:50 -05:00
|
|
|
sum(1, time)
|
|
|
|
end
|
|
|
|
alias :from_now :since
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
# Calculates a new Time or Date that is as far in the past
|
|
|
|
# as this Duration represents.
|
2008-04-20 22:57:04 -04:00
|
|
|
def ago(time = ::Time.current)
|
2007-01-15 01:54:50 -05:00
|
|
|
sum(-1, time)
|
|
|
|
end
|
|
|
|
alias :until :ago
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def inspect #:nodoc:
|
2013-08-27 23:07:18 -04:00
|
|
|
parts.
|
|
|
|
reduce(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }.
|
|
|
|
sort_by {|unit, _ | [:years, :months, :days, :minutes, :seconds].index(unit)}.
|
|
|
|
map {|unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}"}.
|
2013-08-12 13:40:11 -04:00
|
|
|
to_sentence(:locale => :en)
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2010-12-29 17:20:14 -05:00
|
|
|
def as_json(options = nil) #:nodoc:
|
|
|
|
to_i
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
protected
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2008-04-20 22:57:04 -04:00
|
|
|
def sum(sign, time = ::Time.current) #:nodoc:
|
2007-12-14 21:27:41 -05:00
|
|
|
parts.inject(time) do |t,(type,number)|
|
|
|
|
if t.acts_like?(:time) || t.acts_like?(:date)
|
|
|
|
if type == :seconds
|
|
|
|
t.since(sign * number)
|
|
|
|
else
|
|
|
|
t.advance(type => sign * number)
|
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
else
|
2008-01-02 04:08:14 -05:00
|
|
|
raise ::ArgumentError, "expected a time or date, got #{time.inspect}"
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
private
|
2007-12-14 21:27:41 -05:00
|
|
|
|
2013-11-26 10:03:25 -05:00
|
|
|
# We define it as a workaround to Ruby 2.0.0-p353 bug.
|
|
|
|
# For more information, check rails/rails#13055.
|
|
|
|
# It should be dropped once a new Ruby patch-level
|
|
|
|
# release after 2.0.0-p353 happens.
|
|
|
|
def ===(other) #:nodoc:
|
|
|
|
value === other
|
|
|
|
end
|
|
|
|
|
2007-12-14 21:27:41 -05:00
|
|
|
def method_missing(method, *args, &block) #:nodoc:
|
2010-08-30 15:50:44 -04:00
|
|
|
value.send(method, *args, &block)
|
2007-12-14 21:27:41 -05:00
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-10-06 21:07:00 -04:00
|
|
|
end
|