public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Merge [5924] from trunk. References #7023.

git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5925 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
jeremy (author)
Sun Jan 14 04:20:52 -0800 2007
commit  68849e3239f1e363da02e6ffd3457f281167de4a
tree    6a4c39a1265e82f89852c169209a14104493f2b4
parent  debaf2080332672bbdeb0ac42bf946602ef00d63
...
1
 
 
 
2
3
4
 
 
 
5
6
7
...
 
1
2
3
4
5
6
7
8
9
10
11
12
0
@@ -1,7 +1,12 @@
0
-*1.4.0 RC2* (r5847, January 4th, 2007)
0
+*SVN*
0
+
0
+* Document Inflector.ordinalize and merge docs from String inflections. #7023 [smeade]
0
 
0
 * Unbundle flexmock. [Jeremy Kemper]
0
 
0
+
0
+*1.4.0 RC2* (r5847, January 4th, 2007)
0
+
0
 * Fix Dependencies.autoloaded? to ignore anonymous modules. Closes #6561. [Nicholas Seckar]
0
 
0
 * Update load once paths to prevent nested once constants from being detected and claimed by an external non-once load. [Nicholas Seckar]
...
1
 
 
2
3
4
5
6
7
8
 
 
 
 
 
 
 
 
9
10
11
...
 
1
2
3
4
5
6
 
 
 
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1,11 +1,17 @@
0
-require File.dirname(__FILE__) + '/../../inflector' unless defined? Inflector
0
+require 'active_support/inflector'
0
+
0
 module ActiveSupport #:nodoc:
0
   module CoreExtensions #:nodoc:
0
     module Integer #:nodoc:
0
       module Inflections
0
- # 1.ordinalize # => "1st"
0
- # 3.ordinalize # => "3rd"
0
- # 10.ordinalize # => "10th"
0
+ # Ordinalize turns a number into an ordinal string used to denote the
0
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
0
+ #
0
+ # Examples
0
+ # 1.ordinalize # => "1st"
0
+ # 2.ordinalize # => "2nd"
0
+ # 1002.ordinalize # => "1002nd"
0
+ # 1003.ordinalize # => "1003rd"
0
         def ordinalize
0
           Inflector.ordinalize(self)
0
         end
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-require File.dirname(__FILE__) + '/../../inflector' unless defined? Inflector
0
+require 'active_support/inflector'
0
 
0
 module ActiveSupport #:nodoc:
0
   module CoreExtensions #:nodoc:
...
1
 
2
3
4
5
6
 
7
8
9
...
21
22
23
24
 
25
26
 
27
28
29
30
31
 
 
32
33
34
35
36
37
 
 
38
39
40
...
42
43
44
45
 
46
47
48
...
50
51
52
53
 
54
55
 
56
57
58
...
60
61
62
63
 
64
65
66
...
87
88
89
 
 
 
 
 
 
 
 
 
90
91
92
...
98
99
100
 
 
 
 
 
 
 
 
 
101
102
103
...
109
110
111
 
 
 
 
 
 
 
 
 
 
112
113
114
...
117
118
119
 
 
 
 
 
 
 
 
 
120
121
122
123
 
 
 
 
 
 
 
 
124
125
126
...
128
129
130
131
 
 
 
 
 
132
133
134
135
 
 
 
 
 
 
136
137
138
139
 
 
 
 
 
140
141
142
143
 
 
 
 
 
 
 
144
145
146
147
 
 
 
 
 
 
 
 
148
149
150
151
152
 
 
 
 
 
 
 
 
153
154
155
156
 
 
 
 
 
 
 
157
158
159
160
 
 
 
 
161
162
163
164
 
 
 
 
 
 
 
 
165
166
167
...
 
1
2
3
4
5
 
6
7
8
9
...
21
22
23
 
24
25
 
26
27
28
29
 
 
30
31
32
33
34
35
 
 
36
37
38
39
40
...
42
43
44
 
45
46
47
48
...
50
51
52
 
53
54
 
55
56
57
58
...
60
61
62
 
63
64
65
66
...
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 
160
161
162
163
164
165
166
167
168
169
170
...
172
173
174
 
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
 
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
 
 
 
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
0
@@ -1,9 +1,9 @@
0
-require 'singleton'
0
+require 'singleton'
0
 
0
 # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
0
 # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
0
 # in inflections.rb.
0
-module Inflector
0
+module Inflector
0
   # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
0
   # inflection rules. Examples:
0
   #
0
@@ -21,20 +21,20 @@ module Inflector
0
   # already have been loaded.
0
   class Inflections
0
     include Singleton
0
-
0
+
0
     attr_reader :plurals, :singulars, :uncountables
0
-
0
+
0
     def initialize
0
       @plurals, @singulars, @uncountables = [], [], []
0
     end
0
-
0
- # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
0
+
0
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
0
     # The replacement should always be a string that may include references to the matched data from the rule.
0
     def plural(rule, replacement)
0
       @plurals.insert(0, [rule, replacement])
0
     end
0
-
0
- # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
0
+
0
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
0
     # The replacement should always be a string that may include references to the matched data from the rule.
0
     def singular(rule, replacement)
0
       @singulars.insert(0, [rule, replacement])
0
@@ -42,7 +42,7 @@ module Inflector
0
 
0
     # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
0
     # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
0
- #
0
+ #
0
     # Examples:
0
     # irregular 'octopus', 'octopi'
0
     # irregular 'person', 'people'
0
@@ -50,9 +50,9 @@ module Inflector
0
       plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
0
       singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
0
     end
0
-
0
+
0
     # Add uncountable words that shouldn't be attempted inflected.
0
- #
0
+ #
0
     # Examples:
0
     # uncountable "money"
0
     # uncountable "money", "information"
0
@@ -60,7 +60,7 @@ module Inflector
0
     def uncountable(*words)
0
       (@uncountables << words).flatten!
0
     end
0
-
0
+
0
     # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
0
     # the options are: :plurals, :singulars, :uncountables
0
     #
0
@@ -87,6 +87,15 @@ module Inflector
0
     end
0
   end
0
 
0
+ # Returns the plural form of the word in the string.
0
+ #
0
+ # Examples
0
+ # "post".pluralize #=> "posts"
0
+ # "octopus".pluralize #=> "octopi"
0
+ # "sheep".pluralize #=> "sheep"
0
+ # "words".pluralize #=> "words"
0
+ # "the blue mailman".pluralize #=> "the blue mailmen"
0
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
0
   def pluralize(word)
0
     result = word.to_s.dup
0
 
0
@@ -98,6 +107,15 @@ module Inflector
0
     end
0
   end
0
 
0
+ # The reverse of pluralize, returns the singular form of a word in a string.
0
+ #
0
+ # Examples
0
+ # "posts".singularize #=> "post"
0
+ # "octopi".singularize #=> "octopus"
0
+ # "sheep".singluarize #=> "sheep"
0
+ # "word".singluarize #=> "word"
0
+ # "the blue mailmen".singularize #=> "the blue mailman"
0
+ # "CamelOctopi".singularize #=> "CamelOctopus"
0
   def singularize(word)
0
     result = word.to_s.dup
0
 
0
@@ -109,6 +127,16 @@ module Inflector
0
     end
0
   end
0
 
0
+ # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
0
+ # is set to ":lower" then camelize produces lowerCamelCase.
0
+ #
0
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
0
+ #
0
+ # Examples
0
+ # "active_record".camelize #=> "ActiveRecord"
0
+ # "active_record".camelize(:lower) #=> "activeRecord"
0
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
0
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
0
   def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
0
     if first_letter_in_uppercase
0
       lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
0
@@ -117,10 +145,26 @@ module Inflector
0
     end
0
   end
0
 
0
+ # Capitalizes all the words and replaces some characters in the string to create
0
+ # a nicer looking title. Titleize is meant for creating pretty output. It is not
0
+ # used in the Rails internals.
0
+ #
0
+ # titleize is also aliased as as titlecase
0
+ #
0
+ # Examples
0
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
0
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
0
   def titleize(word)
0
     humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
0
   end
0
-
0
+
0
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
0
+ #
0
+ # Changes '::' to '/' to convert namespaces to paths.
0
+ #
0
+ # Examples
0
+ # "ActiveRecord".underscore #=> "active_record"
0
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
0
   def underscore(camel_cased_word)
0
     camel_cased_word.to_s.gsub(/::/, '/').
0
       gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
0
@@ -128,40 +172,93 @@ module Inflector
0
       tr("-", "_").
0
       downcase
0
   end
0
-
0
+
0
+ # Replaces underscores with dashes in the string.
0
+ #
0
+ # Example
0
+ # "puni_puni" #=> "puni-puni"
0
   def dasherize(underscored_word)
0
     underscored_word.gsub(/_/, '-')
0
   end
0
 
0
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
0
+ # Like titleize, this is meant for creating pretty output.
0
+ #
0
+ # Examples
0
+ # "employee_salary" #=> "Employee salary"
0
+ # "author_id" #=> "Author"
0
   def humanize(lower_case_and_underscored_word)
0
     lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
0
   end
0
 
0
+ # Removes the module part from the expression in the string
0
+ #
0
+ # Examples
0
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
0
+ # "Inflections".demodulize #=> "Inflections"
0
   def demodulize(class_name_in_module)
0
     class_name_in_module.to_s.gsub(/^.*::/, '')
0
   end
0
 
0
+ # Create the name of a table like Rails does for models to table names. This method
0
+ # uses the pluralize method on the last word in the string.
0
+ #
0
+ # Examples
0
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
0
+ # "egg_and_ham".tableize #=> "egg_and_hams"
0
+ # "fancyCategory".tableize #=> "fancy_categories"
0
   def tableize(class_name)
0
     pluralize(underscore(class_name))
0
   end
0
-
0
+
0
+ # Create a class name from a table name like Rails does for table names to models.
0
+ # Note that this returns a string and not a Class. (To convert to an actual class
0
+ # follow classify with constantize.)
0
+ #
0
+ # Examples
0
+ # "egg_and_hams".classify #=> "EggAndHam"
0
+ # "post".classify #=> "Post"
0
   def classify(table_name)
0
     # strip out any leading schema name
0
     camelize(singularize(table_name.to_s.sub(/.*\./, '')))
0
   end
0
 
0
+ # Creates a foreign key name from a class name.
0
+ # +separate_class_name_and_id_with_underscore+ sets whether
0
+ # the method should put '_' between the name and 'id'.
0
+ #
0
+ # Examples
0
+ # "Message".foreign_key #=> "message_id"
0
+ # "Message".foreign_key(false) #=> "messageid"
0
+ # "Admin::Post".foreign_key #=> "post_id"
0
   def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
0
     underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
   end
0
 
0
+ # Constantize tries to find a declared constant with the name specified
0
+ # in the string. It raises a NameError when the name is not in CamelCase
0
+ # or is not initialized.
0
+ #
0
+ # Examples
0
+ # "Module".constantize #=> Module
0
+ # "Class".constantize #=> Class
0
   def constantize(camel_cased_word)
0
- raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" unless
0
- /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word
0
-
0
+ unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word
0
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
0
+ end
0
+
0
     camel_cased_word = "::#{camel_cased_word}" unless $1
0
     Object.module_eval(camel_cased_word, __FILE__, __LINE__)
0
   end
0
 
0
+ # Ordinalize turns a number into an ordinal string used to denote the
0
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
0
+ #
0
+ # Examples
0
+ # ordinalize(1) # => "1st"
0
+ # ordinalize(2) # => "2nd"
0
+ # ordinalize(1002) # => "1002nd"
0
+ # ordinalize(1003) # => "1003rd"
0
   def ordinalize(number)
0
     if (11..13).include?(number.to_i % 100)
0
       "#{number}th"

Comments

    No one has commented yet.