Changeset 8399
- Timestamp:
- 12/15/07 02:27:41 (5 months ago)
- Files:
-
- trunk/activesupport/lib/active_support/duration.rb (modified) (6 diffs)
- trunk/activesupport/test/core_ext/duration_test.rb (modified) (1 diff)
- trunk/activesupport/test/core_ext/numeric_ext_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/lib/active_support/duration.rb
r7762 r8399 7 7 class Duration < BasicObject 8 8 attr_accessor :value, :parts 9 9 10 10 def initialize(value, parts) #:nodoc: 11 11 @value, @parts = value, parts 12 12 end 13 13 14 14 # Adds another Duration or a Numeric to this Duration. Numeric values 15 15 # are treated as seconds. … … 21 21 end 22 22 end 23 23 24 24 # Subtracts another Duration or a Numeric from this Duration. Numeric 25 25 # values are treated as seconds. … … 27 27 self + (-other) 28 28 end 29 29 30 30 def -@ #:nodoc: 31 31 Duration.new(-value, parts.map { |type,number| [type, -number] }) 32 32 end 33 33 34 34 def is_a?(klass) #:nodoc: 35 35 klass == Duration || super 36 36 end 37 37 38 # Returns true if <tt>other</tt> is also a Duration instance with the 39 # same <tt>value</tt>, or if <tt>other == value</tt>. 40 def ==(other) 41 if Duration === other 42 other.value == value 43 else 44 other == value 45 end 46 end 47 38 48 def self.===(other) #:nodoc: 39 49 other.is_a?(Duration) rescue super 40 50 end 41 51 42 52 # Calculates a new Time or Date that is as far in the future 43 53 # as this Duration represents. … … 46 56 end 47 57 alias :from_now :since 48 58 49 59 # Calculates a new Time or Date that is as far in the past 50 60 # as this Duration represents. … … 53 63 end 54 64 alias :until :ago 55 65 56 66 def inspect #:nodoc: 57 67 consolidated = parts.inject(Hash.new(0)) { |h,part| h[part.first] += part.last; h } … … 61 71 end.compact.to_sentence 62 72 end 63 73 64 74 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) 75 76 def sum(sign, time = ::Time.now) #:nodoc: 77 parts.inject(time) do |t,(type,number)| 78 if t.acts_like?(:time) || t.acts_like?(:date) 79 if type == :seconds 80 t.since(sign * number) 81 else 82 t.advance(type => sign * number) 83 end 71 84 else 72 t.advance(type => sign * number)85 raise ArgumentError, "expected a time or date, got #{time.inspect}" 73 86 end 74 else75 raise ArgumentError, "expected a time or date, got #{time.inspect}"76 87 end 77 88 end 78 end 79 89 80 90 private 81 82 def method_missing(method, *args, &block) #:nodoc:83 value.send(method, *args)84 end91 92 def method_missing(method, *args, &block) #:nodoc: 93 value.send(method, *args) 94 end 85 95 end 86 96 end trunk/activesupport/test/core_ext/duration_test.rb
r7648 r8399 16 16 end 17 17 18 # FIXME: ruby 1.919 18 def test_plus_with_time 20 19 assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration" trunk/activesupport/test/core_ext/numeric_ext_test.rb
r7653 r8399 14 14 end 15 15 16 # FIXME: ruby 1.917 16 def test_units 18 17 @seconds.each do |actual, expected|