Skip to content
Browse files

Readable Date and DateTime#inspect. Closes #8570.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6933 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 3cfb894 commit 1312259294e859646d5c1a4d3000f2cd160fae94 @jeremy jeremy committed Jun 4, 2007
View
2 activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Readable Date and DateTime#inspect. #8570 [Geoff Buesing]
+
* Added proper handling of arrays #8537 [hasmanyjosh]
Before:
View
7 activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -14,6 +14,8 @@ module Conversions
def self.included(klass) #:nodoc:
klass.send(:alias_method, :to_default_s, :to_s)
klass.send(:alias_method, :to_s, :to_formatted_s)
+ klass.send(:alias_method, :default_inspect, :inspect)
+ klass.send(:alias_method, :inspect, :readable_inspect)
end
def to_formatted_s(format = :default)
@@ -27,6 +29,11 @@ def to_formatted_s(format = :default)
to_default_s
end
end
+
+ # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
+ def readable_inspect
+ strftime("%a, %d %b %Y")
+ end
# To be able to keep Times, Dates and DateTimes interchangeable on conversions
def to_date
View
7 activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -6,6 +6,8 @@ module Conversions
def self.included(klass)
klass.send(:alias_method, :to_datetime_default_s, :to_s)
klass.send(:alias_method, :to_s, :to_formatted_s)
+ klass.send(:alias_method, :default_inspect, :inspect)
+ klass.send(:alias_method, :inspect, :readable_inspect)
end
def to_formatted_s(format = :default)
@@ -19,6 +21,11 @@ def to_formatted_s(format = :default)
to_datetime_default_s
end
end
+
+ # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005 14:30:00 +0000"
+ def readable_inspect
+ to_s(:rfc822)
+ end
# Converts self to a Ruby Date object; time portion is discarded
def to_date
View
5 activesupport/test/core_ext/date_ext_test.rb
@@ -8,6 +8,11 @@ def test_to_s
assert_equal "2005-02-21", Date.new(2005, 2, 21).to_s(:db)
assert_equal "21 Feb 2005", Date.new(2005, 2, 21).to_s(:rfc822)
end
+
+ def test_readable_inspect
+ assert_equal "Mon, 21 Feb 2005", Date.new(2005, 2, 21).readable_inspect
+ assert_equal Date.new(2005, 2, 21).readable_inspect, Date.new(2005, 2, 21).inspect
+ end
def test_to_time
assert_equal Time.local(2005, 2, 21), Date.new(2005, 2, 21).to_time
View
6 activesupport/test/core_ext/date_time_ext_test.rb
@@ -12,6 +12,12 @@ def test_to_s
assert_equal "February 21st, 2005 14:30", datetime.to_s(:long_ordinal)
end
+ def test_readable_inspect
+ datetime = DateTime.new(2005, 2, 21, 14, 30, 0)
+ assert_equal "Mon, 21 Feb 2005 14:30:00 +0000", datetime.readable_inspect
+ assert_equal datetime.readable_inspect, datetime.inspect
+ end
+
def test_custom_date_format
Time::DATE_FORMATS[:custom] = '%Y%m%d%H%M%S'
assert_equal '20050221143000', DateTime.new(2005, 2, 21, 14, 30, 0).to_s(:custom)

0 comments on commit 1312259

Please sign in to comment.
Something went wrong with that request. Please try again.