Module: Humanize

Included in:
BigDecimal, Float, Integer
Defined in:
lib/humanize.rb,
lib/humanize/locales/az.rb,
lib/humanize/locales/de.rb,
lib/humanize/locales/en.rb,
lib/humanize/locales/fr.rb,
lib/humanize/locales/id.rb,
lib/humanize/locales/ru.rb,
lib/humanize/locales/th.rb,
lib/humanize/locales/tr.rb,
lib/humanize/locales/constants/az.rb,
lib/humanize/locales/constants/de.rb,
lib/humanize/locales/constants/en.rb,
lib/humanize/locales/constants/fr.rb,
lib/humanize/locales/constants/id.rb,
lib/humanize/locales/constants/ru.rb,
lib/humanize/locales/constants/th.rb,
lib/humanize/locales/constants/tr.rb

Defined Under Namespace

Classes: Az, Configuration, De, En, Fr, Id, Ru, Th, Tr

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject



100
101
102
# File 'lib/humanize.rb', line 100

def self.config
  @config ||= Configuration.new
end

Class Method Details

.configure {|config| ... } ⇒ Object

Yields:



108
109
110
# File 'lib/humanize.rb', line 108

def self.configure
  yield(config)
end

.decimals_as_number(decimal, locale:) ⇒ Object



92
93
94
# File 'lib/humanize.rb', line 92

def self.decimals_as_number(decimal, locale:)
  decimal.significant_digits.join('').to_i.humanize(locale: locale)
end

.for_locale(locale) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/humanize.rb', line 28

def self.for_locale(locale)
  case locale.to_sym
  # NOTE: add locales here in ealphabetical order
  when :az
    Humanize::Az
  when :de
    Humanize::De
  when :en
    Humanize::En
  when :fr
    Humanize::Fr
  when :id
    Humanize::Id
  when :ru
    Humanize::Ru
  when :th
    Humanize::Th
  when :tr
    Humanize::Tr
  else
    raise "Unsupported humanize locale: #{locale}"
  end
end

.reset_configObject



104
105
106
# File 'lib/humanize.rb', line 104

def self.reset_config
  @config = Configuration.new
end

.stringify(parts, sign) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/humanize.rb', line 52

def self.stringify(parts, sign)
  output = parts.reverse.join(' ').squeeze(' ')
  if sign
    "#{sign} #{output}"
  else
    output
  end
end

Instance Method Details

#humanize(locale: Humanize.config.default_locale, decimals_as: Humanize.config.decimals_as) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/humanize.rb', line 7

def humanize(locale: Humanize.config.default_locale,
             decimals_as: Humanize.config.decimals_as)
  locale_class = Humanize.for_locale(locale)

  return locale_class::SUB_ONE_GROUPING[0] if zero?

  infinity = to_f.infinite?
  if infinity
    infinity_word = locale_class::INFINITY
    return infinity == 1 ? infinity_word : "#{locale_class::NEGATIVE} #{infinity_word}"
  elsif is_a?(Float) && nan?
    return locale_class::UNDEFINED
  end

  sign = locale_class::NEGATIVE if negative?

  parts = locale_class.new.humanize(abs)
  process_decimals(locale_class, locale, parts, decimals_as)
  Humanize.stringify(parts, sign)
end

#process_decimals(locale_class, locale, parts, decimals_as) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/humanize.rb', line 61

def process_decimals(locale_class, locale, parts, decimals_as)
  return unless is_a?(Float) || is_a?(BigDecimal)

  # Why 15?
  # (byebug) BigDecimal.new(number, 15)
  # 0.8000015e1
  # (byebug) BigDecimal.new(number, 16)
  # 0.8000014999999999e1
  decimal = BigDecimal(self, 15) - BigDecimal(to_i)

  _sign, significant_digits, _base, exponent = decimal.split
  return if significant_digits == "0"

  grouping = locale_class::SUB_ONE_GROUPING
  leading_zeroes = [grouping[0]] * exponent.abs
  decimals_as = :digits if leading_zeroes.any?

  decimals_as_words = case decimals_as
                      when :digits
                        digits = significant_digits.chars.map do |num|
                          grouping[num.to_i]
                        end

                        (leading_zeroes + digits).join(' ')
                      when :number
                        significant_digits.to_i.humanize(locale: locale)
                      end

  parts.insert(0, decimals_as_words, locale_class::POINT)
end