Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10468: define_equals_for_duration.diff

File define_equals_for_duration.diff, 3.9 kB (added by chuyeow, 9 months ago)
  • test/core_ext/numeric_ext_test.rb

    old new  
    1313    } 
    1414  end 
    1515 
    16   # FIXME: ruby 1.9 
    1716  def test_units 
    1817    @seconds.each do |actual, expected| 
    1918      assert_equal expected, actual 
  • test/core_ext/duration_test.rb

    old new  
    1515    assert_nothing_raised { Date.today - Date.today } 
    1616  end 
    1717 
    18   # FIXME: ruby 1.9 
    1918  def test_plus_with_time 
    2019    assert_equal 1 + 1.second, 1.second + 1, "Duration + Numeric should == Numeric + Duration" 
    2120  end 
  • lib/active_support/duration.rb

    old new  
    66  #   1.month.ago       # equivalent to Time.now.advance(:months => -1) 
    77  class Duration < BasicObject 
    88    attr_accessor :value, :parts 
    9      
     9 
    1010    def initialize(value, parts) #:nodoc: 
    1111      @value, @parts = value, parts 
    1212    end 
    13      
     13 
    1414    # Adds another Duration or a Numeric to this Duration. Numeric values 
    1515    # are treated as seconds. 
    1616    def +(other) 
     
    2020        Duration.new(value + other, @parts + [[:seconds, other]]) 
    2121      end 
    2222    end 
    23      
     23 
    2424    # Subtracts another Duration or a Numeric from this Duration. Numeric 
    2525    # values are treated as seconds. 
    2626    def -(other) 
    2727      self + (-other) 
    2828    end 
    29      
     29 
    3030    def -@ #:nodoc: 
    3131      Duration.new(-value, parts.map { |type,number| [type, -number] }) 
    3232    end 
    33      
     33 
    3434    def is_a?(klass) #:nodoc: 
    3535      klass == Duration || super 
    3636    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 
    3848    def self.===(other) #:nodoc: 
    3949      other.is_a?(Duration) rescue super 
    4050    end 
    41      
     51 
    4252    # Calculates a new Time or Date that is as far in the future 
    4353    # as this Duration represents. 
    4454    def since(time = ::Time.now) 
    4555      sum(1, time) 
    4656    end 
    4757    alias :from_now :since 
    48      
     58 
    4959    # Calculates a new Time or Date that is as far in the past 
    5060    # as this Duration represents. 
    5161    def ago(time = ::Time.now) 
    5262      sum(-1, time) 
    5363    end 
    5464    alias :until :ago 
    55      
     65 
    5666    def inspect #:nodoc: 
    5767      consolidated = parts.inject(Hash.new(0)) { |h,part| h[part.first] += part.last; h } 
    5868      [:years, :months, :days, :minutes, :seconds].map do |length| 
     
    6070        "#{n} #{n == 1 ? length.to_s.singularize : length.to_s}" if n.nonzero? 
    6171      end.compact.to_sentence 
    6272    end 
    63      
     73 
    6474    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 
    7184          else 
    72             t.advance(type => sign * number) 
     85            raise ArgumentError, "expected a time or date, got #{time.inspect}" 
    7386          end 
    74         else 
    75           raise ArgumentError, "expected a time or date, got #{time.inspect}" 
    7687        end 
    7788      end 
    78     end 
    79      
     89 
    8090    private 
    81      
    82     def method_missing(method, *args, &block) #:nodoc: 
    83       value.send(method, *args) 
    84     end 
     91 
     92      def method_missing(method, *args, &block) #:nodoc: 
     93        value.send(method, *args) 
     94      end 
    8595  end 
    8696end