class SleepingKingStudios::Tools::Toolbox::Inflector::Rules
Rules
for inflecting words.
Attributes
@return [Array<Array<(String, String)>] Hash of irregular word pairs in
singular => plural order.
@return [Array<Array<(String, String)>] Hash of irregular word pairs in
plural => singular order.
@return [Array<Array<(Regexp, String)>>] Rules
for pluralizing words.
@return [Array<Array<(Regexp, String)>>] Rules
for singularizing words.
@return [Array<String>] List of uncountable words.
Public Class Methods
@param irregular_words
[Hash<String, String>] Hash of irregular word
pairs in singular => plural order, e.g. "child" => "children".
@param plural_rules
[Array<Array<(Regexp, String)>>] Rules
for
pluralizing words.
@param singular_rules
[Array<Array<(Regexp, String)>>] Rules
for
singularizing words.
@param uncountable_words
[Array<String>] List of uncountable words,
e.g. "data".
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 18 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
Public Instance Methods
Defines an irregular word pair.
@param singular [String] The singular form of the word. @param plural [String] The plural form of the word.
@return [Rules] The rules object.
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 56 def define_irregular_word(singular, plural) validate_string(singular) validate_string(plural) @irregular_words[singular] = plural @irregular_words_reversed[plural] = singular self end
Defines a pluralization rule.
@param pattern [Regexp] The pattern to match. @param replace [String] The string to replace.
@return [Rules] The rules object.
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 72 def define_plural_rule(pattern, replace) validate_pattern(pattern) validate_string(replace, as: 'replace') @plural_rules.unshift([pattern, replace]) self end
Defines a singularization rule.
@param pattern [Regexp] The pattern to match. @param replace [String] The string to replace.
@return [Rules] The rules object.
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 87 def define_singular_rule(pattern, replace) validate_pattern(pattern) validate_string(replace, as: 'replace') @singular_rules.unshift([pattern, replace]) self end
Defines an uncountable word.
@param word [String] The uncountable word.
@return [Rules] The rules object.
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 101 def define_uncountable_word(word) validate_string(word) @uncountable_words << word self end
@return [String] A human-readable representation of the rules object.
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 110 def inspect "#<SleepingKingStudios::Tools::Toolbox::Inflector::Rules:#{object_id}>" end
Private Instance Methods
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 116 def default_irregular_words { 'child' => 'children', 'person' => 'people' } end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 123 def default_plural_rules [ [/([^aeiouy])y$/i, '\1ies'], # Winery => Wineries [/([^aeiouy]o)$/i, '\1es'], # Halo => Haloes [/(ss|[xz]|[cs]h)$/i, '\1es'], # Truss => Trusses [/s$/i, 's'], # Words => Words [/$/, 's'] # Word => Words ] end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 133 def default_singular_rules [ [/([^aeiouy])ies$/i, '\1y'], # Wineries => Winery [/([^aeiouy]o)es$/, '\1'], # Haloes => Halo [/(ss|[sxz]|[cs]h)es$/, '\1'], # Torches => Torch [/ss$/i, 'ss'], # Truss => Truss [/s$/i, ''] # Words => Word ] end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 143 def default_uncountable_words %w[data] end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 147 def reverse_hash(hsh) hsh.each.with_object({}) do |(key, value), reversed| reversed[value] = key end end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 153 def validate_pattern(rxp) raise ArgumentError, "pattern can't be blank", caller(1..-1) if rxp.nil? return if rxp.is_a?(Regexp) raise ArgumentError, 'pattern must be a Regexp', caller(1..-1) end
# File lib/sleeping_king_studios/tools/toolbox/inflector/rules.rb, line 161 def validate_string(word, as: 'word') raise ArgumentError, "#{as} can't be blank", caller(1..-1) if word.nil? unless word.is_a?(String) raise ArgumentError, "#{as} must be a String", caller(1..-1) end return unless word.empty? raise ArgumentError, "#{as} can't be blank", caller(1..-1) end