Extlib::Inflection

English Nouns Number Inflection.

This module provides english singular <-> plural noun inflections.

Attributes

singular_of[R]
plural_of[R]

Public Class Methods

camelize(lower_case_and_underscored_word, *args) click to toggle source

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 32
32:       def camelize(lower_case_and_underscored_word, *args)
33:         lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
34:       end
classify(name) click to toggle source

Take an underscored name and make it into a camelized name

@example

  "egg_and_hams".classify #=> "EggAndHam"
  "enlarged_testes".classify #=> "EnlargedTestis"
  "post".classify #=> "Post"
    # File lib/extlib/inflection.rb, line 18
18:       def classify(name)
19:         words = name.to_s.sub(/.*\./, '').split('_')
20:         words[1] = singularize(words[1])
21:         words.collect { |word| word.capitalize }.join
22:       end
clear(type = :all) click to toggle source
     # File lib/extlib/inflection.rb, line 138
138:       def clear(type = :all)
139:         if type == :singular || type == :all
140:           @singular_of = {}
141:           @singular_rules = []
142:           @singularization_rules, @singularization_regex = nil, nil
143:         end
144:         if type == :plural || type == :all
145:           @singular_of = {}
146:           @singular_rules = []
147:           @singularization_rules, @singularization_regex = nil, nil
148:         end
149:       end
constantize(camel_cased_word) click to toggle source

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 98
 98:       def constantize(camel_cased_word)
 99:         unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
100:           raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
101:         end
102: 
103:         Object.module_eval("::#{$1}", __FILE__, __LINE__)
104:       end
demodulize(class_name_in_module) click to toggle source

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 64
64:       def demodulize(class_name_in_module)
65:         class_name_in_module.to_s.gsub(/^.*::/, '')
66:       end
foreign_key(class_name, key = "id") click to toggle source

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 87
87:       def foreign_key(class_name, key = "id")
88:         underscore(demodulize(class_name.to_s)) << "_" << key.to_s
89:       end
humanize(lower_case_and_underscored_word) click to toggle source

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 55
55:       def humanize(lower_case_and_underscored_word)
56:         lower_case_and_underscored_word.to_s.gsub(/_id$/, '').tr('_', ' ').capitalize
57:       end
plural(word) click to toggle source

Convert an English word from singular to plural.

  "boy".plural     #=> boys
  "tomato".plural  #=> tomatoes

Parameters

word

word to pluralize

Returns

pluralized form of word

Notes

Aliased as pluralize (a Railism)

     # File lib/extlib/inflection.rb, line 305
305:       def plural(word)
306:         # special exceptions
307:         return "" if word == ""
308:         if result = plural_of[word]
309:           return result.dup
310:         end
311:         result = word.dup
312:         regex, hash = pluralization_rules
313:         result.sub!(regex) {|m| hash[m]}
314:         plural_of[word] = result
315:         return result
316:       end
plural_rule(singular, plural) click to toggle source

Define a plurualization rule.

Parameters

singular

ending of the word in singular form

plural

ending of the word in plural form

Examples

Once the following rule is defined: English::Inflect.singular_rule ‘fe’, ‘ves’

You can see the following results: irb> “wife”.plural

> wives

     # File lib/extlib/inflection.rb, line 237
237:       def plural_rule(singular, plural)
238:         @plural_rules << [singular, plural]
239:       end
plural_word(singular, plural) click to toggle source

Define a pluralization exception.

Parameters

singular

singular form of the word

plural

plural form of the word

     # File lib/extlib/inflection.rb, line 171
171:       def plural_word(singular, plural)
172:         @plural_of[singular] = plural
173:         @plural_of[singular.capitalize] = plural.capitalize
174:       end
pluralization_rules() click to toggle source

Read prepared pluralization rules.

     # File lib/extlib/inflection.rb, line 253
253:       def pluralization_rules
254:         if defined?(@pluralization_regex) && @pluralization_regex
255:           return [@pluralization_regex, @pluralization_hash]
256:         end
257:         @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
258:         @pluralization_hash  = Hash[*@plural_rules.flatten]
259:         [@pluralization_regex, @pluralization_hash]
260:       end
rule(singular, plural, whole_word = false) click to toggle source

Define a general rule.

Parameters

singular

ending of the word in singular form

plural

ending of the word in plural form

whole_word

for capitalization, since words can be capitalized (Man => Men) #

Examples

Once the following rule is defined: English::Inflect.rule ‘y’, ‘ies’

You can see the following results: irb> “fly”.plural

> flies

irb> “cry”.plural

> cries

Define a general rule.

     # File lib/extlib/inflection.rb, line 197
197:       def rule(singular, plural, whole_word = false)
198:         singular_rule(singular, plural)
199:         plural_rule(singular, plural)
200:         word(singular, plural) if whole_word
201:       end
singular(word) click to toggle source

Convert an English word from plural to singular.

  "boys".singular      #=> boy
  "tomatoes".singular  #=> tomato

Parameters

word

word to singularize

Returns

singularized form of word

Notes

Aliased as singularize (a Railism)

     # File lib/extlib/inflection.rb, line 277
277:       def singular(word)
278:         if result = singular_of[word]
279:           return result.dup
280:         end
281:         result = word.dup
282:         regex, hash = singularization_rules
283:         result.sub!(regex) {|m| hash[m]}
284:         singular_of[word] = result
285:         return result
286:       end
singular_rule(singular, plural) click to toggle source

Define a singularization rule.

Parameters

singular

ending of the word in singular form

plural

ending of the word in plural form

Examples

Once the following rule is defined: English::Inflect.singular_rule ‘o’, ‘oes’

You can see the following results: irb> “heroes”.singular

> hero

     # File lib/extlib/inflection.rb, line 218
218:       def singular_rule(singular, plural)
219:         @singular_rules << [singular, plural]
220:       end
singular_word(singular, plural) click to toggle source

Define a singularization exception.

Parameters

singular

singular form of the word

plural

plural form of the word

     # File lib/extlib/inflection.rb, line 159
159:       def singular_word(singular, plural)
160:         @singular_of[plural] = singular
161:         @singular_of[plural.capitalize] = singular.capitalize
162:       end
singularization_rules() click to toggle source

Read prepared singularization rules.

     # File lib/extlib/inflection.rb, line 242
242:       def singularization_rules
243:         if defined?(@singularization_regex) && @singularization_regex
244:           return [@singularization_regex, @singularization_hash]
245:         end
246:         # No sorting needed: Regexen match on longest string
247:         @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i")
248:         @singularization_hash  = Hash[*@singular_rules.flatten].invert
249:         [@singularization_regex, @singularization_hash]
250:       end
tableize(class_name) click to toggle source

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"
  "EnlargedTestis".tableize #=> "enlarged_testes"
  "egg_and_ham".tableize #=> "egg_and_hams"
  "fancyCategory".tableize #=> "fancy_categories"
    # File lib/extlib/inflection.rb, line 76
76:       def tableize(class_name)
77:         words = class_name.to_const_path.tr('/', '_').split('_')
78:         words[1] = pluralize(words[1])
79:         words.join('_')
80:       end
underscore(camel_cased_word) click to toggle source

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 45
45:       def underscore(camel_cased_word)
46:         camel_cased_word.to_const_path
47:       end
word(singular, plural=nil) click to toggle source

Defines a general inflection exception case.

Parameters

singular

singular form of the word

plural

plural form of the word

Examples

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 132
132:       def word(singular, plural=nil)
133:         plural = singular unless plural
134:         singular_word(singular, plural)
135:         plural_word(singular, plural)
136:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.