module Wordsmith::Inflector

Extended Modules

Defined in:

wordsmith/inflector/inflections.cr
wordsmith/inflector/methods.cr

Instance Method Summary

Instance Method Detail

def camelize(term, uppercase_first_letter = true) #

Convert a given word to the camel-case version of that word.

Optionally, a second parameter can be provided that controls whether or not the first letter is capitalized.

Examples:

Wordsmith::Inflector.camelize("application_controller")                                # => "ApplicationController"
Wordsmith::Inflector.camelize("application_controller", uppercase_first_letter: false) # => "applicationController"

[View source]
def classify(table_name) #

Convert a given table name to the class name for that table.

Examples:

Wordsmith::Inflector.classify("users")               # => "User"
Wordsmith::Inflector.classify("people")              # => "Person"
Wordsmith::Inflector.classify("schema.users")        # => "User"
Wordsmith::Inflector.classify("schema.public.users") # => "User"

[View source]
def dasherize(underscored_word) #

Convert a given underscore-separated word to the same word, separated by dashes.

Example:

Wordsmith::Inflector.dasherize("post_office") # => "post-office"

[View source]
def deconstantize(path) #

Remove any trailing constants from the provided path.

Example:

Wordsmith::Inflector.deconstantize("Helpers::Mixins::User::FREE_TIER_COMMENTS") # => "Helpers::Mixins::User"

[View source]
def demodulize(path) #

Remove leading modules from a provided class name path.

Example:

Wordsmith::Inflector.demodulize("Helpers::Mixins::User") # => "User"

[View source]
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) #

Determine the foreign key representation of a given class name.

Example:

Wordsmith::Inflector.foreign_key("Person") # => "person_id"

[View source]
def humanize(lower_case_and_underscored_word, capitalize = true, keep_id_suffix = false) #

Convert a given word to the human-friendly version of that word.

Capitalization and whether or not to retain an _id suffix can be controlled with optional parameters.

Examples:

Wordsmith::Inflector.humanize("employee_id")                       # => "Employee"
Wordsmith::Inflector.humanize("employee_id", capitalize: false)    # => "employee"
Wordsmith::Inflector.humanize("employee_id", keep_id_suffix: true) # => "Employee id"

[View source]
def inflections #

Return the current set of stored inflection logic.


[View source]
def ordinal(number) #

Determine the ordinal suffix for a given number.

Example:

Wordsmith::Inflector.ordinal(1) # => "st"
Wordsmith::Inflector.ordinal(2) # => "nd"
Wordsmith::Inflector.ordinal(3) # => "rd"
Wordsmith::Inflector.ordinal(4) # => "th"

[View source]
def ordinalize(number) #

Given a number, return the number with the correct ordinal suffix.

Example:

Wordsmith::Inflector.ordinalize(1) # => "1st"
Wordsmith::Inflector.ordinalize(2) # => "2nd"
Wordsmith::Inflector.ordinalize(3) # => "3rd"
Wordsmith::Inflector.ordinalize(4) # => "4th"

[View source]
def parameterize(content : String, separator : String | Nil = "-", preserve_case : Bool = false) #

Convert the given string to a parameter-friendly version.

The used separator and whether or not to preserve the original object case can be controlled through optional parameters.

Examples:

Wordsmith::Inflector.parameterize("Admin/product")                       # => "admin-product"
Wordsmith::Inflector.parameterize("Admin::Product", separator: "_")      # => "admin_product"
Wordsmith::Inflector.parameterize("Admin::Product", preserve_case: true) # => "Admin-Product"

[View source]
def pluralize(word) #

Given a word, return the plural version of that word.

Example:

Wordsmith::Inflector.pluralize("sandal") # => "sandals"
Wordsmith::Inflector.pluralize("person") # => "people"
Wordsmith::Inflector.pluralize("people") # => "people"

[View source]
def singularize(word) #

Given a word, return the singular version of that word.

Example:

Wordsmith::Inflector.singularize("sandals") # => "sandal"
Wordsmith::Inflector.singularize("people")  # => "person"
Wordsmith::Inflector.singularize("person")  # => "person"

[View source]
def tableize(class_name) #

Convert a given class name to the database table name for that class.

Examples:

Wordsmith::Inflector.tableize("User")   # => "users"
Wordsmith::Inflector.tableize("Person") # => "people"

[View source]
def titleize(word, keep_id_suffix = false) #

Convert a given word to the titleized version of that word, which generally means each word is capitalized.

Example:

Wordsmith::Inflector.titleize("amazon web services") # => "Amazon Web Services"

[View source]
def underscore(camel_cased_word) #

Convert a given camel-case word to the underscored version of that word.

Example:

Wordsmith::Inflector.underscore("ApplicationController") # => "application_controller"

[View source]
def upcase_first(string) #

Capitalize the first letter of a given word.

Example:

Wordsmith::Inflector.upcase_first("lucky") # => "Lucky"

[View source]