Module | Inflecto |
In: |
lib/inflecto.rb
lib/inflecto/inflections.rb |
The Inflecto transforms words from singular to plural, class names to table names, modularized class names to ones without, and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept in inflections.rb.
The Rails core team has stated patches for the inflections library will not be accepted in order to avoid breaking legacy applications which may be relying on errant inflections. If you discover an incorrect inflection and require it for your application, you‘ll need to correct it yourself (explained below).
Convert input to UpperCamelCase
Will also convert ’/’ to ’::’ which is useful for converting paths to namespaces.
@param [String] input
@example
Inflecto.camelize("data_mapper") # => "DataMapper" Inflecto.camelize("data_mapper/errors") # => "DataMApper::Errors"
@return [String]
@api public
Classify input
Create a class name from a plural table name like Rails does for table names to models. Note that this returns a string and not a Class.
To convert to an actual class # follow classify with constantize.
@examples:
Inflecto.classify("egg_and_hams") # => "EggAndHam" Inflecto.classify("posts") # => "Post" # Singular names are not handled correctly: Inflecto.classify("business") # => "Busines"
@return [String]
@api private
Find a constant with the name specified in the argument string
The name is assumed to be the one of a top-level constant, constant scope of caller is igored
@param [String] input
@example
Inflecto.constantize("Module") # => Module Inflecto.constantize("DataMapper::Error") # => Test::Unit
@return [Class, Module]
@api private
Convert input underscores to dashes
@param [String] input
@example
Inflecto.dasherize("foo_bar") # => "foo-bar"
@return [String]
@api public
Return unscoped constant name
@param [String] input
@example
Inflecto.demodulize("DataMapper::Error") # => "Error" Inflecto.demodulize("DataMapper") # => "DataMapper"
@return [String]
@api public
Creates a foreign key name
@param [String] input
@example
Inflecto.foreign_key("Message) => "message_id"
@return [String]
@api private
Humanize string
@param [String] input
capitalizes the first word and turns underscores into spaces and strips a # trailing "_id", if any. Like titleize, this is meant for creating pretty output.
@example
Inflecto.humanize("employee_salary") # => "Employee salary" Inflecto.humanize("author_id") # => "Author"
@return [String]
@api private
Yields a singleton instance of Inflecto::Inflections
@example
Inflecto.inflections do |inflect| inflect.uncountable "rails" end
@return [Inflecto::Inflections]
@api public
Convert a number into an ordinal string
@param [Fixnum] number
@example
ordinalize(1) # => "1st" ordinalize(2) # => "2nd" ordinalize(1002) # => "1002nd" ordinalize(1003) # => "1003rd"
@return [String]
@api private
Convert input word string to plural
@param [String] word
@example
Inflecto.pluralize("post") # => "posts" Inflecto.pluralize("octopus") # => "octopi" Inflecto.pluralize("sheep") # => "sheep" Inflecto.pluralize("words") # => "words" Inflecto.pluralize("CamelOctopus") # => "CamelOctopi"
@return [String]
@api public
Convert word to singular
@param [String] word
@example
Inflecto.singularize("posts") # => "post" Inflecto.singularize("octopi") # => "octopus" Inflecto.singularize("sheep") # => "sheep" Inflecto.singularize("word") # => "word" Inflecto.singularize("CamelOctopi") # => "CamelOctopus"
@return [String]
@api public
Tabelize input string
@param [String] input
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
Inflecto.tabelize("RawScaledScorer") # => "raw_scaled_scorers" Inflecto.tabelize("egg_and_ham") # => "egg_and_hams" Inflecto.tabelize("fancyCategory") # => "fancy_categories"
@return [String]
@api private