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

root/branches/1-2-stable/activesupport/lib/active_support/inflector.rb

Revision 6077, 9.4 kB (checked in by bitsweat, 2 years ago)

Merge [6075] from trunk. References #7228.

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