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 /activesupport/lib | |
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
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/string/inflections.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 6 |
2 files changed, 11 insertions, 1 deletions
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' |