Module: Humanize

Included in:
BigDecimal, Float, Integer
Defined in:
lib/humanize.rb,
lib/humanize/locales.rb,
lib/humanize/locales/az.rb,
lib/humanize/locales/de.rb,
lib/humanize/locales/en.rb,
lib/humanize/locales/es.rb,
lib/humanize/locales/fr.rb,
lib/humanize/locales/id.rb,
lib/humanize/locales/pt.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/es.rb,
lib/humanize/locales/constants/fr.rb,
lib/humanize/locales/constants/id.rb,
lib/humanize/locales/constants/pt.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, Es, Fr, Id, Pt, Ru, Th, Tr

Constant Summary collapse

SPACE =
' '.freeze
EMPTY =
''.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject



94
95
96
# File 'lib/humanize.rb', line 94

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

Class Method Details

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

Yields:



102
103
104
# File 'lib/humanize.rb', line 102

def self.configure
  yield(config)
end

.for_locale(locale) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/humanize.rb', line 30

def self.for_locale(locale)
  case locale.to_sym
  # NOTE: add locales here in ealphabetical order
  when :az, :de, :en, :es, :fr, :id, :pt, :ru
    [Object.const_get("Humanize::#{locale.capitalize}"), SPACE]
  when :th
    [Humanize::Th, EMPTY]
  when :tr
    [Humanize::Tr, SPACE]
  else
    raise "Unsupported humanize locale: #{locale}"
  end
end

.locale_is?(locale) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/humanize.rb', line 55

def self.locale_is?(locale)
  Humanize.config.default_locale == locale
end

.reset_configObject



98
99
100
# File 'lib/humanize.rb', line 98

def self.reset_config
  @config = Configuration.new
end

.stringify(parts, sign, spacer) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/humanize.rb', line 44

def self.stringify(parts, sign, spacer)
  output = parts.reverse.join(spacer).squeeze(spacer)
  if locale_is?(:es) && sign
    "#{output} #{sign}"
  elsif sign
    "#{sign} #{output}"
  else
    output
  end
end

Instance Method Details

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



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

def humanize(locale: Humanize.config.default_locale,
             decimals_as: Humanize.config.decimals_as)
  locale_class, spacer = 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}#{spacer}#{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, spacer)
  Humanize.stringify(parts, sign, spacer)
end

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



59
60
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
# File 'lib/humanize.rb', line 59

def process_decimals(locale_class, locale, parts, decimals_as, spacer)
  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(spacer)
                      when :number
                        significant_digits.to_i.humanize(locale: locale)
                      end

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