inflection

New in version 1.1.0.

Word transformation.

Transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys.

Inflection is language specific and the default inflection handles English language inflection. You can access inflection handlers for other languages by importing the appropriate inflector. For example from miwa.inflection.sv import inflection to use Swedish inflection.

Author:Rasmus Andersson http://hunch.se/
var inflections:
 locale-to-Inflection-object map
var inflection:English inflection
class smisk.inflection.Inflector(*locales)

Language inflector.

Variable locales:
 languages this inflection handles
Variable plurals:
 plural rules
Variable singulars:
 singular rules
Variable uncountables:
 list of uncountable words
camelize(lower_case_and_underscored_word, first_letter_uppercase=True)

By default, camelize converts strings to UpperCamelCase. If the first_letter_uppercase argument is set to False, camelize produces lowerCamelCase.

camelize will also convert / to . which is useful for converting paths to namespaces

Examples
“active_record”.camelize -> “ActiveRecord” “active_record”.camelize(False) -> “activeRecord” “active_record/errors”.camelize -> “ActiveRecord.Errors” “active_record/errors”.camelize(0) -> “activeRecord.Errors”
Return type:unicode
classify(table_name)

Create a class name from a table name.

Examples
“egg_and_hams”.classify -> “EggAndHam” “post”.classify -> “Post”
Return type:unicode
clear()

Clears any loaded inflections.

Return type:None
demodulize(class_name_in_module)

Removes the module part from the expression in the string

Examples
“ActiveRecord.CoreExtensions.String.Inflectors”.demodulize #=> “Inflectors” “Inflectors”.demodulize #=> “Inflectors”
Return type:unicode
foreignKey(class_name, separate_class_name_and_id_with_underscore=True)

Creates a foreign key name from a class name. +separate_class_name_and_id_with_underscore+ sets whether the method should put ‘_’ between the name and ‘id’.

Examples
“Message”.foreignKey -> “message_id” “Message”.foreignKey(false) -> “messageid” “Admin::Post”.foreignKey -> “post_id”
Return type:unicode
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.

Examples
“employee_salary” -> “Employee salary” “author_id” -> “Author”
Return type:unicode
irregular(singular, plural, first_letter_is_the_same=True)

Specifies a new irregular that applies to both pluralization and singularization at the same time.

Examples:
inf.irregular(‘octopus’, ‘octopi’) inf.irregular(‘person’, ‘people’)
Return type:None
ordinalize(number)

Ordinalize turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Examples
ordinalize(1) -> “1st” ordinalize(2) -> “2nd” ordinalize(1002) -> “1002nd” ordinalize(1003) -> “1003rd”
Return type:unicode
plural(rule, replacement)

Specifies a new pluralization rule and its replacement.

Return type:None
pluralize(word)

Returns the plural form of the word in the string.

Examples
“post”.pluralize -> “posts” “octopus”.pluralize -> “octopi” “sheep”.pluralize -> “sheep” “words”.pluralize -> “words” “the blue mailman”.pluralize -> “the blue mailmen” “CamelOctopus”.pluralize -> “CamelOctopi”
Return type:unicode
regular(plural_find, plural_replace, singular_find, singular_replace='')

Specified a new regular inflection.

Parameters:
  • plural_find (string) – regular expression pattern (which will be compiled)
  • plural_replace (string) – replacement (may contain back-references to regexp groups from plural_find)
  • singular_find (string) – regular expression pattern (which will be compiled)
  • singular_replace (string) – replacement (may contain back-references to regexp groups from singular_find)
Return type:

None

singular(rule, replacement)

Specifies a new singularization rule and its replacement.

Return type:None
singularize(word)

The reverse of pluralize, returns the singular form of a word in a string.

Examples
“posts”.singularize #=> “post” “octopi”.singularize #=> “octopus” “sheep”.singluarize #=> “sheep” “word”.singluarize #=> “word” “the blue mailmen”.singularize #=> “the blue mailman” “CamelOctopi”.singularize #=> “CamelOctopus”
Parameters:
  • word – a possibly plural word which should be converted to singular form.
Return type:

unicode

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.

Examples
“RawScaledScorer”.tableize -> “raw_scaled_scorers” “egg_and_ham”.tableize -> “egg_and_hams” “fancyCategory”.tableize -> “fancy_categories”
Return type:unicode
uncountable(*words)

Add uncountable words that shouldn’t be attempted inflected.

Examples:
uncountable “money” uncountable “money”), “information” uncountable %w( money information rice )
Parameters:
  • words – strings
Return type:

None

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.

Examples
“ActiveRecord”.underscore -> “active_record” “ActiveRecord::Errors”.underscore -> active_record/errors
Return type:unicode

Modules