Module: Extlib::Inflection

Module | Extlib::Inflection
In: | lib/extlib/inflection.rb

# English Nouns Number Inflection.

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

### Methods

camelize classify clear constantize demodulize foreign_key humanize plural plural_rule plural_word pluralization_rules rule singular singular_rule singular_word singularization_rules tableize underscore word

### External Aliases

singular | -> | singularize
| Alias for singular (a Railism).
plural | -> | pluralize
| Alias for plural (a Railism).

### Attributes

plural_of | [R] |
singular_of | [R] |

### Public Class methods

camelize(lower_case_and_underscored_word, *args)

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"

classify(name)

Take an underscored name and make it into a camelized name

@example

      "egg_and_hams".classify #=> "EggAndHam"
      "post".classify #=> "Post"

clear(type = :all)
constantize(camel_cased_word)

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

demodulize(class_name_in_module)

Removes the module part from the expression in the string

@example

      "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
      "Inflections".demodulize #=> "Inflections"

foreign_key(class_name, key = "id")

Creates a foreign key name from a class name.

@example

      "Message".foreign_key #=> "message_id"
      "Admin::Post".foreign_key #=> "post_id"

humanize(lower_case_and_underscored_word)

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"

plural(word)

Convert an English word from singular to plurel.

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

#### Parameters

word<String>: | word to pluralize

#### Returns

<String>: | pluralized form of word

#### Notes

Aliased as pluralize (a Railism)

plural_rule(singular, plural)

Define a plurualization rule.

#### Parameters

singular<String>: | ending of the word in singular form
plural<String>: | 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

plural_word(singular, plural)

Define a pluralization exception.

#### Parameters

singular<String>: | singular form of the word
plural<String>: | plural form of the word

pluralization_rules()

Read prepared pluralization rules.

rule(singular, plural, whole_word = false)

Define a general rule.

#### Parameters

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) #

#### 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.

singular(word)

Convert an English word from plurel to singular.

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

#### Parameters

word<String>: | word to singularize

#### Returns

<String>: | singularized form of word

#### Notes

Aliased as singularize (a Railism)

singular_rule(singular, plural)

Define a singularization rule.

#### Parameters

singular<String>: | ending of the word in singular form
plural<String>: | 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

singular_word(singular, plural)

Define a singularization exception.

#### Parameters

singular<String>: | singular form of the word
plural<String>: | plural form of the word

singularization_rules()

Read prepared singularization rules.

tableize(class_name)

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"

underscore(camel_cased_word)

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

word(singular, plural=nil)

Defines a general inflection exception case.

#### Parameters

singular<String>: | singular form of the word
plural<String>: | 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‘

[Validate]