diff options
author | Marcel Molina <marcel@vernix.org> | 2005-10-19 20:20:11 +0000 |
---|---|---|
committer | Marcel Molina <marcel@vernix.org> | 2005-10-19 20:20:11 +0000 |
commit | b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e (patch) | |
tree | 7dbe954938956592ae79a164863457601f2cb8cb | |
parent | 4da2d6c1edee8b31896a206a3ed18e7af605501d (diff) | |
download | rails-b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e.tar.gz rails-b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e.tar.bz2 rails-b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e.zip |
Add title case method to String to do, e.g., 'action_web_service'.titlecase # => 'Action Web Service'.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2690 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 3 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/inflections.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 6 | ||||
-rw-r--r-- | activesupport/test/inflector_test.rb | 16 |
4 files changed, 29 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 1020a96be4..237a713255 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,10 +1,11 @@ +* Add title case method to String to do, e.g., 'action_web_service'.titlecase # => 'Action Web Service'. [Marcel Molina Jr.] + *1.2.1* (October 19th, 2005) * Classify generated routing code as framework code to avoid appearing in application traces. [Nicholas Seckar] * Show all framework frames in the framework trace. [Nicholas Seckar] - *1.2.0* (October 16th, 2005) * Update Exception extension to show the first few framework frames in an application trace. [Nicholas Seckar] diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index 65107afaf9..9b5e9ddfcd 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -15,6 +15,12 @@ module ActiveSupport #:nodoc: def camelize Inflector.camelize(self) end + alias_method :camelcase, :camelize + + def titleize + Inflector.titleize(self) + end + alias_method :titlecase, :titleize def underscore Inflector.underscore(self) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 281ea3f4b9..7009628cb3 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -112,6 +112,10 @@ module Inflector def camelize(lower_case_and_underscored_word) lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } end + + def titleize(word) + humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize } + end def underscore(camel_cased_word) camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase @@ -157,4 +161,4 @@ module Inflector end end -require File.dirname(__FILE__) + '/inflections'
\ No newline at end of file +require File.dirname(__FILE__) + '/inflections' diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 0f398df8ff..cbf8250823 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -146,6 +146,16 @@ class InflectorTest < Test::Unit::TestCase "underground" => "Underground" } + MixtureToTitleCase = { + 'active_record' => 'Active Record', + 'ActiveRecord' => 'Active Record', + 'action web service' => 'Action Web Service', + 'Action Web Service' => 'Action Web Service', + 'Action web service' => 'Action Web Service', + 'actionwebservice' => 'Actionwebservice', + 'Actionwebservice' => 'Actionwebservice' + } + OrdinalNumbers = { "0" => "0th", "1" => "1st", @@ -196,6 +206,12 @@ class InflectorTest < Test::Unit::TestCase end end + MixtureToTitleCase.each do |before, title_cased| + define_method 'test_titlecase' do + assert_equal(title_cased, Inflector.titleize(before)) + end + end + def test_camelize CamelToUnderscore.each do |camel, underscore| assert_equal(camel, Inflector.camelize(underscore)) |