Class: SleepingKingStudios::Tools::Toolbox::Inflector::Rules

Inherits:
Object
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb

Overview

Rules for inflecting words.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(irregular_words: nil, plural_rules: nil, singular_rules: nil, uncountable_words: nil) ⇒ Rules

Returns a new instance of Rules.

Parameters:

  • irregular_words (Hash<String, String>) (defaults to: nil)

    irregular word pairs in singular => plural order, e.g. “child” => “children”.

  • plural_rules (Array<Array<(Regexp, String)>>) (defaults to: nil)

    rules for pluralizing words.

  • singular_rules (Array<Array<(Regexp, String)>>) (defaults to: nil)

    rules for singularizing words.

  • uncountable_words (Array<String>) (defaults to: nil)

    uncountable words e.g. “data”.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 17

def initialize(
  irregular_words:   nil,
  plural_rules:      nil,
  singular_rules:    nil,
  uncountable_words: nil
)
  @plural_rules      = plural_rules    || default_plural_rules
  @singular_rules    = singular_rules  || default_singular_rules
  @irregular_words   = irregular_words || default_irregular_words
  @uncountable_words =
    Set.new(uncountable_words || default_uncountable_words)

  @irregular_words_reversed = reverse_hash(@irregular_words)
end

Instance Attribute Details

#irregular_wordsArray<Array<(String, String)>> (readonly)

Returns irregular word pairs in singular => plural order.

Returns:

  • (Array<Array<(String, String)>>)

    irregular word pairs in singular => plural order.



34
35
36
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 34

def irregular_words
  @irregular_words
end

#irregular_words_reversedArray<Array<(String, String)>> (readonly)

Returns irregular word pairs in plural => singular order.

Returns:

  • (Array<Array<(String, String)>>)

    irregular word pairs in plural => singular order.



38
39
40
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 38

def irregular_words_reversed
  @irregular_words_reversed
end

#plural_rulesArray<Array<(Regexp, String)>> (readonly)

Returns rules for pluralizing words.

Returns:

  • (Array<Array<(Regexp, String)>>)

    rules for pluralizing words.



41
42
43
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 41

def plural_rules
  @plural_rules
end

#singular_rulesArray<Array<(Regexp, String)>> (readonly)

Returns rules for singularizing words.

Returns:

  • (Array<Array<(Regexp, String)>>)

    rules for singularizing words.



44
45
46
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 44

def singular_rules
  @singular_rules
end

#uncountable_wordsArray<String> (readonly)

Returns uncountable words.

Returns:

  • (Array<String>)

    uncountable words.



47
48
49
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 47

def uncountable_words
  @uncountable_words
end

Instance Method Details

#define_irregular_word(singular, plural) ⇒ self

Defines an irregular word pair.

Parameters:

  • singular (String)

    the singular form of the word.

  • plural (String)

    the plural form of the word.

Returns:

  • (self)

    the rules object.



55
56
57
58
59
60
61
62
63
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 55

def define_irregular_word(singular, plural)
  validate_string(singular)
  validate_string(plural)

  @irregular_words[singular]        = plural
  @irregular_words_reversed[plural] = singular

  self
end

#define_plural_rule(pattern, replace) ⇒ self

Defines a pluralization rule.

Parameters:

  • pattern (Regexp)

    the pattern to match.

  • replace (String)

    the string to replace.

Returns:

  • (self)

    the rules object.



71
72
73
74
75
76
77
78
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 71

def define_plural_rule(pattern, replace)
  validate_pattern(pattern)
  validate_string(replace, as: 'replace')

  @plural_rules.unshift([pattern, replace])

  self
end

#define_singular_rule(pattern, replace) ⇒ self

Defines a singularization rule.

Parameters:

  • pattern (Regexp)

    the pattern to match.

  • replace (String)

    the string to replace.

Returns:

  • (self)

    the rules object.



86
87
88
89
90
91
92
93
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 86

def define_singular_rule(pattern, replace)
  validate_pattern(pattern)
  validate_string(replace, as: 'replace')

  @singular_rules.unshift([pattern, replace])

  self
end

#define_uncountable_word(word) ⇒ self

Defines an uncountable word.

Parameters:

  • word (String)

    the uncountable word.

Returns:

  • (self)

    the rules object.



100
101
102
103
104
105
106
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 100

def define_uncountable_word(word)
  validate_string(word)

  @uncountable_words << word

  self
end

#inspectString

Returns a human-readable representation of the rules object.

Returns:

  • (String)

    a human-readable representation of the rules object.



109
110
111
# File 'lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb', line 109

def inspect
  "#<SleepingKingStudios::Tools::Toolbox::Inflector::Rules:#{object_id}>"
end