Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/spanish_pluralizer.rb

Constant Summary collapse

SPANISH_PLURALIZER_ACUTES =
{
  'a' => 'á',
  'e' => 'é',
  'i' => 'í',
  'o' => 'ó',
  'u' => 'ú'
}

Instance Method Summary collapse

Instance Method Details

#pluralize_spanishObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/spanish_pluralizer.rb', line 31

def pluralize_spanish
  #orden por probabilidad de ocurrencia para optimizar
  case self
  #caja => cajas
  #puente => puentes
  #papi => papis
  #mono => monos
  #río => ríos
  #palabru => palabrus
  #sofá => sofás
  #palabré => palabrés
  #palabró => palabrós
  when /.*[aeiouáéó]$/ then self + 's'
  when /(?<pre>.*)(?<last_vowel>.)(?<ending>[ns])$/
    if $~[:last_vowel].spanish_pluralizer_is_acute?
      new_str = self
      #aguda con tilde => grave sin tilde, a menos que el tilde se mantenga por existir hiato
      new_str[-2] = new_str[-2].spanish_pluralizer_unacute unless new_str[-3..-1].match(/(a|e|o)(í|ú)/)
      new_str + 'es'
    elsif $~[:ending] == 's'
      #análisis => análisis
      self
    else
      #imagen => imágenes
      #palebren => palébrenes
      #origen => orígenes
      #orden => órdenes
      #resumen => resúmenes
      ending = $~[:last_vowel] + 'nes'
      $~[:pre].sub(/(a|e|i|o|u)(?=[^aeiou]*$)/, &:spanish_pluralizer_acute) + ending
    end
  #arroz => arroces
  when /.*z$/ then self[0..-2] + 'ces'
  #delantal => delantales
  #mantel => manteles
  #mandril => mandriles
  #tambor => tambores
  #gandul => gandules
  #maní => maníes
  #menú => menúes
  #rey => reyes
  #fax => faxes
  #árbol => árboles
  #laúd => laúdes
  else
   self + 'es'
  end
end

#spanish_pluralizer_acuteObject



19
20
21
# File 'lib/spanish_pluralizer.rb', line 19

def spanish_pluralizer_acute
  SPANISH_PLURALIZER_ACUTES.fetch(self, self)
end

#spanish_pluralizer_is_acute?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/spanish_pluralizer.rb', line 27

def spanish_pluralizer_is_acute?
  SPANISH_PLURALIZER_ACUTES.values.include? self
end

#spanish_pluralizer_unacuteObject



23
24
25
# File 'lib/spanish_pluralizer.rb', line 23

def spanish_pluralizer_unacute
  SPANISH_PLURALIZER_ACUTES.invert.fetch(self, self)
end