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/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/th.rb,
lib/humanize/locales/constants/tr.rb
Defined Under Namespace
Classes: Az, Configuration, De, En, Fr, Id, Th, Tr
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Class Attribute Details
.config ⇒ Object
96
97
98
|
# File 'lib/humanize.rb', line 96
def self.config
@config ||= Configuration.new
end
|
Class Method Details
104
105
106
|
# File 'lib/humanize.rb', line 104
def self.configure
yield(config)
end
|
.decimals_as_number(decimal, locale:) ⇒ Object
88
89
90
|
# File 'lib/humanize.rb', line 88
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
|
# File 'lib/humanize.rb', line 28
def self.for_locale(locale)
case locale.to_sym
when :az
Humanize::Az
when :de
Humanize::De
when :en
Humanize::En
when :fr
Humanize::Fr
when :id
Humanize::Id
when :th
Humanize::Th
when :tr
Humanize::Tr
else
raise "Unsupported humanize locale: #{locale}"
end
end
|
.reset_config ⇒ Object
100
101
102
|
# File 'lib/humanize.rb', line 100
def self.reset_config
@config = Configuration.new
end
|
.stringify(parts, sign) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/humanize.rb', line 50
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
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
|
# File 'lib/humanize.rb', line 59
def process_decimals(locale_class, locale, parts, decimals_as)
return unless is_a?(Float)
decimal = BigDecimal(self, 15) - BigDecimal(to_i)
_sign, significant_digits, _base, exponent = decimal.split
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
|