module Strings::Inflection

Constants

VERSION

Public Class Methods

Noun(word) click to toggle source

Create a noun object

@api public

# File lib/strings/inflection.rb, line 17
def Noun(word)
  Noun[word]
end
Verb(word) click to toggle source

Create a verb object

@api public

# File lib/strings/inflection.rb, line 25
def Verb(word)
  Verb[word]
end
configuration() click to toggle source

A configuration object

@api public

# File lib/strings/inflection.rb, line 33
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Configure custom inflections

@example

configure do |config|
  config.plural "index", "indexes"
  config.singular "axes", "ax"
end

@api public

# File lib/strings/inflection.rb, line 55
def configure
  if block_given?
    yield configuration
  else
    configuration
  end
end
inflect(word, count, term: :noun) click to toggle source

Inflect a noun into a correct form

@example

Strings::Inflection.inflect("error", 3)
# => "errors"

@param [String] word

the word to inflect

@param [Integer] count

the count of items

@api public

# File lib/strings/inflection.rb, line 89
def inflect(word, count, term: :noun)
  template = !!(word =~ /\{\{([^\}]+)\}\}/)

  Parser.parse(template ? word : "{{#{term.upcase[0]}:#{word}}}", count)
end
join_words(*words, **options) click to toggle source

Join a list of words into a single sentence

@example

Strings::Inflection.join_words("one", "two", "three")
# => "one, two and three"

@param [Array] words

the words to join

@param [String] separator

the character to use to join words, defaults to `,`

@param [String] final_separator

the separator used before joining the last word

@param [String] conjunctive

the word used for combining the last word with the rest

@return [String]

@api public

# File lib/strings/inflection.rb, line 197
def join_words(*words, **options)
  CombinedNoun.new(words).join_words(**options)
end
plural?(word, term: :noun) click to toggle source

Check if noun is in plural form

@example

Strings::Inflection.plural?("errors")
# => true

@return [Boolean]

@api public

# File lib/strings/inflection.rb, line 166
def plural?(word, term: :noun)
  case term.to_sym
  when :noun, :n
    Noun[word].plural?
  when :verb, :v
    Verb[word].plural?
  else
    raise Error, "Unknown option '#{term}' as a term"
  end
end
pluralize(word, term: :noun) click to toggle source

Inflect a word to its plural form

@example

Strings::Inflection.pluralize("error")
# => "errors"

@param [String] word

noun to inflect to plural form

@api public

# File lib/strings/inflection.rb, line 126
def pluralize(word, term: :noun)
  case term
  when :noun, :n
    Noun[word].plural
  when :verb, :v
    Verb[word].plural
  end
end
reset(scope = :all) click to toggle source

Reset configuration and remove loaded inflections

@api public

# File lib/strings/inflection.rb, line 41
def reset(scope = :all)
  configuration.reset(scope)
end
singular?(word, term: :noun) click to toggle source

Check if noun is in singular form

@example

Strings::Inflection.singular?("error")
# => true

@return [Boolean]

@api public

# File lib/strings/inflection.rb, line 145
def singular?(word, term: :noun)
  case term.to_sym
  when :noun, :n
    Noun[word].singular?
  when :verb, :v
    Verb[word].singular?
  else
    raise Error, "Unknown option '#{term}' as a term"
  end
end
singularize(word, term: :noun) click to toggle source

Inflect a pural word to its singular form

@example

Strings::Inflection.singularize("errors")
# => "error"

@param [String] word

the noun to inflect to singular form

@api public

# File lib/strings/inflection.rb, line 106
def singularize(word, term: :noun)
  case term
  when :noun, :n
    Noun[word].singular
  when :verb, :v
    Verb[word].singular
  end
end
uncountable?(word) click to toggle source

Check if word is uncountable

@param [String] word

the word to check

@return [Boolean]

@api private

# File lib/strings/inflection.rb, line 72
def uncountable?(word)
  Noun[word].uncountable?
end

Private Instance Methods

Noun(word) click to toggle source

Create a noun object

@api public

# File lib/strings/inflection.rb, line 17
def Noun(word)
  Noun[word]
end
Verb(word) click to toggle source

Create a verb object

@api public

# File lib/strings/inflection.rb, line 25
def Verb(word)
  Verb[word]
end
configuration() click to toggle source

A configuration object

@api public

# File lib/strings/inflection.rb, line 33
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Configure custom inflections

@example

configure do |config|
  config.plural "index", "indexes"
  config.singular "axes", "ax"
end

@api public

# File lib/strings/inflection.rb, line 55
def configure
  if block_given?
    yield configuration
  else
    configuration
  end
end
inflect(word, count, term: :noun) click to toggle source

Inflect a noun into a correct form

@example

Strings::Inflection.inflect("error", 3)
# => "errors"

@param [String] word

the word to inflect

@param [Integer] count

the count of items

@api public

# File lib/strings/inflection.rb, line 89
def inflect(word, count, term: :noun)
  template = !!(word =~ /\{\{([^\}]+)\}\}/)

  Parser.parse(template ? word : "{{#{term.upcase[0]}:#{word}}}", count)
end
join_words(*words, **options) click to toggle source

Join a list of words into a single sentence

@example

Strings::Inflection.join_words("one", "two", "three")
# => "one, two and three"

@param [Array] words

the words to join

@param [String] separator

the character to use to join words, defaults to `,`

@param [String] final_separator

the separator used before joining the last word

@param [String] conjunctive

the word used for combining the last word with the rest

@return [String]

@api public

# File lib/strings/inflection.rb, line 197
def join_words(*words, **options)
  CombinedNoun.new(words).join_words(**options)
end
plural?(word, term: :noun) click to toggle source

Check if noun is in plural form

@example

Strings::Inflection.plural?("errors")
# => true

@return [Boolean]

@api public

# File lib/strings/inflection.rb, line 166
def plural?(word, term: :noun)
  case term.to_sym
  when :noun, :n
    Noun[word].plural?
  when :verb, :v
    Verb[word].plural?
  else
    raise Error, "Unknown option '#{term}' as a term"
  end
end
pluralize(word, term: :noun) click to toggle source

Inflect a word to its plural form

@example

Strings::Inflection.pluralize("error")
# => "errors"

@param [String] word

noun to inflect to plural form

@api public

# File lib/strings/inflection.rb, line 126
def pluralize(word, term: :noun)
  case term
  when :noun, :n
    Noun[word].plural
  when :verb, :v
    Verb[word].plural
  end
end
reset(scope = :all) click to toggle source

Reset configuration and remove loaded inflections

@api public

# File lib/strings/inflection.rb, line 41
def reset(scope = :all)
  configuration.reset(scope)
end
singular?(word, term: :noun) click to toggle source

Check if noun is in singular form

@example

Strings::Inflection.singular?("error")
# => true

@return [Boolean]

@api public

# File lib/strings/inflection.rb, line 145
def singular?(word, term: :noun)
  case term.to_sym
  when :noun, :n
    Noun[word].singular?
  when :verb, :v
    Verb[word].singular?
  else
    raise Error, "Unknown option '#{term}' as a term"
  end
end
singularize(word, term: :noun) click to toggle source

Inflect a pural word to its singular form

@example

Strings::Inflection.singularize("errors")
# => "error"

@param [String] word

the noun to inflect to singular form

@api public

# File lib/strings/inflection.rb, line 106
def singularize(word, term: :noun)
  case term
  when :noun, :n
    Noun[word].singular
  when :verb, :v
    Verb[word].singular
  end
end
uncountable?(word) click to toggle source

Check if word is uncountable

@param [String] word

the word to check

@return [Boolean]

@api private

# File lib/strings/inflection.rb, line 72
def uncountable?(word)
  Noun[word].uncountable?
end