Class: Dry::Inflector

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/inflector.rb,
lib/dry/inflector/rules.rb,
lib/dry/inflector/version.rb,
lib/dry/inflector/acronyms.rb,
lib/dry/inflector/inflections.rb,
lib/dry/inflector/inflections/defaults.rb

Overview

dry-inflector

Since:

  • 0.1.0

Defined Under Namespace

Classes: Acronyms, Inflections, Rules

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

"0.1.2"

Instance Method Summary collapse

Constructor Details

#initialize(&blk) {|the| ... } ⇒ Dry::Inflector

Instantiate the inflector

Examples:

Basic usage

require "dry/inflector"

inflector = Dry::Inflector.new

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

Parameters:

  • blk (Proc)

    an optional block to specify custom inflection rules

Yield Parameters:

Since:

  • 0.1.0



33
34
35
# File 'lib/dry/inflector.rb', line 33

def initialize(&blk)
  @inflections = Inflections.build(&blk)
end

Instance Method Details

#camelize(input) ⇒ String

Camelize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.camelize("dry/inflector") # => "Dry::Inflector"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the camelized string

Since:

  • 0.1.0



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dry/inflector.rb', line 49

def camelize(input)
  input = input.to_s.dup
  input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match) }
  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

#classify(input) ⇒ String

Classify a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.classify("books") # => "Book"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the classified string

Since:

  • 0.1.0



93
94
95
# File 'lib/dry/inflector.rb', line 93

def classify(input)
  camelize(singularize(input.to_s.sub(/.*\./, "")))
end

#constantize(input) ⇒ Class, Module

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

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.constantize("Module")         # => Module
inflector.constantize("Dry::Inflector") # => Dry::Inflector

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (Class, Module)

    the class or module

Since:

  • 0.1.0



77
78
79
# File 'lib/dry/inflector.rb', line 77

def constantize(input)
  Object.const_get(input)
end

#dasherize(input) ⇒ String

Dasherize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.dasherize("dry_inflector") # => "dry-inflector"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the dasherized string

Since:

  • 0.1.0



109
110
111
# File 'lib/dry/inflector.rb', line 109

def dasherize(input)
  input.to_s.tr("_", "-")
end

#demodulize(input) ⇒ String

Demodulize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.demodulize("Dry::Inflector") # => "Inflector"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the demodulized string

Since:

  • 0.1.0



125
126
127
# File 'lib/dry/inflector.rb', line 125

def demodulize(input)
  input.to_s.split("::").last
end

#foreign_key(input) ⇒ String

Creates a foreign key name

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.foreign_key("Message") => "message_id"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    foreign key

Since:

  • 0.1.0



164
165
166
# File 'lib/dry/inflector.rb', line 164

def foreign_key(input)
  "#{underscore(demodulize(input))}_id"
end

#humanize(input) ⇒ String

Humanize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.humanize("dry_inflector") # => "Dry inflector"
inflector.humanize("author_id")     # => "Author"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the humanized string

Since:

  • 0.1.0



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dry/inflector.rb', line 142

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, index.zero?)
  }.join(separator)
end

#ordinalize(number) ⇒ String

Ordinalize a number

Examples:

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"

Parameters:

  • number (Integer)

    the input

Returns:

  • (String)

    the ordinalized number

Since:

  • 0.1.0



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dry/inflector.rb', line 184

def ordinalize(number) # rubocop:disable Metrics/MethodLength
  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) ⇒ String

Pluralize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.pluralize("book")  # => "books"
inflector.pluralize("money") # => "money"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the pluralized string

Since:

  • 0.1.0



212
213
214
215
216
# File 'lib/dry/inflector.rb', line 212

def pluralize(input)
  input = input.to_s
  return input if uncountable?(input)
  inflections.plurals.apply_to(input)
end

#singularize(input) ⇒ String

Singularize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.singularize("books") # => "book"
inflector.singularize("money") # => "money"

Parameters:

  • input (String)

    the input

Returns:

  • (String)

    the singularized string

Since:

  • 0.1.0



231
232
233
234
235
# File 'lib/dry/inflector.rb', line 231

def singularize(input)
  input = input.to_s
  return input if uncountable?(input)
  inflections.singulars.apply_to(input)
end

#tableize(input) ⇒ String

Tableize a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.tableize("Book") # => "books"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the tableized string

Since:

  • 0.1.0



249
250
251
252
# File 'lib/dry/inflector.rb', line 249

def tableize(input)
  input = input.to_s.gsub(/::/, "_")
  pluralize(underscore(input))
end

#uncountable?(input) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the input is an uncountable word

Parameters:

  • input (String)

    the input

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



287
288
289
# File 'lib/dry/inflector.rb', line 287

def uncountable?(input)
  !(input =~ /\A[[:space:]]*\z/).nil? || inflections.uncountables.include?(input.downcase)
end

#underscore(input) ⇒ String

Underscore a string

Examples:

require "dry/inflector"

inflector = Dry::Inflector.new
inflector.underscore("dry-inflector") # => "dry_inflector"

Parameters:

  • input (String, Symbol)

    the input

Returns:

  • (String)

    the underscored string

Since:

  • 0.1.0



266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/dry/inflector.rb', line 266

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