Extlib::Inflection

English Nouns Number Inflection.

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

Attributes

plural_of[R]
singular_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
def camelize(lower_case_and_underscored_word, *args)
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
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
def classify(name)
  words = name.to_s.sub(/.*\./, '').split('_')
  words[-1] = singularize(words[-1])
  words.collect { |word| word.capitalize }.join
end
clear(type = :all) click to toggle source
# File lib/extlib/inflection.rb, line 138
def clear(type = :all)
  if type == :singular || type == :all
    @singular_of = {}
    @singular_rules = []
    @singularization_rules, @singularization_regex = nil, nil
  end
  if type == :plural || type == :all
    @singular_of = {}
    @singular_rules = []
    @singularization_rules, @singularization_regex = nil, nil
  end
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
def constantize(camel_cased_word)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
    raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
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
def demodulize(class_name_in_module)
  class_name_in_module.to_s.gsub(/^.*::/, '')
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
def foreign_key(class_name, key = "id")
  underscore(demodulize(class_name.to_s)) << "_" << key.to_s
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
def humanize(lower_case_and_underscored_word)
  lower_case_and_underscored_word.to_s.gsub(/_id$/, '').tr('_', ' ').capitalize
end
plural(word) click to toggle source

Convert an English word from singular to plural.

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

Parameters

word<String>

word to pluralize

Returns

<String>

pluralized form of word

Notes

Aliased as pluralize (a Railism)

# File lib/extlib/inflection.rb, line 305
def plural(word)
  # special exceptions
  return "" if word == ""
  if result = plural_of[word]
    return result.dup
  end
  result = word.dup
  regex, hash = pluralization_rules
  result.sub!(regex) {|m| hash[m]}
  plural_of[word] = result
  return result
end
Also aliased as: pluralize
plural_rule(singular, plural) click to toggle source

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

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

Define a pluralization exception.

Parameters

singular<String>

singular form of the word

plural<String>

plural form of the word

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

Read prepared pluralization rules.

# File lib/extlib/inflection.rb, line 253
def pluralization_rules
  if defined?(@pluralization_regex) && @pluralization_regex
    return [@pluralization_regex, @pluralization_hash]
  end
  @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i")
  @pluralization_hash  = Hash[*@plural_rules.flatten]
  [@pluralization_regex, @pluralization_hash]
end
pluralize(word) click to toggle source

Alias for plural (a Railism).

Alias for: plural
rule(singular, plural, whole_word = false) click to toggle source

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.

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

Convert an English word from plural 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)

# File lib/extlib/inflection.rb, line 277
def singular(word)
  if result = singular_of[word]
    return result.dup
  end
  result = word.dup
  regex, hash = singularization_rules
  result.sub!(regex) {|m| hash[m]}
  singular_of[word] = result
  return result
end
Also aliased as: singularize
singular_rule(singular, plural) click to toggle source

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

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

Define a singularization exception.

Parameters

singular<String>

singular form of the word

plural<String>

plural form of the word

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

Read prepared singularization rules.

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

Alias for singular (a Railism).

Alias for: singular
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
def tableize(class_name)
  words = class_name.to_const_path.tr('/', '_').split('_')
  words[-1] = pluralize(words[-1])
  words.join('_')
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
def underscore(camel_cased_word)
  camel_cased_word.to_const_path
end
word(singular, plural=nil) click to toggle source

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'

# File lib/extlib/inflection.rb, line 132
def word(singular, plural=nil)
  plural = singular unless plural
  singular_word(singular, plural)
  plural_word(singular, plural)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.