Class: SleepingKingStudios::Tools::StringTools::PluralInflector

Inherits:
Object
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb

Overview

Inflector class to handle word pluralization using a set of defined rules and irregular/uncountable words.

Instance Method Summary collapse

Instance Method Details

#define_irregular_word(singular, plural) ⇒ Object

Defines an irregular word, which is a singular and plural word pair that do not obey any defined rule, such as “goose” and “geese”.

Parameters:

  • singular (String)

    The singular form of the word.

  • plural (String)

    The plural form of the word.



14
15
16
17
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 14

def define_irregular_word singular, plural
  irregular_words[singular]       = plural
  inverse_irregular_words[plural] = singular
end

#define_plural_rule(match, replace) ⇒ Object

Defines a rule for pluralization. The rule will be applied to any words that match the first parameter, performing a replace on the word using the second parameter.

Rules are applied in reverse order of definition, meaning that rules defined later will take precedence over previously defined rules.

Parameters:

  • match (Regexp)

    The matching rule.

  • replace (String)

    The replacement string.



28
29
30
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 28

def define_plural_rule match, replace
  plural_rules.unshift [match, replace]
end

#define_singular_rule(match, replace) ⇒ Object

Defines a rule for singularization. The rule will be applied to any words that match the first parameter, performing a replace on the word using the second parameter.

Rules are applied in reverse order of definition, meaning that rules defined later will take precedence over previously defined rules.

Parameters:

  • match (Regexp)

    The matching rule.

  • replace (String)

    The replacement string.



41
42
43
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 41

def define_singular_rule match, replace
  singular_rules.unshift [match, replace]
end

#define_uncountable_word(word) ⇒ Object

Defines an uncountable word, such as “data”. If #pluralize or #singularize is called with an uncountable word as its parameter, it will return the unmodified word.

Parameters:

  • word (String)

    The uncountable word.



50
51
52
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 50

def define_uncountable_word word
  uncountable_words << word
end

#pluralize(str) ⇒ String

Note:

The defined rules and exceptions are deliberately basic. Each application is responsible for defining its own pluralization rules using this framework.

Takes a word in singular form and returns the plural form, based on the defined rules and known irregular/uncountable words.

First, checks if the word is known to be uncountable (see #define_uncountable_word). Then, checks if the word is known to be irregular (see #define_irregular_word). Finally, iterates through the defined plural rules from most recently defined to first defined (see #define_plural_rule).

Parameters:

  • str (String)

    The word to pluralize.

Returns:

  • (String)

    The pluralized word.

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 72

def pluralize str
  str        = str.to_s.strip
  normalized = str.downcase

  uncountable_words.each do |word|
    return str if word == normalized
  end # each

  return str if inverse_irregular_words.key?(normalized)

  return irregular_words[normalized] if irregular_words.key?(normalized)

  plural_rules.each do |match, replace|
    next unless str =~ match

    return str.sub(match, replace)
  end # each

  str
end

#singularize(str) ⇒ String

Takes a word in plural form and returns the singular form, based on the defined rules and known irregular/uncountable words.

Parameters:

  • str (String)

    The word to singularize.

Returns:

  • (String)

    The singularized word.

See Also:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sleeping_king_studios/tools/string_tools/plural_inflector.rb', line 101

def singularize str
  str        = str.to_s.strip
  normalized = str.downcase

  uncountable_words.each do |word|
    return str if word == normalized
  end # each

  return inverse_irregular_words[normalized] if inverse_irregular_words.key?(normalized)

  singular_rules.each do |match, replace|
    next unless str =~ match

    return str.sub(match, replace)
  end # each

  str
end