Browse files

Add title case method to String to do, e.g., 'action_web_service'.tit…

…lecase # => 'Action Web Service'.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2690 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 4da2d6c commit b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e Marcel Molina committed Oct 19, 2005
View
3 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]
View
6 activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -15,6 +15,12 @@ def singularize
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)
View
6 activesupport/lib/active_support/inflector.rb
@@ -112,6 +112,10 @@ def singularize(word)
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 @@ def ordinalize(number)
end
end
-require File.dirname(__FILE__) + '/inflections'
+require File.dirname(__FILE__) + '/inflections'
View
16 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 @@ def test_pluralize_plurals
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))

0 comments on commit b23c72f

Please sign in to comment.