Module: Strings::Inflection

Defined in:
lib/strings/inflection.rb,
lib/strings/inflection/noun.rb,
lib/strings/inflection/term.rb,
lib/strings/inflection/verb.rb,
lib/strings/inflection/nouns.rb,
lib/strings/inflection/verbs.rb,
lib/strings/inflection/parser.rb,
lib/strings/inflection/version.rb,
lib/strings/inflection/extensions.rb,
lib/strings/inflection/combined_noun.rb,
lib/strings/inflection/configuration.rb

Defined Under Namespace

Modules: Extensions, Nouns, Verbs Classes: CombinedNoun, Configuration, Error, Noun, Parser, Term, Verb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.configurationObject

A configuration object



33
34
35
# File 'lib/strings/inflection.rb', line 33

def configuration
  @configuration ||= Configuration.new
end

.configureObject

Configure custom inflections

Examples:

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


55
56
57
58
59
60
61
# File 'lib/strings/inflection.rb', line 55

def configure
  if block_given?
    yield configuration
  else
    configuration
  end
end

.inflect(word, count, term: :noun) ⇒ Object

Inflect a noun into a correct form

Examples:

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

Parameters:

  • word (String)

    the word to inflect

  • count (Integer)

    the count of items



89
90
91
92
93
# 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) ⇒ String

Join a list of words into a single sentence

Examples:

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

Parameters:

  • words (Array[String])

    the words to join

  • separator (String)

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

  • final_separator (String)

    the separator used before joining the last word

  • conjunctive (String)

    the word used for combining the last word with the rest

Returns:

  • (String)


197
198
199
# File 'lib/strings/inflection.rb', line 197

def join_words(*words, **options)
  CombinedNoun.new(words).join_words(**options)
end

.Noun(word) ⇒ Object

Create a noun object



17
18
19
# File 'lib/strings/inflection.rb', line 17

def Noun(word)
  Noun[word]
end

.plural?(word, term: :noun) ⇒ Boolean

Check if noun is in plural form

Examples:

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

Returns:

  • (Boolean)


166
167
168
169
170
171
172
173
174
175
# 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) ⇒ Object

Inflect a word to its plural form

Examples:

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

Parameters:

  • word (String)

    noun to inflect to plural form



126
127
128
129
130
131
132
133
# 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) ⇒ Object

Reset configuration and remove loaded inflections



41
42
43
# File 'lib/strings/inflection.rb', line 41

def reset(scope = :all)
  configuration.reset(scope)
end

.singular?(word, term: :noun) ⇒ Boolean

Check if noun is in singular form

Examples:

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

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object

Inflect a pural word to its singular form

Examples:

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

Parameters:

  • word (String)

    the noun to inflect to singular form



106
107
108
109
110
111
112
113
# 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) ⇒ Boolean

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 word is uncountable

Parameters:

  • word (String)

    the word to check

Returns:

  • (Boolean)


72
73
74
# File 'lib/strings/inflection.rb', line 72

def uncountable?(word)
  Noun[word].uncountable?
end

.Verb(word) ⇒ Object

Create a verb object



25
26
27
# File 'lib/strings/inflection.rb', line 25

def Verb(word)
  Verb[word]
end