minting 1.9.1 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +18 -18
- data/lib/minting/mint/i18n.rb +1 -1
- data/lib/minting/money/conversion.rb +1 -1
- data/lib/minting/money/format/to_s.rb +22 -20
- data/lib/minting/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 23c80bac64759b49a2da0d18633a2c960d3417e3b4b997a9c018d9ec1dcd9b16
|
|
4
|
+
data.tar.gz: af77fde4b65155855694b0ab33ecec2b812d063780fe61ed8040c0a505599d96
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 805ca02f7017378de64228a61d1679b86d34368254812d32e0a791c74fce5a91f01ea8fbddc367b63cd816ab469c54a8a59e5a85d036f33a0a9795a0bf09d20f
|
|
7
|
+
data.tar.gz: d5d9c81600d03d8ff15e4cfc0d87b4c5b93d97ebc5b3e1e476a437fac58446e87ed203311bdf93f9354be84afae415cb91d21a60ad2da97e19654096dfbd06dd
|
data/README.md
CHANGED
|
@@ -83,43 +83,43 @@ price = Mint.money(9.99, 'USD')
|
|
|
83
83
|
loss = Mint.money(-1234.56, 'USD')
|
|
84
84
|
|
|
85
85
|
# Built-in named presets
|
|
86
|
-
loss.
|
|
87
|
-
Mint.money(1234.56, 'EUR').
|
|
88
|
-
price.
|
|
89
|
-
price.
|
|
86
|
+
loss.to_formatted_s(:accounting) #=> "($1,234.56)"
|
|
87
|
+
Mint.money(1234.56, 'EUR').to_formatted_s(:european) #=> "1.234,56 €"
|
|
88
|
+
price.to_formatted_s(:amount) #=> "9.99"
|
|
89
|
+
price.to_formatted_s(:currency) #=> "USD 9.99"
|
|
90
90
|
|
|
91
91
|
# Presets can be overridden with explicit kwargs
|
|
92
|
-
Mint.money(1234.56, 'EUR').
|
|
92
|
+
Mint.money(1234.56, 'EUR').to_formatted_s(:european, format: '%<amount>f %<currency>s')
|
|
93
93
|
#=> "1.234,56 EUR"
|
|
94
94
|
|
|
95
95
|
# Or use direct format strings
|
|
96
|
-
price.
|
|
97
|
-
price.
|
|
98
|
-
price.
|
|
99
|
-
price.
|
|
100
|
-
(-price).
|
|
96
|
+
price.to_formatted_s #=> "$9.99",
|
|
97
|
+
price.to_formatted_s(format: '%<amount>d') #=> "9",
|
|
98
|
+
price.to_formatted_s(format: '%<symbol>s%<amount>f') #=> "$9.99",
|
|
99
|
+
price.to_formatted_s(format: '%<symbol>s%<amount>+f') #=> "$+9.99",
|
|
100
|
+
(-price).to_formatted_s(format: '%<amount>f') #=> "-9.99",
|
|
101
101
|
|
|
102
102
|
# Format with padding
|
|
103
103
|
price_in_euros = Mint.money(12.34, 'EUR')
|
|
104
104
|
|
|
105
|
-
price.
|
|
106
|
-
price.
|
|
107
|
-
(-price).
|
|
105
|
+
price.to_formatted_s(format: '--%<amount>7d') #=> "-- 9"
|
|
106
|
+
price.to_formatted_s(format: ' %<amount>10f %<currency>s') #=> " 9.99 USD"
|
|
107
|
+
(-price).to_formatted_s(format: ' %<amount>10f') #=> " -9.99"
|
|
108
108
|
|
|
109
|
-
price_in_euros.
|
|
109
|
+
price_in_euros.to_formatted_s(format: '%<symbol>2s%<amount>+10f') #=> " € +12.34"
|
|
110
110
|
|
|
111
111
|
# Integral & fractional parts
|
|
112
|
-
price.
|
|
112
|
+
price.to_formatted_s(format: '%<integral>d %<fractional>d/100') #=> "9 99/100"
|
|
113
113
|
Mint.money(0.99, 'USD').to_s(format: '%<integral>d dollars and %<fractional>02d cents')
|
|
114
114
|
#=> "0 dollars and 99 cents"
|
|
115
115
|
|
|
116
116
|
# Per-sign Hash format (e.g. accounting parentheses for losses)
|
|
117
117
|
loss = Mint.money(-1234.56, 'USD')
|
|
118
|
-
loss.
|
|
119
|
-
Mint.money(0, 'BRL').
|
|
118
|
+
loss.to_formatted_s(format: { negative: '(%<symbol>s%<amount>f)' }) #=> "($1,234.56)"
|
|
119
|
+
Mint.money(0, 'BRL').to_formatted_s(format: { zero: '--' }) #=> "--"
|
|
120
120
|
# All three keys at once:
|
|
121
121
|
fmt = { positive: '%<symbol>s%<amount>f', negative: '(%<symbol>s%<amount>f)', zero: '--' }
|
|
122
|
-
Mint.money(1234.56, 'USD').
|
|
122
|
+
Mint.money(1234.56, 'USD').to_formatted_s(format: fmt) #=> "$1,234.56"
|
|
123
123
|
|
|
124
124
|
# Json serialization
|
|
125
125
|
|
data/lib/minting/mint/i18n.rb
CHANGED
|
@@ -10,7 +10,7 @@ module Mint
|
|
|
10
10
|
# [+:thousand+] Thousands delimiter (e.g. +"."+)
|
|
11
11
|
# [+:format+] Format template string (e.g. +"%<amount>f %<symbol>s"+)
|
|
12
12
|
#
|
|
13
|
-
# When set, +#
|
|
13
|
+
# When set, +#to_formatted_s+ and +#format+ use these values as fallbacks when the
|
|
14
14
|
# corresponding parameter is not explicitly provided.
|
|
15
15
|
#
|
|
16
16
|
# @example Rails I18n integration (in minting-rails railtie)
|
|
@@ -26,7 +26,7 @@ module Mint
|
|
|
26
26
|
# @return [String] HTML5 `<data>` representation
|
|
27
27
|
def to_html(format = DEFAULT_FORMAT)
|
|
28
28
|
title = Kernel.format("#{currency_code} %0.#{currency.subunit}f", amount)
|
|
29
|
-
body =
|
|
29
|
+
body = to_formatted_s(format: format)
|
|
30
30
|
%(<data class='money' title='#{title}'>#{ERB::Util.html_escape(body)}</data>)
|
|
31
31
|
end
|
|
32
32
|
|
|
@@ -22,7 +22,7 @@ module Mint
|
|
|
22
22
|
# holding a format string. A Hash is convenient for sign-aware formats
|
|
23
23
|
# such as accounting parentheses:
|
|
24
24
|
#
|
|
25
|
-
# money.
|
|
25
|
+
# money.to_formatted_s(format: { negative: '(%<symbol>s%<amount>f)' })
|
|
26
26
|
#
|
|
27
27
|
# Missing keys fall back to the module default, so a Hash with only
|
|
28
28
|
# :negative will still format positives sensibly. The valid keys are
|
|
@@ -41,42 +41,42 @@ module Mint
|
|
|
41
41
|
#
|
|
42
42
|
# @example Basic formatting
|
|
43
43
|
# money = Mint.money(1234.56, 'USD')
|
|
44
|
-
# money.
|
|
45
|
-
# money.
|
|
46
|
-
# money.
|
|
44
|
+
# money.to_formatted_s #=> "$1,234.56"
|
|
45
|
+
# money.to_formatted_s(thousand: '.', decimal: ',') #=> "$1.234,56"
|
|
46
|
+
# money.to_formatted_s(decimal: ',', thousand: '') #=> "$1234,56"
|
|
47
47
|
#
|
|
48
48
|
# @example Preset formats
|
|
49
49
|
# loss = Mint.money(-1234.56, 'USD')
|
|
50
|
-
# loss.
|
|
51
|
-
# money.
|
|
52
|
-
# money.
|
|
53
|
-
# money.
|
|
50
|
+
# loss.to_formatted_s(:accounting) #=> "($1,234.56)"
|
|
51
|
+
# money.to_formatted_s(:european) #=> "1.234,56 €"
|
|
52
|
+
# money.to_formatted_s(:amount) #=> "1234.56"
|
|
53
|
+
# money.to_formatted_s(:currency) #=> "USD 1234.56"
|
|
54
54
|
#
|
|
55
55
|
# @example Custom formats
|
|
56
|
-
# money.
|
|
57
|
-
# money.
|
|
58
|
-
# money.
|
|
59
|
-
# money.
|
|
56
|
+
# money.to_formatted_s(format: '%<amount>f') #=> "1234.56"
|
|
57
|
+
# money.to_formatted_s(format: '%<currency>s %<amount>f') #=> "USD 1234.56"
|
|
58
|
+
# money.to_formatted_s(format: '%<amount>f %<symbol>s') #=> "1234.56 $"
|
|
59
|
+
# money.to_formatted_s(format: '%<symbol>s%<amount>+f') #=> "$+1234.56"
|
|
60
60
|
#
|
|
61
61
|
# @example Integral & fractional parts
|
|
62
|
-
# money.
|
|
62
|
+
# money.to_formatted_s(format: '%<integral>d.%<fractional>02d') #=> "1234.56"
|
|
63
63
|
# price = Mint.money(0.99, 'USD')
|
|
64
|
-
# price.
|
|
64
|
+
# price.to_formatted_s(format: '%<integral>d dollars and %<fractional>02d cents')
|
|
65
65
|
# #=> "0 dollars and 99 cents"
|
|
66
66
|
#
|
|
67
67
|
# @example Per-sign Hash format (accounting parentheses)
|
|
68
68
|
# loss = Mint.money(-1234.56, 'USD')
|
|
69
|
-
# loss.
|
|
70
|
-
# Mint.money(0, 'BRL').
|
|
69
|
+
# loss.to_formatted_s(format: { negative: '(%<symbol>s%<amount>f)' }) #=> "($1,234.56)"
|
|
70
|
+
# Mint.money(0, 'BRL').to_formatted_s(format: { zero: '--' }) #=> "--"
|
|
71
71
|
#
|
|
72
72
|
# @example Padding and alignment
|
|
73
|
-
# money.
|
|
74
|
-
# money.
|
|
73
|
+
# money.to_formatted_s(format: '%<amount>10.2f') #=> " 1234.56"
|
|
74
|
+
# money.to_formatted_s(format: '%<symbol>s%<amount>010.2f') #=> "$0001234.56"
|
|
75
75
|
#
|
|
76
76
|
# @example Locale-aware formatting (with Mint.locale_backend set)
|
|
77
|
-
# money.
|
|
77
|
+
# money.to_formatted_s # decimal and thousand come from locale_backend
|
|
78
78
|
#
|
|
79
|
-
def
|
|
79
|
+
def to_formatted_s(preset = nil, format: nil, decimal: nil, thousand: nil, width: nil)
|
|
80
80
|
if preset
|
|
81
81
|
config = PRESETS.fetch(preset) { raise ArgumentError, "Unknown format preset: #{preset.inspect}" }
|
|
82
82
|
format ||= config[:format]
|
|
@@ -100,5 +100,7 @@ module Mint
|
|
|
100
100
|
|
|
101
101
|
width ? formatted.rjust(width) : formatted
|
|
102
102
|
end
|
|
103
|
+
|
|
104
|
+
def to_s = to_formatted_s
|
|
103
105
|
end
|
|
104
106
|
end
|
data/lib/minting/version.rb
CHANGED