class Dry::Inflector

dry-inflector

@since 0.1.0

Constants

DEFAULT_SEPARATOR

@since 0.1.2 @api private

ORDINALIZE_TH

@since 0.1.0 @api private

VERSION

@since 0.1.0

Attributes

inflections[R]

Public Class Methods

new(&blk) click to toggle source

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
# File lib/dry/inflector.rb, line 33
def initialize(&blk)
  @inflections = Inflections.build(&blk)
end

Public Instance Methods

camelize(input)
Alias for: camelize_upper
camelize_lower(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 49
def camelize_lower(input)
  internal_camelize(input, false)
end
camelize_upper(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 66
def camelize_upper(input)
  internal_camelize(input, true)
end
Also aliased as: camelize
classify(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 104
def classify(input)
  camelize(singularize(input.to_s.sub(/.*\./, "")))
end
constantize(input) click to toggle source

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
# File lib/dry/inflector.rb, line 88
def constantize(input)
  Object.const_get(input, false)
end
dasherize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 120
def dasherize(input)
  input.to_s.tr("_", "-")
end
demodulize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 136
def demodulize(input)
  input.to_s.split("::").last
end
foreign_key(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 175
def foreign_key(input)
  "#{underscore(demodulize(input))}_id"
end
humanize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 153
def humanize(input)
  input = input.to_s
  result = inflections.humans.apply_to(input)
  result.chomp!("_id")
  result.tr!("_", " ")
  match = /(?<separator>\W)/.match(result)
  separator = match ? match[:separator] : DEFAULT_SEPARATOR
  result.split(separator).map.with_index { |word, index|
    inflections.acronyms.apply_to(word, capitalize: index.zero?)
  }.join(separator)
end
inspect()
Alias for: to_s
ordinalize(number) click to toggle source

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"
# File lib/dry/inflector.rb, line 195
def ordinalize(number)
  abs_value = number.abs

  if ORDINALIZE_TH.key?(abs_value % 100)
    "#{number}th"
  else
    case abs_value % 10
    when 1 then "#{number}st"
    when 2 then "#{number}nd"
    when 3 then "#{number}rd"
    else        "#{number}th"
    end
  end
end
pluralize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 223
def pluralize(input)
  input = input.to_s
  return input if uncountable?(input)

  inflections.plurals.apply_to(input)
end
singularize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 243
def singularize(input)
  input = input.to_s
  return input if uncountable?(input)

  inflections.singulars.apply_to(input)
end
tableize(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 262
def tableize(input)
  input = input.to_s.gsub(/::/, "_")
  pluralize(underscore(input))
end
to_s() click to toggle source

@return [String]

@since 0.2.0 @api public

# File lib/dry/inflector.rb, line 308
def to_s
  "#<Dry::Inflector>"
end
Also aliased as: inspect
uncountable?(input) click to toggle source

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

# File lib/dry/inflector.rb, line 300
def uncountable?(input)
  !(input =~ /\A[[:space:]]*\z/).nil? || inflections.uncountables.include?(input.downcase)
end
underscore(input) click to toggle source

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"
# File lib/dry/inflector.rb, line 279
def underscore(input)
  input = input.to_s.gsub("::", "/")
  input.gsub!(inflections.acronyms.regex) do
    m1 = Regexp.last_match(1)
    m2 = Regexp.last_match(2)
    "#{m1 ? "_" : ""}#{m2.downcase}"
  end
  input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  input.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  input.tr!("-", "_")
  input.downcase!
  input
end

Private Instance Methods

internal_camelize(input, upper) click to toggle source

@since 0.1.3 @api private

# File lib/dry/inflector.rb, line 327
def internal_camelize(input, upper)
  input = input.to_s.dup
  input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, capitalize: upper) }
  input.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do
    m1 = Regexp.last_match(1)
    m2 = Regexp.last_match(2)
    "#{m1}#{inflections.acronyms.apply_to(m2)}"
  end
  input.gsub!("/", "::")
  input
end