Class: SleepingKingStudios::Tools::StringTools::PluralInflector
- Inherits:
-
Object
- Object
- SleepingKingStudios::Tools::StringTools::PluralInflector
- 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
-
#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”.
-
#define_plural_rule(match, replace) ⇒ Object
Defines a rule for pluralization.
-
#define_singular_rule(match, replace) ⇒ Object
Defines a rule for singularization.
-
#define_uncountable_word(word) ⇒ Object
Defines an uncountable word, such as “data”.
-
#pluralize(str) ⇒ String
Takes a word in singular form and returns the plural form, based on the defined rules and known irregular/uncountable words.
-
#singularize(str) ⇒ String
Takes a word in plural form and returns the singular form, based on the defined rules and known irregular/uncountable words.
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”.
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.
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.
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.
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
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).
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.
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 |