From b23c72fd4220b3d282ec07f63b0571fe5dfc4f0e Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Wed, 19 Oct 2005 20:20:11 +0000 Subject: 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 --- activesupport/lib/active_support/core_ext/string/inflections.rb | 6 ++++++ activesupport/lib/active_support/inflector.rb | 6 +++++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') 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' -- cgit v1.2.3