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

Ticket #7524: added_upto_to_time_calculations.diff

File added_upto_to_time_calculations.diff, 3.3 kB (added by jakev, 2 years ago)

diff file for patch

  • activesupport/test/core_ext/time_ext_test.rb

    old new  
    293293    midnight = Time.local(2005, 2, 21, 0, 0, 0) 
    294294    assert_equal midnight.midnight, (midnight + 1.hour + 0.000001).midnight 
    295295  end 
     296   
     297   
     298  def test_upto_by_days 
     299    time = Time.local(2005, 2, 21, 17, 44, 30) 
     300    end_date = time + 10.days 
     301    counter = 0 
     302    time.upto(end_date) do |date| 
     303      assert_equal date, (time + counter.days) 
     304      counter += 1 
     305    end 
     306   
     307  end 
     308   
     309  def test_upto_by_months 
     310    time = Time.local(2005, 2, 21, 17, 44, 30) 
     311    end_date = time + 10.months 
     312    counter = 0 
     313    time.upto(end_date, :month) do |date| 
     314      assert_equal date, (time + counter.months) 
     315      counter += 1 
     316    end 
     317   
     318  end 
     319   
     320  def test_upto_by_years 
     321    time = Time.local(2005, 2, 21, 17, 44, 30) 
     322    end_date = time + 10.years 
     323    counter = 0 
     324    time.upto(end_date, :year) do |date| 
     325      assert_equal date, (time + counter.years) 
     326      counter += 1 
     327    end 
     328   
     329  end 
     330   
     331  def test_upto_by_seconds 
     332    time = Time.local(2005, 2, 21, 17, 44, 30) 
     333    end_date = time + 10.seconds 
     334    counter = 0 
     335    time.upto(end_date, :seconds) do |date| 
     336      assert_equal date, (time + counter.seconds) 
     337      counter += 1 
     338    end 
     339   
     340  end 
     341   
     342  def test_upto_by_hours 
     343    time = Time.local(2005, 2, 21, 17, 44, 30) 
     344    end_date = time + 10.hours 
     345    counter = 0 
     346    time.upto(end_date, :hours) do |date| 
     347      assert_equal date, (time + counter.hours) 
     348      counter += 1 
     349    end 
     350   
     351  end 
     352   
     353  def test_upto_by_minutes 
     354    time = Time.local(2005, 2, 21, 17, 44, 30) 
     355    end_date = time + 10.minutes 
     356    counter = 0 
     357    time.upto(end_date, :minutes) do |date| 
     358      assert_equal date, (time + counter.minutes) 
     359      counter += 1 
     360    end 
     361   
     362  end 
     363   
     364  def test_upto_with_bad_step_size 
     365    time = Time.local(2005, 2, 21, 17, 44, 30) 
     366    end_date = time + 10.minutes 
     367    assert_raise NoMethodError do 
     368       time.upto(end_date, :non_step_size) do |date| 
     369         puts date 
    296370 
     371       end 
     372    end 
     373     
     374   
     375  end 
     376 
    297377  def test_days_in_month 
    298378    assert_equal 31, Time.days_in_month(1, 2005) 
    299379 
  • activesupport/lib/active_support/core_ext/time/calculations.rb

    old new  
    196196          self.since(1.day) 
    197197        end 
    198198         
     199         
     200         
     201        # Convenience method to step though time in different increments 
     202        # step_sizes = :days, :months, :years, :seconds, :minutes, :hours 
     203        def upto(end_date, step_size = :days) 
     204          start_date = self 
     205          stepper = 1.send(step_size.to_s.pluralize.to_sym) 
     206           
     207          while start_date < end_date 
     208            yield start_date 
     209            start_date += stepper 
     210          end 
     211        rescue NoMethodError => e 
     212          raise NoMethodError("Invalid Step Size") 
     213           
     214        end 
     215         
    199216        def plus_with_duration(other) #:nodoc: 
    200217          if ActiveSupport::Duration === other 
    201218            other.since(self)