module Strings::Inflection
Constants
- VERSION
Public Class Methods
Create a noun object
@api public
# File lib/strings/inflection.rb, line 17 def Noun(word) Noun[word] end
Create a verb object
@api public
# File lib/strings/inflection.rb, line 25 def Verb(word) Verb[word] end
A configuration object
@api public
# File lib/strings/inflection.rb, line 33 def configuration @configuration ||= Configuration.new end
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 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 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
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
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 configuration and remove loaded inflections
@api public
# File lib/strings/inflection.rb, line 41 def reset(scope = :all) configuration.reset(scope) end
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
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
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
Create a noun object
@api public
# File lib/strings/inflection.rb, line 17 def Noun(word) Noun[word] end
Create a verb object
@api public
# File lib/strings/inflection.rb, line 25 def Verb(word) Verb[word] end
A configuration object
@api public
# File lib/strings/inflection.rb, line 33 def configuration @configuration ||= Configuration.new end
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 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 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
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
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 configuration and remove loaded inflections
@api public
# File lib/strings/inflection.rb, line 41 def reset(scope = :all) configuration.reset(scope) end
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
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
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