Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activesupport / lib / active_support / duration.rb
blobfd9b505c2fa72c4e9074939d8ad12564248a7a1a
1 module ActiveSupport
2   # Provides accurate date and time measurements using Date#advance and 
3   # Time#advance, respectively. It mainly supports the methods on Numeric,
4   # such as in this example:
5   #
6   #   1.month.ago       # equivalent to Time.now.advance(:months => -1)
7   class Duration < BasicObject
8     attr_accessor :value, :parts
9     
10     def initialize(value, parts) #:nodoc:
11       @value, @parts = value, parts
12     end
13     
14     # Adds another Duration or a Numeric to this Duration. Numeric values
15     # are treated as seconds.
16     def +(other)
17       if Duration === other
18         Duration.new(value + other.value, @parts + other.parts)
19       else
20         Duration.new(value + other, @parts + [[:seconds, other]])
21       end
22     end
23     
24     # Subtracts another Duration or a Numeric from this Duration. Numeric
25     # values are treated as seconds.
26     def -(other)
27       self + (-other)
28     end
29     
30     def -@ #:nodoc:
31       Duration.new(-value, parts.map { |type,number| [type, -number] })
32     end
33     
34     def is_a?(klass) #:nodoc:
35       klass == Duration || super
36     end
37     
38     def self.===(other) #:nodoc:
39       other.is_a?(Duration) rescue super
40     end
41     
42     # Calculates a new Time or Date that is as far in the future
43     # as this Duration represents.
44     def since(time = ::Time.now)
45       sum(1, time)
46     end
47     alias :from_now :since
48     
49     # Calculates a new Time or Date that is as far in the past
50     # as this Duration represents.
51     def ago(time = ::Time.now)
52       sum(-1, time)
53     end
54     alias :until :ago
55     
56     def inspect #:nodoc:
57       consolidated = parts.inject(Hash.new(0)) { |h,part| h[part.first] += part.last; h }
58       [:years, :months, :days, :minutes, :seconds].map do |length|
59         n = consolidated[length]
60         "#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero?
61       end.compact.to_sentence
62     end
63     
64     protected
65     
66     def sum(sign, time = ::Time.now) #:nodoc:
67       parts.inject(time) do |t,(type,number)|
68         if t.acts_like?(:time) || t.acts_like?(:date)
69           if type == :seconds
70             t.since(sign * number)
71           else
72             t.advance(type => sign * number)
73           end
74         else
75           raise ArgumentError, "expected a time or date, got #{time.inspect}"
76         end
77       end
78     end
79     
80     private
81     
82     def method_missing(method, *args, &block) #:nodoc:
83       value.send(method, *args)
84     end
85   end
86 end