From ff5c7c8c40ba24431ec5c9cdad23ddb9963d2c67 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 31 May 2007 16:37:09 +0000 Subject: [PATCH] Added Date#change (like Time#change) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6910 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ .../active_support/core_ext/date/calculations.rb | 14 ++++++++++++++ .../active_support/core_ext/time/calculations.rb | 2 +- activesupport/test/core_ext/date_ext_test.rb | 5 +++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d3af47bc6a..b0fadc4092 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added Date#change (like Time#change) [DHH] + * DateTime#to_time converts to Time unless out of range. #8512 [Geoff Buesing] * Date#to_datetime, #to_s(:rfc822). #8512 [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index b104e5271a..c522256aea 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -38,6 +38,20 @@ module ActiveSupport #:nodoc: d = d + options.delete(:days) if options[:days] d end + + # Returns a new Date where one or more of the elements have been changed according to the +options+ parameter. + # + # Examples: + # + # Date.new(2007, 5, 12).change(:day => 1) # => Date.new(2007, 5, 12) + # Date.new(2007, 5, 12).change(:year => 2005, :month => 1) # => Date.new(2005, 1, 12) + def change(options) + ::Date.new( + options[:year] || self.year, + options[:month] || self.month, + options[:day] || options[:mday] || self.day # mday is deprecated + ) + end end end end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 1fdfbcdf45..8f9c2ffa70 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -61,7 +61,7 @@ module ActiveSupport #:nodoc: self.utc? ? :utc_time : :local_time, options[:year] || self.year, options[:month] || self.month, - options[:mday] || self.mday, + options[:day] || options[:mday] || self.day, # mday is deprecated options[:hour] || self.hour, options[:min] || (options[:hour] ? 0 : self.min), options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec), diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 3557c11748..f9891c9358 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -21,4 +21,9 @@ class DateExtCalculationsTest < Test::Unit::TestCase def test_to_date assert_equal Date.new(2005, 2, 21), Date.new(2005, 2, 21).to_date end + + def test_change + assert_equal Date.new(2005, 2, 21), Date.new(2005, 2, 11).change(:day => 21) + assert_equal Date.new(2007, 5, 11), Date.new(2005, 2, 11).change(:year => 2007, :month => 5) + end end