Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 2690

Show
Ignore:
Timestamp:
10/19/05 20:20:11 (3 years ago)
Author:
marcel
Message:

Add title case method to String to do, e.g., 'action_web_service'.titlecase # => 'Action Web Service'.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r2687 r2690  
     1* Add title case method to String to do, e.g., 'action_web_service'.titlecase #  => 'Action Web Service'. [Marcel Molina Jr.] 
     2 
    13*1.2.1* (October 19th, 2005) 
    24 
     
    46 
    57* Show all framework frames in the framework trace. [Nicholas Seckar] 
    6  
    78 
    89*1.2.0* (October 16th, 2005) 
  • trunk/activesupport/lib/active_support/core_ext/string/inflections.rb

    r1854 r2690  
    1616          Inflector.camelize(self) 
    1717        end 
     18        alias_method :camelcase, :camelize 
     19 
     20        def titleize 
     21          Inflector.titleize(self) 
     22        end 
     23        alias_method :titlecase, :titleize 
    1824 
    1925        def underscore 
  • trunk/activesupport/lib/active_support/inflector.rb

    r2227 r2690  
    113113    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } 
    114114  end 
     115 
     116  def titleize(word) 
     117    humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize } 
     118  end 
    115119   
    116120  def underscore(camel_cased_word) 
  • trunk/activesupport/test/inflector_test.rb

    r2227 r2690  
    145145    "employee_id"     => "Employee", 
    146146    "underground"     => "Underground" 
     147  } 
     148 
     149  MixtureToTitleCase = { 
     150    'active_record'       => 'Active Record', 
     151    'ActiveRecord'        => 'Active Record', 
     152    'action web service'  => 'Action Web Service', 
     153    'Action Web Service'  => 'Action Web Service', 
     154    'Action web service'  => 'Action Web Service', 
     155    'actionwebservice'    => 'Actionwebservice', 
     156    'Actionwebservice'    => 'Actionwebservice' 
    147157  } 
    148158 
     
    197207  end 
    198208 
     209  MixtureToTitleCase.each do |before, title_cased| 
     210    define_method 'test_titlecase' do 
     211      assert_equal(title_cased, Inflector.titleize(before)) 
     212    end 
     213  end 
     214 
    199215  def test_camelize 
    200216    CamelToUnderscore.each do |camel, underscore|