2008-11-23 19:10:20 -05:00
|
|
|
require 'active_support/basic_object'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/array/conversions'
|
2008-11-23 19:10:20 -05:00
|
|
|
|
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
|
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:
|
|
|
|
klass == Duration || super
|
|
|
|
end
|
2007-12-14 21:27:41 -05:00
|
|
|
|
|
|
|
# Returns true if <tt>other</tt> is also a Duration instance with the
|
|
|
|
# same <tt>value</tt>, or if <tt>other == value</tt>.
|
|
|
|
def ==(other)
|
|
|
|
if Duration === other
|
|
|
|
other.value == value
|
|
|
|
else
|
|
|
|
other == value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-15 01:54:50 -05:00
|
|
|
def self.===(other) #:nodoc:
|
|
|
|
other.is_a?(Duration) rescue super
|
|
|
|
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:
|
2007-12-14 21:30:17 -05:00
|
|
|
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?
|
2009-02-27 08:22:39 -05:00
|
|
|
end.compact.to_sentence(:locale => :en)
|
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
|
|
|
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
|
|
|
|
|
|
|
def method_missing(method, *args, &block) #:nodoc:
|
|
|
|
value.send(method, *args)
|
|
|
|
end
|
2007-01-15 01:54:50 -05:00
|
|
|
end
|
2007-10-06 21:07:00 -04:00
|
|
|
end
|