class Dry::Inflector
- Dry::Inflector
- Reference
- Object
Overview
dry-inflector
@since 0.1.0
Defined in:
dry/inflector.crdry/inflector/acronyms.cr
dry/inflector/inflections.cr
dry/inflector/inflections/defaults.cr
dry/inflector/rules.cr
dry/inflector/version.cr
Constant Summary
-
VERSION =
"1.0.0"
-
@since 0.1.0
Constructors
-
.new(&blk : Inflections -> _)
Instantiate the inflector
- .new
Instance Method Summary
- #camelize(input)
-
#camelize_lower(input)
Lower camelize a string
-
#camelize_upper(input)
Upper camelize a string
-
#classify(input)
Classify a string
-
#constantize(input)
Find a constant with the name specified in the argument string
-
#dasherize(input)
Dasherize a string
-
#demodulize(input)
Demodulize a string
-
#foreign_key(input)
Creates a foreign key name
-
#humanize(input)
Humanize a string
-
#inspect
Returns an unambiguous and information-rich string representation of this object, typically intended for developers.
-
#ordinalize(number)
Ordinalize a number
-
#pluralize(input)
Pluralize a string
-
#singularize(input)
Singularize a string
-
#tableize(input)
Tableize a string
-
#to_s
@return [String]
-
#uncountable?(input)
Check if the input is an uncountable word
-
#underscore(input)
Underscore a string
Constructor Detail
Instantiate the inflector
@param blk [Proc] an optional block to specify custom inflection rules @yieldparam [Dry::Inflector::Inflections] the inflection rules
@return [Dry::Inflector] the inflector
@since 0.1.0
@example Basic usage require "dry/inflector"
inflector = Dry::Inflector.new
@example Custom inflection rules require "dry/inflector"
inflector = Dry::Inflector.new do |inflections| inflections.plural "virus", "viruses" # specify a rule for #pluralize inflections.singular "thieves", "thief" # specify a rule for #singularize inflections.uncountable "dry-inflector" # add an exception for an uncountable word end
Instance Method Detail
Lower camelize a string
@param input [String,Symbol] the input @return [String] the lower camelized string
@since 0.1.3
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.camelize_lower("data_mapper") # => "dataMapper"
Upper camelize a string
@param input [String,Symbol] the input @return [String] the upper camelized string
@since 0.1.3
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.camelize_upper("data_mapper") # => "DataMapper" inflector.camelize_upper("dry/inflector") # => "Dry::Inflector"
Classify a string
@param input [String,Symbol] the input @return [String] the classified string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.classify("books") # => "Book"
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 ignored
@param input [String,Symbol] the input @return [Class, Module] the class or module
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.constantize("Module") # => Module inflector.constantize("Dry::Inflector") # => Dry::Inflector
Dasherize a string
@param input [String,Symbol] the input @return [String] the dasherized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.dasherize("dry_inflector") # => "dry-inflector"
Demodulize a string
@param input [String,Symbol] the input @return [String] the demodulized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.demodulize("Dry::Inflector") # => "Inflector"
Creates a foreign key name
@param input [String, Symbol] the input @return [String] foreign key
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.foreign_key("Message") => "message_id"
Humanize a string
@param input [String,Symbol] the input @return [String] the humanized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.humanize("dry_inflector") # => "Dry inflector" inflector.humanize("author_id") # => "Author"
Returns an unambiguous and information-rich string representation of this object, typically intended for developers.
This method should usually not be overridden. It delegates to
#inspect(IO)
which can be overridden for custom implementations.
Also see #to_s
.
Ordinalize a number
@param number [Integer] the input @return [String] the ordinalized number
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.ordinalize(1) # => "1st" inflector.ordinalize(2) # => "2nd" inflector.ordinalize(3) # => "3rd" inflector.ordinalize(10) # => "10th" inflector.ordinalize(23) # => "23rd"
Pluralize a string
@param input [String,Symbol] the input @return [String] the pluralized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.pluralize("book") # => "books" inflector.pluralize("money") # => "money"
Singularize a string
@param input [String] the input @return [String] the singularized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.singularize("books") # => "book" inflector.singularize("money") # => "money"
Tableize a string
@param input [String,Symbol] the input @return [String] the tableized string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.tableize("Book") # => "books"
Check if the input is an uncountable word
@param input [String] the input @return [TrueClass,FalseClass] the result of the check
@since 0.1.0 @api private
Underscore a string
@param input [String,Symbol] the input @return [String] the underscored string
@since 0.1.0
@example require "dry/inflector"
inflector = Dry::Inflector.new inflector.underscore("dry-inflector") # => "dry_inflector"