Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 213 lines (197 sloc) 7.99 kb
d04c6f2 Add a missing require in 'active_support/core_ext/string/inflections'
Carl Lerche authored
1 require 'active_support/inflector/methods'
cd0267c @fxn adds missing require for #parameterize
fxn authored
2 require 'active_support/inflector/transliterate'
3
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
4 # String inflections define new methods on the String class to transform names for different purposes.
25e3d2c @r00k Correct bad wording in description.
r00k authored
5 # For instance, you can figure out the name of a table from the name of a class.
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
6 #
432a65f @gazay String quotes and trailing spaces
gazay authored
7 # 'ScaleScore'.tableize # => "scale_scores"
f36a380 @smathy Inflection dependency, [#4067 status:resolved]
smathy authored
8 #
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
9 class String
10 # Returns the plural form of the word in the string.
11 #
4535191 @rroblak Modified String#pluralize to take an optional count parameter.
rroblak authored
12 # If the optional parameter +count+ is specified,
13 # the singular form will be returned if <tt>count == 1</tt>.
14 # For any other value of +count+ the plural will be returned.
15 #
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
16 # If the optional parameter +locale+ is specified,
17 # the word will be pluralized as a word of that language.
18 # By default, this parameter is set to <tt>:en</tt>.
19 # You must define your own inflection rules for languages other than English.
20 #
432a65f @gazay String quotes and trailing spaces
gazay authored
21 # 'post'.pluralize # => "posts"
22 # 'octopus'.pluralize # => "octopi"
23 # 'sheep'.pluralize # => "sheep"
24 # 'words'.pluralize # => "words"
25 # 'the blue mailman'.pluralize # => "the blue mailmen"
26 # 'CamelOctopus'.pluralize # => "CamelOctopi"
27 # 'apple'.pluralize(1) # => "apple"
28 # 'apple'.pluralize(2) # => "apples"
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
29 # 'ley'.pluralize(:es) # => "leyes"
30 # 'ley'.pluralize(1, :es) # => "ley"
d716fe0 @rafaelfranca Revert "Merge pull request #10158 from steveklabnik/issue_10125"
rafaelfranca authored
31 def pluralize(count = nil, locale = :en)
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
32 locale = count if count.is_a?(Symbol)
4535191 @rroblak Modified String#pluralize to take an optional count parameter.
rroblak authored
33 if count == 1
34 self
35 else
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
36 ActiveSupport::Inflector.pluralize(self, locale)
4535191 @rroblak Modified String#pluralize to take an optional count parameter.
rroblak authored
37 end
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
38 end
a66b321 @dhh Added the meat for String inflection
dhh authored
39
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
40 # The reverse of +pluralize+, returns the singular form of a word in a string.
1e593db @GRoguelon Adding a missing parenthesis in the doc of String#parameterize.
GRoguelon authored
41 #
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
42 # If the optional parameter +locale+ is specified,
43 # the word will be singularized as a word of that language.
e890a69 @carsonmcdonald Corrected paramter to parameter
carsonmcdonald authored
44 # By default, this parameter is set to <tt>:en</tt>.
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
45 # You must define your own inflection rules for languages other than English.
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
46 #
432a65f @gazay String quotes and trailing spaces
gazay authored
47 # 'posts'.singularize # => "post"
48 # 'octopi'.singularize # => "octopus"
49 # 'sheep'.singularize # => "sheep"
50 # 'word'.singularize # => "word"
51 # 'the blue mailmen'.singularize # => "the blue mailman"
52 # 'CamelOctopi'.singularize # => "CamelOctopus"
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
53 # 'leyes'.singularize(:es) # => "ley"
d716fe0 @rafaelfranca Revert "Merge pull request #10158 from steveklabnik/issue_10125"
rafaelfranca authored
54 def singularize(locale = :en)
7db0b07 @davidcelis Make ActiveSupport::Inflector locale aware and multilingual
davidcelis authored
55 ActiveSupport::Inflector.singularize(self, locale)
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
56 end
a66b321 @dhh Added the meat for String inflection
dhh authored
57
51be8db @spastorino Move constantize from conversions to inflections.
spastorino authored
58 # +constantize+ tries to find a declared constant with the name specified
59 # in the string. It raises a NameError when the name is not in CamelCase
310565f @rroblak Added ActiveSupport::Inflector.safe_constantize and String#safe_constant...
rroblak authored
60 # or is not initialized. See ActiveSupport::Inflector.constantize
51be8db @spastorino Move constantize from conversions to inflections.
spastorino authored
61 #
432a65f @gazay String quotes and trailing spaces
gazay authored
62 # 'Module'.constantize # => Module
63 # 'Class'.constantize # => Class
64 # 'blargle'.constantize # => NameError: wrong constant name blargle
51be8db @spastorino Move constantize from conversions to inflections.
spastorino authored
65 def constantize
66 ActiveSupport::Inflector.constantize(self)
67 end
4535191 @rroblak Modified String#pluralize to take an optional count parameter.
rroblak authored
68
310565f @rroblak Added ActiveSupport::Inflector.safe_constantize and String#safe_constant...
rroblak authored
69 # +safe_constantize+ tries to find a declared constant with the name specified
70 # in the string. It returns nil when the name is not in CamelCase
71 # or is not initialized. See ActiveSupport::Inflector.safe_constantize
72 #
432a65f @gazay String quotes and trailing spaces
gazay authored
73 # 'Module'.safe_constantize # => Module
74 # 'Class'.safe_constantize # => Class
75 # 'blargle'.safe_constantize # => nil
310565f @rroblak Added ActiveSupport::Inflector.safe_constantize and String#safe_constant...
rroblak authored
76 def safe_constantize
77 ActiveSupport::Inflector.safe_constantize(self)
78 end
51be8db @spastorino Move constantize from conversions to inflections.
spastorino authored
79
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
80 # By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
81 # is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
82 #
83 # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
84 #
432a65f @gazay String quotes and trailing spaces
gazay authored
85 # 'active_record'.camelize # => "ActiveRecord"
86 # 'active_record'.camelize(:lower) # => "activeRecord"
87 # 'active_record/errors'.camelize # => "ActiveRecord::Errors"
88 # 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
89 def camelize(first_letter = :upper)
90 case first_letter
9257224 @gazay AS core_ext refactoring pt.2
gazay authored
91 when :upper
92 ActiveSupport::Inflector.camelize(self, true)
93 when :lower
94 ActiveSupport::Inflector.camelize(self, false)
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
95 end
96 end
97 alias_method :camelcase, :camelize
b23c72f Add title case method to String to do, e.g., 'action_web_service'.titlec...
Marcel Molina authored
98
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
99 # Capitalizes all the words and replaces some characters in the string to create
100 # a nicer looking title. +titleize+ is meant for creating pretty output. It is not
101 # used in the Rails internals.
102 #
103 # +titleize+ is also aliased as +titlecase+.
104 #
432a65f @gazay String quotes and trailing spaces
gazay authored
105 # 'man from the boondocks'.titleize # => "Man From The Boondocks"
106 # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
107 def titleize
108 ActiveSupport::Inflector.titleize(self)
109 end
110 alias_method :titlecase, :titleize
a66b321 @dhh Added the meat for String inflection
dhh authored
111
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
112 # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
113 #
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
114 # +underscore+ will also change '::' to '/' to convert namespaces to paths.
115 #
432a65f @gazay String quotes and trailing spaces
gazay authored
116 # 'ActiveModel'.underscore # => "active_model"
117 # 'ActiveModel::Errors'.underscore # => "active_model/errors"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
118 def underscore
119 ActiveSupport::Inflector.underscore(self)
120 end
a66b321 @dhh Added the meat for String inflection
dhh authored
121
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
122 # Replaces underscores with dashes in the string.
123 #
fce1735 @marcandre Fix doc example for dasherize
marcandre authored
124 # 'puni_puni'.dasherize # => "puni-puni"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
125 def dasherize
126 ActiveSupport::Inflector.dasherize(self)
127 end
cd98947 @dhh Added Hash#to_xml and Array#to_xml that makes it much easier to produce ...
dhh authored
128
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
129 # Removes the module part from the constant expression in the string.
130 #
432a65f @gazay String quotes and trailing spaces
gazay authored
131 # 'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
132 # 'Inflections'.demodulize # => "Inflections"
11f6795 @fxn defines Module#qualified_const_(defined?|get|set) and String#deconstanti...
fxn authored
133 #
9d1ba37 @fxn fixes a typo (thanks to Alexey Vakhov)
fxn authored
134 # See also +deconstantize+.
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
135 def demodulize
136 ActiveSupport::Inflector.demodulize(self)
137 end
a66b321 @dhh Added the meat for String inflection
dhh authored
138
11f6795 @fxn defines Module#qualified_const_(defined?|get|set) and String#deconstanti...
fxn authored
139 # Removes the rightmost segment from the constant expression in the string.
140 #
432a65f @gazay String quotes and trailing spaces
gazay authored
141 # 'Net::HTTP'.deconstantize # => "Net"
142 # '::Net::HTTP'.deconstantize # => "::Net"
143 # 'String'.deconstantize # => ""
144 # '::String'.deconstantize # => ""
145 # ''.deconstantize # => ""
11f6795 @fxn defines Module#qualified_const_(defined?|get|set) and String#deconstanti...
fxn authored
146 #
147 # See also +demodulize+.
148 def deconstantize
149 ActiveSupport::Inflector.deconstantize(self)
150 end
151
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
152 # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
153 #
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
154 # class Person
155 # def to_param
156 # "#{id}-#{name.parameterize}"
157 # end
158 # end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
159 #
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
160 # @person = Person.find(1)
161 # # => #<Person id: 1, name: "Donald E. Knuth">
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
162 #
1e593db @GRoguelon Adding a missing parenthesis in the doc of String#parameterize.
GRoguelon authored
163 # <%= link_to(@person.name, person_path) %>
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
164 # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
165 def parameterize(sep = '-')
166 ActiveSupport::Inflector.parameterize(self, sep)
167 end
90366a1 @dhh Added Inflector#parameterize for easy slug generation ("Donald E. Knuth"...
dhh authored
168
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
169 # Creates the name of a table like Rails does for models to table names. This method
170 # uses the +pluralize+ method on the last word in the string.
171 #
432a65f @gazay String quotes and trailing spaces
gazay authored
172 # 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
173 # 'egg_and_ham'.tableize # => "egg_and_hams"
174 # 'fancyCategory'.tableize # => "fancy_categories"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
175 def tableize
176 ActiveSupport::Inflector.tableize(self)
177 end
a66b321 @dhh Added the meat for String inflection
dhh authored
178
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
179 # Create a class name from a plural table name like Rails does for table names to models.
180 # Note that this returns a string and not a class. (To convert to an actual class
181 # follow +classify+ with +constantize+.)
182 #
432a65f @gazay String quotes and trailing spaces
gazay authored
183 # 'egg_and_hams'.classify # => "EggAndHam"
184 # 'posts'.classify # => "Post"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
185 #
186 # Singular names are not handled correctly.
187 #
432a65f @gazay String quotes and trailing spaces
gazay authored
188 # 'business'.classify # => "Busines"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
189 def classify
190 ActiveSupport::Inflector.classify(self)
191 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -exec ...
spastorino authored
192
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
193 # Capitalizes the first word, turns underscores into spaces, and strips '_id'.
194 # Like +titleize+, this is meant for creating pretty output.
195 #
4eefa1e @rafaelfranca Merge pull request #8560 from u16suzu/master
rafaelfranca authored
196 # 'employee_salary'.humanize # => "Employee salary"
197 # 'author_id'.humanize # => "Author"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
198 def humanize
199 ActiveSupport::Inflector.humanize(self)
200 end
a66b321 @dhh Added the meat for String inflection
dhh authored
201
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
202 # Creates a foreign key name from a class name.
203 # +separate_class_name_and_id_with_underscore+ sets whether
204 # the method should put '_' between the name and 'id'.
205 #
432a65f @gazay String quotes and trailing spaces
gazay authored
206 # 'Message'.foreign_key # => "message_id"
207 # 'Message'.foreign_key(false) # => "messageid"
208 # 'Admin::Post'.foreign_key # => "post_id"
b70239b @jeremy Convert string extension modules to class reopens
jeremy authored
209 def foreign_key(separate_class_name_and_id_with_underscore = true)
210 ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
211 end
a66b321 @dhh Added the meat for String inflection
dhh authored
212 end
Something went wrong with that request. Please try again.