2007-01-15 01:54:50 -05:00
|
|
|
module ActiveSupport
|
|
|
|
# Provides accurate date and time measurements using Date#advance and
|
|
|
|
# Time#advance, respectively. It mainly supports the methods on Numeric,
|
|
|
|
# such as in this example:
|
|
|
|
#
|
|
|
|
# 1.month.ago # equivalent to Time.now.advance(:months => -1)
|
2007-10-06 21:07:00 -04:00
|
|
|
class Duration < BasicObject
|
2007-01-15 01:54:50 -05:00
|
|
|
attr_accessor :value, :parts
|
|
|
|
|
|
|
|
def initialize(value, parts) #:nodoc:
|
|
|
|
@value, @parts = value, parts
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# Subtracts another Duration or a Numeric from this Duration. Numeric
|
|
|
|
# values are treated as seconds.
|
|
|
|
def -(other)
|
|
|
|
self + (-other)
|
|
|
|
end
|
|
|
|
|
|
|
|
def -@ #:nodoc:
|
|
|
|
Duration.new(-value, parts.map { |type,number| [type, -number] })
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_a?(klass) #:nodoc:
|
|
|
|
klass == Duration || super
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.===(other) #:nodoc:
|
|
|
|
other.is_a?(Duration) rescue super
|
|
|
|
end
|
|
|
|
|
|
|
|
# Calculates a new Time or Date that is as far in the future
|
|
|
|
# as this Duration represents.
|
|
|
|
def since(time = ::Time.now)
|
|
|
|
sum(1, time)
|
|
|
|
end
|
|
|
|
alias :from_now :since
|
|
|
|
|
|
|
|
# Calculates a new Time or Date that is as far in the past
|
|
|
|
# as this Duration represents.
|
|
|
|
def ago(time = ::Time.now)
|
|
|
|
sum(-1, time)
|
|
|
|
end
|
|
|
|
alias :until :ago
|
|
|
|
|
|
|
|
def inspect #:nodoc:
|
|
|
|
consolidated = parts.inject(Hash.new(0)) { |h,part| h[part.first] += part.last; h }
|
2007-01-23 00:32:08 -05:00
|
|
|
[:years, :months, :days, :minutes, :seconds].map do |length|
|
2007-01-15 01:54:50 -05:00
|
|
|
n = consolidated[length]
|
|
|
|
"#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
|
|
|
|
end.compact.to_sentence
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def sum(sign, time = ::Time.now) #:nodoc:
|
|
|
|
parts.inject(time) do |t,(type,number)|
|
2007-06-04 18:00:53 -04:00
|
|
|
if t.acts_like?(:time) || t.acts_like?(:date)
|
2007-01-15 01:54:50 -05:00
|
|
|
if type == :seconds
|
2007-05-30 02:21:08 -04:00
|
|
|
t.since(sign * number)
|
2007-01-15 01:54:50 -05:00
|
|
|
else
|
|
|
|
t.advance(type => sign * number)
|
|
|
|
end
|
2007-01-15 11:47:51 -05:00
|
|
|
else
|
|
|
|
raise ArgumentError, "expected a time or date, got #{time.inspect}"
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def method_missing(method, *args, &block) #:nodoc:
|
|
|
|
value.send(method, *args)
|
|
|
|
end
|
|
|
|
end
|
2007-10-06 21:07:00 -04:00
|
|
|
end
|