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'
|
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.
|
|
|
|
# Example:
|
2007-01-15 01:54:50 -05:00
|
|
|
#
|
|
|
|
# 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:
|
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
|
|
|
|
|
|
|
# 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:
|
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:
|
2007-12-14 21:30:17 -05:00
|
|
|
consolidated = parts.inject(::Hash.new(0)) { |h,part| h[part.first] += part.last; h }
|
2009-06-25 16:47:27 -04:00
|
|
|
parts = [: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-06-25 16:47:27 -04:00
|
|
|
end.compact
|
|
|
|
parts = ["0 seconds"] if parts.empty?
|
|
|
|
parts.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
|
|
|
|
|
|
|
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
|