aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-12 23:04:17 +0200
committerXavier Noria <fxn@hashref.com>2010-05-12 23:04:17 +0200
commit2203c781a7dfa8b0c8b6c97cd318d941f9fbb26c (patch)
tree49573544181a36d9a2fee792a140a54c5947067e
parent903637f5f0a1a9789fa12da1519e028b9faa37d8 (diff)
downloadrails-2203c781a7dfa8b0c8b6c97cd318d941f9fbb26c.tar.gz
rails-2203c781a7dfa8b0c8b6c97cd318d941f9fbb26c.tar.bz2
rails-2203c781a7dfa8b0c8b6c97cd318d941f9fbb26c.zip
defines prev_(month|year) in Date and Time to ease transition to 1.9, and deprecates last_(month|year)
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/date/calculations.rb21
-rw-r--r--activesupport/lib/active_support/core_ext/time/calculations.rb14
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb26
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb8
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb18
-rw-r--r--railties/guides/source/active_support_core_extensions.textile22
7 files changed, 77 insertions, 34 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 6146cc6a97..7afd9926b5 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.0.0 [beta 4/release candidate] (unreleased)*
+* Defines prev_(month|year) in Date and Time, and deprecates last_(month|year). [fxn]
+
* Aliases Date#sunday to Date#end_of_week. [fxn]
* Backports Date#>> from 1.9 so that calculations do the right thing around the calendar reform. [fxn]
diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb
index 3038729d34..755d96ce91 100644
--- a/activesupport/lib/active_support/core_ext/date/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/date/calculations.rb
@@ -2,6 +2,7 @@ require 'date'
require 'active_support/duration'
require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/object/acts_like'
+require 'active_support/deprecation'
class Date
if RUBY_VERSION < '1.9'
@@ -146,20 +147,30 @@ class Date
advance(:years => years)
end
- # Short-hand for years_ago(1)
- def last_year
- years_ago(1)
+ def last_year # :nodoc:
+ ActiveSupport::Deprecation.warn("Date#last_year has been deprecated, please use Date#prev_year instead", caller)
+ prev_year
end
+ # Shorthand for years_ago(1)
+ def prev_year
+ years_ago(1)
+ end unless method_defined?(:prev_year)
+
# Short-hand for years_since(1)
def next_year
years_since(1)
end unless method_defined?(:next_year)
+ def last_month # :nodoc:
+ ActiveSupport::Deprecation.warn("Date#last_month has been deprecated, please use Date#prev_month instead", caller)
+ prev_month
+ end
+
# Short-hand for months_ago(1)
- def last_month
+ def prev_month
months_ago(1)
- end
+ end unless method_defined?(:prev_month)
# Short-hand for months_since(1)
def next_month
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb
index 2b47ecd543..e27b08ec2e 100644
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb
@@ -2,6 +2,7 @@ require 'active_support/duration'
require 'active_support/core_ext/date/acts_like'
require 'active_support/core_ext/date/calculations'
require 'active_support/core_ext/date_time/conversions'
+require 'active_support/deprecation'
class Time
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
@@ -132,8 +133,13 @@ class Time
advance(:years => years)
end
+ def last_year # :nodoc:
+ ActiveSupport::Deprecation.warn("Time#last_year has been deprecated, please use Time#prev_year instead", caller)
+ prev_year
+ end
+
# Short-hand for years_ago(1)
- def last_year
+ def prev_year
years_ago(1)
end
@@ -142,9 +148,13 @@ class Time
years_since(1)
end
+ def last_month # :nodoc:
+ ActiveSupport::Deprecation.warn("Time#last_month has been deprecated, please use Time#prev_month instead", caller)
+ prev_month
+ end
# Short-hand for months_ago(1)
- def last_month
+ def prev_month
months_ago(1)
end
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index 2b66fd03d0..1bf118e3b7 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -1,7 +1,7 @@
require 'abstract_unit'
require 'active_support/time'
-class DateExtCalculationsTest < Test::Unit::TestCase
+class DateExtCalculationsTest < ActiveSupport::TestCase
def test_to_s
date = Date.new(2005, 2, 21)
assert_equal "2005-02-21", date.to_s
@@ -145,16 +145,20 @@ class DateExtCalculationsTest < Test::Unit::TestCase
assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day
end
- def test_last_year
- assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year
+ def test_last_year_is_deprecated
+ assert_deprecated { Date.today.last_year }
end
- def test_last_year_in_leap_years
- assert_equal Date.new(1999,2,28), Date.new(2000,2,29).last_year
+ def test_prev_year
+ assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year
end
- def test_last_year_in_calendar_reform
- assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year
+ def test_prev_year_in_leap_years
+ assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year
+ end
+
+ def test_prev_year_in_calendar_reform
+ assert_equal Date.new(1582,10,4), Date.new(1583,10,14).prev_year
end
def test_next_year
@@ -225,8 +229,12 @@ class DateExtCalculationsTest < Test::Unit::TestCase
assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month
end
- def test_last_month_on_31st
- assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month
+ def test_last_month_is_deprecated
+ assert_deprecated { Date.today.last_month }
+ end
+
+ def test_prev_month_on_31st
+ assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month
end
def test_yesterday_constructor
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb
index f9af059acd..4780760a19 100644
--- a/activesupport/test/core_ext/date_time_ext_test.rb
+++ b/activesupport/test/core_ext/date_time_ext_test.rb
@@ -127,8 +127,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day
end
- def test_last_year
- assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year
+ def test_prev_year
+ assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year
end
def test_next_year
@@ -200,8 +200,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month
end
- def test_last_month_on_31st
- assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month
+ def test_prev_month_on_31st
+ assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month
end
def test_xmlschema
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 342d6ab577..30ee1d1652 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -1,7 +1,7 @@
require 'abstract_unit'
require 'active_support/time'
-class TimeExtCalculationsTest < Test::Unit::TestCase
+class TimeExtCalculationsTest < ActiveSupport::TestCase
def test_seconds_since_midnight
assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight
assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight
@@ -166,8 +166,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
# assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177)
end
- def test_last_year
- assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year
+ def test_last_year_is_deprecated
+ assert_deprecated { Time.now.last_year }
+ end
+
+ def test_prev_year
+ assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year
end
def test_next_year
@@ -615,8 +619,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month
end
- def test_last_month_on_31st
- assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month
+ def test_last_month_is_deprecated
+ assert_deprecated { Time.now.last_month }
+ end
+
+ def test_prev_month_on_31st
+ assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month
end
def test_xmlschema_is_available
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index b7b5f47eef..fed0c25e8e 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -2660,13 +2660,13 @@ Active Support defines +Date.current+ to be today in the current time zone. That
h5. Named dates
-h6. +last_year+, +next_year+
+h6. +prev_year+, +next_year+
-The methods +last_year+ and +next_year+ return a date with the same day/month in the last or next year:
+In Ruby 1.9 +prev_year+ and +next_year+ return a date with the same day/month in the last or next year:
<ruby>
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
-d.last_year # => Fri, 08 May 2009
+d.prev_year # => Fri, 08 May 2009
d.next_year # => Sun, 08 May 2011
</ruby>
@@ -2674,29 +2674,33 @@ If date is the 29th of February of a leap year, you obtain the 28th:
<ruby>
d = Date.new(2000, 2, 29) # => Tue, 29 Feb 2000
-d.last_year # => Sun, 28 Feb 1999
+d.prev_year # => Sun, 28 Feb 1999
d.next_year # => Wed, 28 Feb 2001
</ruby>
-h6. +last_month+, +next_month+
+Active Support defines these methods as well for Ruby 1.8.
-The methods +last_month+ and +next_month+ return the date with the same day in the last or next month:
+h6. +prev_month+, +next_month+
+
+In Ruby 1.9 +prev_month+ and +next_month+ return the date with the same day in the last or next month:
<ruby>
d = Date.new(2010, 5, 8) # => Sat, 08 May 2010
-d.last_month # => Thu, 08 Apr 2010
+d.prev_month # => Thu, 08 Apr 2010
d.next_month # => Tue, 08 Jun 2010
</ruby>
If such a day does not exist, the last day of the corresponding month is returned:
<ruby>
-Date.new(2000, 5, 31).last_month # => Sun, 30 Apr 2000
-Date.new(2000, 3, 31).last_month # => Tue, 29 Feb 2000
+Date.new(2000, 5, 31).prev_month # => Sun, 30 Apr 2000
+Date.new(2000, 3, 31).prev_month # => Tue, 29 Feb 2000
Date.new(2000, 5, 31).next_month # => Fri, 30 Jun 2000
Date.new(2000, 1, 31).next_month # => Tue, 29 Feb 2000
</ruby>
+Active Support defines these methods as well for Ruby 1.8.
+
h6. +beginning_of_week+, +end_of_week+
The methods +beginning_of_week+ and +end_of_week+ return the dates for the beginning and end of week, assuming weeks start on Monday: