| Module | Extlib::Inflection |
| In: |
lib/extlib/inflection.rb
|
This module provides english singular <-> plural noun inflections.
| plural_of | [R] | |
| singular_of | [R] |
By default, camelize converts strings to UpperCamelCase.
camelize will also convert ’/’ to ’::’ which is useful for converting paths to namespaces
@example
"active_record".camelize #=> "ActiveRecord" "active_record/errors".camelize #=> "ActiveRecord::Errors"
# File lib/extlib/inflection.rb, line 27
27: def camelize(lower_case_and_underscored_word, *args)
28: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
29: end
Take an underscored name and make it into a camelized name
@example
"egg_and_hams".classify #=> "EggAndHam" "post".classify #=> "Post"
# File lib/extlib/inflection.rb, line 15
15: def classify(name)
16: camelize(singularize(name.to_s.sub(/.*\./, '')))
17: end
# File lib/extlib/inflection.rb, line 130
130: def clear(type = :all)
131: if type == :singular || type == :all
132: @singular_of = {}
133: @singular_rules = []
134: @singularization_rules, @singularization_regex = nil, nil
135: end
136: if type == :plural || type == :all
137: @singular_of = {}
138: @singular_rules = []
139: @singularization_rules, @singularization_regex = nil, nil
140: end
141: end
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
@example
"Module".constantize #=> Module "Class".constantize #=> Class
# File lib/extlib/inflection.rb, line 90
90: def constantize(camel_cased_word)
91: unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
92: raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
93: end
94:
95: Object.module_eval("::#{$1}", __FILE__, __LINE__)
96: end
Removes the module part from the expression in the string
@example
"ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections" "Inflections".demodulize #=> "Inflections"
# File lib/extlib/inflection.rb, line 59
59: def demodulize(class_name_in_module)
60: class_name_in_module.to_s.gsub(/^.*::/, '')
61: end
Creates a foreign key name from a class name.
@example
"Message".foreign_key #=> "message_id" "Admin::Post".foreign_key #=> "post_id"
# File lib/extlib/inflection.rb, line 79
79: def foreign_key(class_name, key = "id")
80: underscore(demodulize(class_name.to_s)) << "_" << key.to_s
81: end
Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.
@example
"employee_salary" #=> "Employee salary" "author_id" #=> "Author"
# File lib/extlib/inflection.rb, line 50
50: def humanize(lower_case_and_underscored_word)
51: lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
52: end
Convert an English word from singular to plurel.
"boy".plural #=> boys "tomato".plural #=> tomatoes
| word<String>: | word to pluralize |
| <String>: | pluralized form of word |
Aliased as pluralize (a Railism)
# File lib/extlib/inflection.rb, line 297
297: def plural(word)
298: # special exceptions
299: return "" if word == ""
300: if result = plural_of[word]
301: return result.dup
302: end
303: result = word.dup
304: regex, hash = pluralization_rules
305: result.sub!(regex) {|m| hash[m]}
306: plural_of[word] = result
307: return result
308: end
Define a plurualization rule.
| singular<String>: | ending of the word in singular form |
| plural<String>: | ending of the word in plural form |
Once the following rule is defined: English::Inflect.singular_rule ‘fe’, ‘ves‘
You can see the following results: irb> "wife".plural
# File lib/extlib/inflection.rb, line 229
229: def plural_rule(singular, plural)
230: @plural_rules << [singular, plural]
231: end
Define a pluralization exception.
| singular<String>: | singular form of the word |
| plural<String>: | plural form of the word |
# File lib/extlib/inflection.rb, line 163
163: def plural_word(singular, plural)
164: @plural_of[singular] = plural
165: @plural_of[singular.capitalize] = plural.capitalize
166: end
Read prepared pluralization rules.
# File lib/extlib/inflection.rb, line 245
245: def pluralization_rules
246: if defined?(@pluralization_regex) && @pluralization_regex
247: return [@pluralization_regex, @pluralization_hash]
248: end
249: @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
250: @pluralization_hash = Hash[*@plural_rules.flatten]
251: [@pluralization_regex, @pluralization_hash]
252: end
Define a general rule.
| singular<String>: | ending of the word in singular form |
| plural<String>: | ending of the word in plural form |
| whole_word<Boolean>: | for capitalization, since words can be capitalized (Man => Men) # |
Once the following rule is defined: English::Inflect.rule ‘y’, ‘ies‘
You can see the following results: irb> "fly".plural
irb> "cry".plural
Define a general rule.
# File lib/extlib/inflection.rb, line 189
189: def rule(singular, plural, whole_word = false)
190: singular_rule(singular, plural)
191: plural_rule(singular, plural)
192: word(singular, plural) if whole_word
193: end
Convert an English word from plurel to singular.
"boys".singular #=> boy "tomatoes".singular #=> tomato
| word<String>: | word to singularize |
| <String>: | singularized form of word |
Aliased as singularize (a Railism)
# File lib/extlib/inflection.rb, line 269
269: def singular(word)
270: if result = singular_of[word]
271: return result.dup
272: end
273: result = word.dup
274: regex, hash = singularization_rules
275: result.sub!(regex) {|m| hash[m]}
276: singular_of[word] = result
277: return result
278: end
Define a singularization rule.
| singular<String>: | ending of the word in singular form |
| plural<String>: | ending of the word in plural form |
Once the following rule is defined: English::Inflect.singular_rule ‘o’, ‘oes‘
You can see the following results: irb> "heroes".singular
# File lib/extlib/inflection.rb, line 210
210: def singular_rule(singular, plural)
211: @singular_rules << [singular, plural]
212: end
Define a singularization exception.
| singular<String>: | singular form of the word |
| plural<String>: | plural form of the word |
# File lib/extlib/inflection.rb, line 151
151: def singular_word(singular, plural)
152: @singular_of[plural] = singular
153: @singular_of[plural.capitalize] = singular.capitalize
154: end
Read prepared singularization rules.
# File lib/extlib/inflection.rb, line 234
234: def singularization_rules
235: if defined?(@singularization_regex) && @singularization_regex
236: return [@singularization_regex, @singularization_hash]
237: end
238: # No sorting needed: Regexen match on longest string
239: @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i")
240: @singularization_hash = Hash[*@singular_rules.flatten].invert
241: [@singularization_regex, @singularization_hash]
242: end
Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.
@example
"RawScaledScorer".tableize #=> "raw_scaled_scorers" "egg_and_ham".tableize #=> "egg_and_hams" "fancyCategory".tableize #=> "fancy_categories"
# File lib/extlib/inflection.rb, line 70
70: def tableize(class_name)
71: pluralize(class_name.to_const_path.gsub(/\//, '_'))
72: end
The reverse of camelize. Makes an underscored form from the expression in the string.
Changes ’::’ to ’/’ to convert namespaces to paths.
@example
"ActiveRecord".underscore #=> "active_record" "ActiveRecord::Errors".underscore #=> active_record/errors
# File lib/extlib/inflection.rb, line 40
40: def underscore(camel_cased_word)
41: camel_cased_word.to_const_path
42: end
Defines a general inflection exception case.
| singular<String>: | singular form of the word |
| plural<String>: | plural form of the word |
Here we define erratum/errata exception case:
English::Inflect.word "erratum", "errata"
In case singular and plural forms are the same omit second argument on call:
English::Inflect.word ‘information‘
# File lib/extlib/inflection.rb, line 124
124: def word(singular, plural=nil)
125: plural = singular unless plural
126: singular_word(singular, plural)
127: plural_word(singular, plural)
128: end