Module: Humanize
- Included in:
- BigDecimal, Float, Integer
- Defined in:
- lib/humanize/module.rb,
lib/humanize/locales.rb,
lib/humanize/core_ext.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/jp.rb,
lib/humanize/locales/ms.rb,
lib/humanize/locales/pt.rb,
lib/humanize/locales/ru.rb,
lib/humanize/locales/th.rb,
lib/humanize/locales/tr.rb,
lib/humanize/locales/vi.rb,
lib/humanize/locales/fr_ch.rb,
lib/humanize/locales/zh_tw.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/jp.rb,
lib/humanize/locales/constants/ms.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,
lib/humanize/locales/constants/vi.rb,
lib/humanize/locales/constants/fr_ch.rb,
lib/humanize/locales/constants/zh_tw.rb
Defined Under Namespace
Classes: Az, Configuration, De, En, Es, Fr, FrCh, Id, Jp, Ms, Pt, Ru, Th, Tr, Vi, ZhTw
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.config ⇒ Object
101
102
103
|
# File 'lib/humanize/module.rb', line 101
def config
@config ||= Configuration.new
end
|
Class Method Details
109
110
111
|
# File 'lib/humanize/module.rb', line 109
def configure
yield(config)
end
|
.for_locale(locale) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/humanize/module.rb', line 30
def for_locale(locale)
case locale.to_sym
when :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
[Object.const_get("Humanize::#{locale.capitalize}"), ' ']
when :th
[Humanize::Th, '']
when :tr
[Humanize::Tr, ' ']
when :jp
[Humanize::Jp, '']
when :'zh-tw'
[Humanize::ZhTw, '']
when :'fr-CH'
[Humanize::FrCh, ' ']
else
raise "Unsupported humanize locale: #{locale}"
end
end
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/humanize/module.rb', line 8
def format(number,
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 number.zero?
infinity = number.to_f.infinite?
if infinity
infinity_word = locale_class::INFINITY
return infinity == 1 ? infinity_word : "#{locale_class::NEGATIVE}#{spacer}#{infinity_word}"
elsif number.is_a?(Float) && number.nan?
return locale_class::UNDEFINED
end
sign = locale_class::NEGATIVE if number.negative?
parts = locale_class.new.humanize(number.abs)
process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
Humanize.stringify(parts, sign, spacer)
end
|
.locale_is?(locale) ⇒ Boolean
61
62
63
|
# File 'lib/humanize/module.rb', line 61
def locale_is?(locale)
Humanize.config.default_locale == locale
end
|
.process_decimals(number, locale_class, locale, parts, decimals_as, spacer) ⇒ Object
rubocop:disable Metrics/ParameterLists
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
91
92
93
94
95
96
97
|
# File 'lib/humanize/module.rb', line 66
def process_decimals(number, locale_class, locale, parts, decimals_as, spacer)
return unless number.is_a?(Float) || number.is_a?(BigDecimal)
decimal = BigDecimal(number, 15) - BigDecimal(number.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
Humanize.format(significant_digits.to_i, locale:)
end
parts.insert(0, decimals_as_words, locale_class::POINT)
end
|
.reset_config ⇒ Object
105
106
107
|
# File 'lib/humanize/module.rb', line 105
def reset_config
@config = Configuration.new
end
|
.stringify(parts, sign, spacer) ⇒ Object
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/humanize/module.rb', line 50
def stringify(parts, sign, spacer)
output = parts.reverse.join(spacer).squeeze(spacer)
if locale_is?(:es) && sign
"#{output}#{spacer}#{sign}"
elsif sign
"#{sign}#{spacer}#{output}"
else
output
end
end
|
Instance Method Details
#humanize(locale: Humanize.config.default_locale, decimals_as: Humanize.config.decimals_as) ⇒ Object