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
This commit is contained in:
Marcel Molina 2005-10-19 20:20:11 +00:00
parent 4da2d6c1ed
commit b23c72fd42
4 changed files with 29 additions and 2 deletions

View File

@ -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 File

@ -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)

View File

@ -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'
require File.dirname(__FILE__) + '/inflections'

View File

@ -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))