Module: OrdinalizeFull

Included in:
Integer
Defined in:
lib/ordinalize_full.rb

Instance Method Summary collapse

Instance Method Details

#ordinalize(in_full: false, gender: :masculine, plurality: :singular) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/ordinalize_full.rb', line 8

def ordinalize(in_full: false, gender: :masculine, plurality: :singular)
  if in_full
    ordinalize_in_full(gender: gender, plurality: plurality)
  else
    ordinalize_in_short(gender: gender, plurality: plurality)
  end
end

#ordinalize_in_full(gender: :masculine, plurality: :singular) ⇒ Object Also known as: ordinalize_full



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ordinalize_full.rb', line 16

def ordinalize_in_full(gender: :masculine, plurality: :singular)
  case I18n.locale
  when :fr
    value = I18n.t("ordinalize_full.n_#{self}_#{gender}", throw: false, default: "")
    value = I18n.t("ordinalize_full.n_#{self}", throw: true) if value.empty?
    value
  when :es
    value = I18n.t("ordinalize_full.n_#{self}", throw: false, default: "")

    if value.empty?
      value = [
        I18n.t("ordinalize_full.n_#{(self / 10) * 10}", throw: true),
        I18n.t("ordinalize_full.n_#{self % 10}", throw: true)
      ].join(" ")
    end

    value = value.split.map { |part| part.chop << "a" }.join(" ") if gender == :feminine
    value << "s" if plurality == :plural
    value = value.chop if value.end_with?("ero")

    value
  else
    I18n.t("ordinalize_full.n_#{self}", throw: true)
  end
rescue ArgumentError
  raise NotImplementedError, "Unknown locale #{I18n.locale}"
end