提交 b23c72fd 编写于 作者: M Marcel Molina

Add title case method to String to do, e.g., 'action_web_service'.titlecase # ...

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
上级 4da2d6c1
* 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]
......
......@@ -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)
......
......@@ -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'
\ No newline at end of file
require File.dirname(__FILE__) + '/inflections'
......@@ -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))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册