Module: OrdinalizeFull

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

Overview

Main module

Instance Method Summary collapse

Instance Method Details

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



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

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



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
43
44
45
46
47
# File 'lib/ordinalize_full.rb', line 17

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
    begin
      integer_to_long_form_ordinal
    rescue ArgumentError
      I18n.t("ordinalize_full.n_#{self}", throw: true)
    end
  end
rescue ArgumentError
  raise NotImplementedError, "Unknown locale #{I18n.locale}"
end