1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Readable Date and DateTime#inspect. Closes #8570.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6933 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2007-06-04 17:49:43 +00:00
parent 3cfb894bac
commit 1312259294
5 changed files with 27 additions and 0 deletions

View file

@ -1,5 +1,7 @@
*SVN*
* Readable Date and DateTime#inspect. #8570 [Geoff Buesing]
* Added proper handling of arrays #8537 [hasmanyjosh]
Before:

View file

@ -14,6 +14,8 @@ module ActiveSupport #:nodoc:
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 @@ module ActiveSupport #:nodoc:
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 file

@ -6,6 +6,8 @@ module ActiveSupport #:nodoc:
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 @@ module ActiveSupport #:nodoc:
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 file

@ -8,6 +8,11 @@ class DateExtCalculationsTest < Test::Unit::TestCase
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 file

@ -12,6 +12,12 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase
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)