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:
parent
4da2d6c1ed
commit
b23c72fd42
|
@ -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)
|
*1.2.1* (October 19th, 2005)
|
||||||
|
|
||||||
* Classify generated routing code as framework code to avoid appearing in application traces. [Nicholas Seckar]
|
* 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]
|
* Show all framework frames in the framework trace. [Nicholas Seckar]
|
||||||
|
|
||||||
|
|
||||||
*1.2.0* (October 16th, 2005)
|
*1.2.0* (October 16th, 2005)
|
||||||
|
|
||||||
* Update Exception extension to show the first few framework frames in an application trace. [Nicholas Seckar]
|
* Update Exception extension to show the first few framework frames in an application trace. [Nicholas Seckar]
|
||||||
|
|
|
@ -15,6 +15,12 @@ module ActiveSupport #:nodoc:
|
||||||
def camelize
|
def camelize
|
||||||
Inflector.camelize(self)
|
Inflector.camelize(self)
|
||||||
end
|
end
|
||||||
|
alias_method :camelcase, :camelize
|
||||||
|
|
||||||
|
def titleize
|
||||||
|
Inflector.titleize(self)
|
||||||
|
end
|
||||||
|
alias_method :titlecase, :titleize
|
||||||
|
|
||||||
def underscore
|
def underscore
|
||||||
Inflector.underscore(self)
|
Inflector.underscore(self)
|
||||||
|
|
|
@ -113,6 +113,10 @@ module Inflector
|
||||||
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def titleize(word)
|
||||||
|
humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
|
||||||
|
end
|
||||||
|
|
||||||
def underscore(camel_cased_word)
|
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
|
camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
|
||||||
end
|
end
|
||||||
|
|
|
@ -146,6 +146,16 @@ class InflectorTest < Test::Unit::TestCase
|
||||||
"underground" => "Underground"
|
"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 = {
|
OrdinalNumbers = {
|
||||||
"0" => "0th",
|
"0" => "0th",
|
||||||
"1" => "1st",
|
"1" => "1st",
|
||||||
|
@ -196,6 +206,12 @@ class InflectorTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_camelize
|
||||||
CamelToUnderscore.each do |camel, underscore|
|
CamelToUnderscore.each do |camel, underscore|
|
||||||
assert_equal(camel, Inflector.camelize(underscore))
|
assert_equal(camel, Inflector.camelize(underscore))
|
||||||
|
|
Loading…
Reference in New Issue