Class: Text::Gen::Function::Pluralizer
- Inherits:
-
Object
- Object
- Text::Gen::Function::Pluralizer
- Defined in:
- lib/text/gen/function/pluralizer.rb
Constant Summary collapse
- EXCEPTIONS =
{ "foot" => "feet", "axis" => "axes", "child" => "children", "codex" => "codices", "die" => "dice", "dwarf" => "dwarves", "goose" => "geese", "elf" => "elves", "man" => "men", "ox" => "oxen", "thief" => "thieves", "tooth" => "teeth", "wolf" => "wolves", "woman" => "women" }.freeze
- SINGLE =
Set.new(%w[a an the this that my your his her its our their])
Class Method Summary collapse
- .ends_with_y(str) ⇒ Object
- .exceptions(str) ⇒ Object
- .others(str) ⇒ Object
- .pluralize(str) ⇒ Object
- .simple(str) ⇒ Object
- .to_num(str) ⇒ Object
Class Method Details
.ends_with_y(str) ⇒ Object
57 58 59 |
# File 'lib/text/gen/function/pluralizer.rb', line 57 def ends_with_y(str) "#{str[0..-2]}ies" if str.end_with?("y") end |
.exceptions(str) ⇒ Object
47 48 49 |
# File 'lib/text/gen/function/pluralizer.rb', line 47 def exceptions(str) EXCEPTIONS[str] end |
.others(str) ⇒ Object
51 52 53 54 55 |
# File 'lib/text/gen/function/pluralizer.rb', line 51 def others(str) if str.end_with?("s") || str.end_with?("x") || str.end_with?("z") || str.end_with?("ch") || str.end_with?("sh") "#{str}es" end end |
.pluralize(str) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/text/gen/function/pluralizer.rb', line 27 def pluralize(str) return str if str.empty? arr = str.split(/\s+/) return str if arr.length < 2 num = to_num(arr[0]) if num && num > 1 dc = arr[-1].downcase arr[-1] = exceptions(dc) || others(dc) || ends_with_y(dc) || simple(dc) end arr.join(" ") end |
.simple(str) ⇒ Object
61 62 63 |
# File 'lib/text/gen/function/pluralizer.rb', line 61 def simple(str) "#{str}s" end |
.to_num(str) ⇒ Object
41 42 43 44 45 |
# File 'lib/text/gen/function/pluralizer.rb', line 41 def to_num(str) return 1 if SINGLE.include?(str.downcase) str =~ /\d+/ ? str.to_i : nil end |