Froze rails gems
[depot.git] / vendor / rails / activesupport / lib / active_support / core_ext / string / inflections.rb
1 require 'active_support/inflector'
2
3 module ActiveSupport #:nodoc:
4 module CoreExtensions #:nodoc:
5 module String #:nodoc:
6 # String inflections define new methods on the String class to transform names for different purposes.
7 # For instance, you can figure out the name of a database from the name of a class.
8 #
9 # "ScaleScore".tableize # => "scale_scores"
10 module Inflections
11 # Returns the plural form of the word in the string.
12 #
13 # "post".pluralize # => "posts"
14 # "octopus".pluralize # => "octopi"
15 # "sheep".pluralize # => "sheep"
16 # "words".pluralize # => "words"
17 # "the blue mailman".pluralize # => "the blue mailmen"
18 # "CamelOctopus".pluralize # => "CamelOctopi"
19 def pluralize
20 Inflector.pluralize(self)
21 end
22
23 # The reverse of +pluralize+, returns the singular form of a word in a string.
24 #
25 # "posts".singularize # => "post"
26 # "octopi".singularize # => "octopus"
27 # "sheep".singularize # => "sheep"
28 # "word".singularize # => "word"
29 # "the blue mailmen".singularize # => "the blue mailman"
30 # "CamelOctopi".singularize # => "CamelOctopus"
31 def singularize
32 Inflector.singularize(self)
33 end
34
35 # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
36 # is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
37 #
38 # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
39 #
40 # "active_record".camelize # => "ActiveRecord"
41 # "active_record".camelize(:lower) # => "activeRecord"
42 # "active_record/errors".camelize # => "ActiveRecord::Errors"
43 # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
44 def camelize(first_letter = :upper)
45 case first_letter
46 when :upper then Inflector.camelize(self, true)
47 when :lower then Inflector.camelize(self, false)
48 end
49 end
50 alias_method :camelcase, :camelize
51
52 # Capitalizes all the words and replaces some characters in the string to create
53 # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
54 # used in the Rails internals.
55 #
56 # +titleize+ is also aliased as +titlecase+.
57 #
58 # "man from the boondocks".titleize # => "Man From The Boondocks"
59 # "x-men: the last stand".titleize # => "X Men: The Last Stand"
60 def titleize
61 Inflector.titleize(self)
62 end
63 alias_method :titlecase, :titleize
64
65 # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
66 #
67 # +underscore+ will also change '::' to '/' to convert namespaces to paths.
68 #
69 # "ActiveRecord".underscore # => "active_record"
70 # "ActiveRecord::Errors".underscore # => active_record/errors
71 def underscore
72 Inflector.underscore(self)
73 end
74
75 # Replaces underscores with dashes in the string.
76 #
77 # "puni_puni" # => "puni-puni"
78 def dasherize
79 Inflector.dasherize(self)
80 end
81
82 # Removes the module part from the constant expression in the string.
83 #
84 # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
85 # "Inflections".demodulize # => "Inflections"
86 def demodulize
87 Inflector.demodulize(self)
88 end
89
90 # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
91 #
92 # ==== Examples
93 #
94 # class Person
95 # def to_param
96 # "#{id}-#{name.parameterize}"
97 # end
98 # end
99 #
100 # @person = Person.find(1)
101 # # => #<Person id: 1, name: "Donald E. Knuth">
102 #
103 # <%= link_to(@person.name, person_path %>
104 # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
105 def parameterize
106 Inflector.parameterize(self)
107 end
108
109 # Creates the name of a table like Rails does for models to table names. This method
110 # uses the +pluralize+ method on the last word in the string.
111 #
112 # "RawScaledScorer".tableize # => "raw_scaled_scorers"
113 # "egg_and_ham".tableize # => "egg_and_hams"
114 # "fancyCategory".tableize # => "fancy_categories"
115 def tableize
116 Inflector.tableize(self)
117 end
118
119 # Create a class name from a plural table name like Rails does for table names to models.
120 # Note that this returns a string and not a class. (To convert to an actual class
121 # follow +classify+ with +constantize+.)
122 #
123 # "egg_and_hams".classify # => "EggAndHam"
124 # "posts".classify # => "Post"
125 #
126 # Singular names are not handled correctly.
127 #
128 # "business".classify # => "Busines"
129 def classify
130 Inflector.classify(self)
131 end
132
133 # Capitalizes the first word, turns underscores into spaces, and strips '_id'.
134 # Like +titleize+, this is meant for creating pretty output.
135 #
136 # "employee_salary" # => "Employee salary"
137 # "author_id" # => "Author"
138 def humanize
139 Inflector.humanize(self)
140 end
141
142 # Creates a foreign key name from a class name.
143 # +separate_class_name_and_id_with_underscore+ sets whether
144 # the method should put '_' between the name and 'id'.
145 #
146 # Examples
147 # "Message".foreign_key # => "message_id"
148 # "Message".foreign_key(false) # => "messageid"
149 # "Admin::Post".foreign_key # => "post_id"
150 def foreign_key(separate_class_name_and_id_with_underscore = true)
151 Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
152 end
153
154 # +constantize+ tries to find a declared constant with the name specified
155 # in the string. It raises a NameError when the name is not in CamelCase
156 # or is not initialized.
157 #
158 # Examples
159 # "Module".constantize # => Module
160 # "Class".constantize # => Class
161 def constantize
162 Inflector.constantize(self)
163 end
164 end
165 end
166 end
167 end