aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-09-11 06:14:05 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-09-11 06:14:05 +0000
commitfe4abaa7df98aa736485b6ac863d05d80d2c1d89 (patch)
treeb7149cf54b63e008b667e266ccbe164041a83686 /activesupport
parentb62243a5cc957ae0c7380bf8e020219e45366fe2 (diff)
downloadrails-fe4abaa7df98aa736485b6ac863d05d80d2c1d89.tar.gz
rails-fe4abaa7df98aa736485b6ac863d05d80d2c1d89.tar.bz2
rails-fe4abaa7df98aa736485b6ac863d05d80d2c1d89.zip
Added Array#to_sentence that'll turn ['one', 'two', 'three'] into 'one, two, and three' #2157 [m.stienstra@fngtps.com]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2185 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/array.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/array/conversions.rb22
-rw-r--r--activesupport/lib/active_support/core_ext/array/to_param.rb12
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb14
5 files changed, 40 insertions, 14 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index acf9788285..59e59e37ba 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added Array#to_sentence that'll turn ['one', 'two', 'three'] into "one, two, and three" #2157 [m.stienstra@fngtps.com]
+
* Added Kernel#silence_warnings to turn off warnings temporarily for the passed block
* Added String#starts_with? and String#ends_with? #2118 [thijs@vandervossen.net]
diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb
index 83eef79b38..ddf2a369c1 100644
--- a/activesupport/lib/active_support/core_ext/array.rb
+++ b/activesupport/lib/active_support/core_ext/array.rb
@@ -1,5 +1,5 @@
-require File.dirname(__FILE__) + '/array/to_param'
+require File.dirname(__FILE__) + '/array/conversions'
class Array #:nodoc:
- include ActiveSupport::CoreExtensions::Array::ToParam
+ include ActiveSupport::CoreExtensions::Array::Conversions
end
diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb
new file mode 100644
index 0000000000..19220da2b1
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/array/conversions.rb
@@ -0,0 +1,22 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Array #:nodoc:
+ # Enables to conversion of Arrays to human readable lists. ['one', 'two', 'three'] => "one, two, and three"
+ module Conversions
+ # Converts the array to comma-seperated sentence where the last element is joined by the connector word (default is 'and').
+ def to_sentence(connector = 'and')
+ if length > 1
+ "#{self[0...-1].join(', ')}, #{connector} #{self[-1]}"
+ elsif length == 1
+ self[0]
+ end
+ end
+
+ # When an array is given to url_for, it is converted to a slash separated string.
+ def to_param
+ join '/'
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/core_ext/array/to_param.rb b/activesupport/lib/active_support/core_ext/array/to_param.rb
deleted file mode 100644
index 85e91e6b1a..0000000000
--- a/activesupport/lib/active_support/core_ext/array/to_param.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-module ActiveSupport #:nodoc:
- module CoreExtensions #:nodoc:
- module Array #:nodoc:
- module ToParam #:nodoc:
- # When an array is given to url_for, it is converted to a slash separated string.
- def to_param
- join '/'
- end
- end
- end
- end
-end
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index 3b16c8d2b3..2ee86c8af9 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -12,3 +12,17 @@ class ArrayExtToParamTests < Test::Unit::TestCase
assert_equal '10/20', [10, 20].to_param
end
end
+
+class ArrayExtConversionTests < Test::Unit::TestCase
+ def test_plain_array_to_sentence
+ assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence
+ end
+
+ def test_to_sentence_with_connector
+ assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence('and also')
+ end
+
+ def test_one_element
+ assert_equal "one", ['one'].to_sentence
+ end
+end