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



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

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)>] Hash of irregular word pairs in singular => plural order. (readonly)



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

def irregular_words
  @irregular_words
end

#irregular_words_reversedArray<Array<(String, String)>] Hash of irregular word pairs in plural => singular order. (readonly)



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

def irregular_words_reversed
  @irregular_words_reversed
end

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



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

def plural_rules
  @plural_rules
end

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



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

def singular_rules
  @singular_rules
end

#uncountable_wordsArray<String> (readonly)



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

def uncountable_words
  @uncountable_words
end

Instance Method Details

#define_irregular_word(singular, plural) ⇒ Rules

Defines an irregular word pair.



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

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) ⇒ Rules

Defines a pluralization rule.



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

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) ⇒ Rules

Defines a singularization rule.



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

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) ⇒ Rules

Defines an uncountable word.



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

def define_uncountable_word(word)
  validate_string(word)

  @uncountable_words << word

  self
end

#inspectString



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

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