monetize 1.12.0 → 2.0.0
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/CHANGELOG.md +14 -0
- data/LICENSE +1 -1
- data/README.md +19 -19
- data/lib/monetize/collection.rb +0 -2
- data/lib/monetize/core_extensions/hash.rb +0 -2
- data/lib/monetize/core_extensions/numeric.rb +0 -2
- data/lib/monetize/core_extensions/string.rb +0 -2
- data/lib/monetize/core_extensions/symbol.rb +0 -2
- data/lib/monetize/core_extensions.rb +0 -2
- data/lib/monetize/parser.rb +38 -20
- data/lib/monetize/version.rb +1 -3
- data/lib/monetize.rb +6 -9
- data/monetize.gemspec +23 -25
- metadata +8 -64
- data/.github/workflows/ruby.yml +0 -34
- data/.gitignore +0 -18
- data/.rubocop.yml +0 -32
- data/CONTRIBUTING.md +0 -20
- data/Gemfile +0 -13
- data/Rakefile +0 -18
- data/spec/core_extensions_spec.rb +0 -261
- data/spec/monetize_spec.rb +0 -587
- data/spec/spec_helper.rb +0 -7
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
|
|
3
|
-
require 'spec_helper'
|
|
4
|
-
require 'monetize'
|
|
5
|
-
require 'monetize/core_extensions'
|
|
6
|
-
|
|
7
|
-
describe Monetize, 'core extensions' do
|
|
8
|
-
describe NilClass do
|
|
9
|
-
describe '#to_money' do
|
|
10
|
-
it 'works as documented' do
|
|
11
|
-
money = nil.to_money
|
|
12
|
-
expect(money.cents).to eq 0
|
|
13
|
-
expect(money.currency).to eq Money.default_currency
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'accepts optional currency' do
|
|
17
|
-
expect(nil.to_money('USD')).to eq Money.new(nil, 'USD')
|
|
18
|
-
expect(nil.to_money('EUR')).to eq Money.new(nil, 'EUR')
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
describe Numeric do
|
|
24
|
-
describe '#to_money' do
|
|
25
|
-
it 'works as documented' do
|
|
26
|
-
money = 1234.to_money
|
|
27
|
-
expect(money.cents).to eq 1_234_00
|
|
28
|
-
expect(money.currency).to eq Money.default_currency
|
|
29
|
-
|
|
30
|
-
money = 100.37.to_money
|
|
31
|
-
expect(money.cents).to eq 100_37
|
|
32
|
-
expect(money.currency).to eq Money.default_currency
|
|
33
|
-
|
|
34
|
-
money = BigDecimal('1234').to_money
|
|
35
|
-
expect(money.cents).to eq 1_234_00
|
|
36
|
-
expect(money.currency).to eq Money.default_currency
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it 'accepts optional currency' do
|
|
40
|
-
expect(1234.to_money('USD')).to eq Money.new(1_234_00, 'USD')
|
|
41
|
-
expect(1234.to_money('EUR')).to eq Money.new(1_234_00, 'EUR')
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
it 'respects :subunit_to_unit currency property' do
|
|
45
|
-
expect(10.to_money('USD')).to eq Money.new(10_00, 'USD')
|
|
46
|
-
expect(10.to_money('TND')).to eq Money.new(10_000, 'TND')
|
|
47
|
-
expect(10.to_money('JPY')).to eq Money.new(10, 'JPY')
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
specify 'GH-15' do
|
|
51
|
-
amount = 555.55.to_money
|
|
52
|
-
expect(amount).to eq Money.new(555_55)
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
describe String do
|
|
58
|
-
describe '#to_money' do
|
|
59
|
-
STRING_TO_MONEY = {
|
|
60
|
-
'20.15' => Money.new(20_15),
|
|
61
|
-
'100' => Money.new(100_00),
|
|
62
|
-
'100.37' => Money.new(100_37),
|
|
63
|
-
'100,37' => Money.new(100_37),
|
|
64
|
-
'100 000' => Money.new(100_000_00),
|
|
65
|
-
'100,000.00' => Money.new(100_000_00),
|
|
66
|
-
'1,000' => Money.new(1_000_00),
|
|
67
|
-
'-1,000' => Money.new(-1_000_00),
|
|
68
|
-
'1,000.5' => Money.new(1_000_50),
|
|
69
|
-
'1,000.51' => Money.new(1_000_51),
|
|
70
|
-
'1,000.505' => Money.new(1_000_50), # ROUND_HALF_EVEN default bankers rounding
|
|
71
|
-
'1,000.515' => Money.new(1_000_52), # ROUND_HALF_EVEN default bankers rounding
|
|
72
|
-
'1,000.504' => Money.new(1_000_50),
|
|
73
|
-
'1,000.0000' => Money.new(1_000_00),
|
|
74
|
-
'1,000.5000' => Money.new(1_000_50),
|
|
75
|
-
'1,000.5099' => Money.new(1_000_51),
|
|
76
|
-
'1.550' => Money.new(1_55),
|
|
77
|
-
'25.' => Money.new(25_00),
|
|
78
|
-
'.75' => Money.new(75),
|
|
79
|
-
|
|
80
|
-
'7.21K' => Money.new(7_210_00),
|
|
81
|
-
'1M' => Money.new(1_000_000_00),
|
|
82
|
-
'1.2M' => Money.new(1_200_000_00),
|
|
83
|
-
'1.37M' => Money.new(1_370_000_00),
|
|
84
|
-
'3.1415927M' => Money.new(3_141_592_70),
|
|
85
|
-
'.42B' => Money.new(420_000_000_00),
|
|
86
|
-
'5T' => Money.new(5_000_000_000_000_00),
|
|
87
|
-
|
|
88
|
-
'100 USD' => Money.new(100_00, 'USD'),
|
|
89
|
-
'-100 USD' => Money.new(-100_00, 'USD'),
|
|
90
|
-
'100 EUR' => Money.new(100_00, 'EUR'),
|
|
91
|
-
'100.37 EUR' => Money.new(100_37, 'EUR'),
|
|
92
|
-
'100,37 EUR' => Money.new(100_37, 'EUR'),
|
|
93
|
-
'100,000.00 USD' => Money.new(100_000_00, 'USD'),
|
|
94
|
-
'100.000,00 EUR' => Money.new(100_000_00, 'EUR'),
|
|
95
|
-
'1,000 USD' => Money.new(1_000_00, 'USD'),
|
|
96
|
-
'-1,000 USD' => Money.new(-1_000_00, 'USD'),
|
|
97
|
-
'1,000.5500 USD' => Money.new(1_000_55, 'USD'),
|
|
98
|
-
'-1,000.6500 USD' => Money.new(-1_000_65, 'USD'),
|
|
99
|
-
'1.550 USD' => Money.new(1_55, 'USD'),
|
|
100
|
-
|
|
101
|
-
'USD 100' => Money.new(100_00, 'USD'),
|
|
102
|
-
'EUR 100' => Money.new(100_00, 'EUR'),
|
|
103
|
-
'EUR 100.37' => Money.new(100_37, 'EUR'),
|
|
104
|
-
'CAD -100.37' => Money.new(-100_37, 'CAD'),
|
|
105
|
-
'EUR 100,37' => Money.new(100_37, 'EUR'),
|
|
106
|
-
'EUR -100,37' => Money.new(-100_37, 'EUR'),
|
|
107
|
-
'USD 100,000.00' => Money.new(100_000_00, 'USD'),
|
|
108
|
-
'EUR 100.000,00' => Money.new(100_000_00, 'EUR'),
|
|
109
|
-
'USD 1,000' => Money.new(1_000_00, 'USD'),
|
|
110
|
-
'USD -1,000' => Money.new(-1_000_00, 'USD'),
|
|
111
|
-
'USD 1,000.9000' => Money.new(1_000_90, 'USD'),
|
|
112
|
-
'USD -1,000.090' => Money.new(-1_000_09, 'USD'),
|
|
113
|
-
'USD 1.5500' => Money.new(1_55, 'USD'),
|
|
114
|
-
|
|
115
|
-
'$100 USD' => Money.new(100_00, 'USD'),
|
|
116
|
-
'$1,194.59 USD' => Money.new(1_194_59, 'USD'),
|
|
117
|
-
'$-1,955 USD' => Money.new(-1_955_00, 'USD'),
|
|
118
|
-
'$1,194.5900 USD' => Money.new(1_194_59, 'USD'),
|
|
119
|
-
'$-1,955.000 USD' => Money.new(-1_955_00, 'USD'),
|
|
120
|
-
'$1.99000 USD' => Money.new(1_99, 'USD')
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
it 'works as documented' do
|
|
124
|
-
STRING_TO_MONEY.each do |string, money|
|
|
125
|
-
expect(string.to_money).to eq money
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
it 'coerces input to string' do
|
|
130
|
-
expect(Monetize.parse(20, 'USD')).to eq Money.new(20_00, 'USD')
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
it 'accepts optional currency' do
|
|
134
|
-
expect('10.10'.to_money('USD')).to eq Money.new(10_10, 'USD')
|
|
135
|
-
expect('10.10'.to_money('EUR')).to eq Money.new(10_10, 'EUR')
|
|
136
|
-
expect('10.10 USD'.to_money('USD')).to eq Money.new(10_10, 'USD')
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
it 'uses parsed currency, even if currency is passed' do
|
|
140
|
-
expect('10.10 USD'.to_money('EUR')).to eq(Money.new(10_10, 'USD'))
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
it 'ignores unrecognized data' do
|
|
144
|
-
expect('hello 2000 world'.to_money).to eq Money.new(2_000_00)
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
it 'respects :subunit_to_unit currency property' do
|
|
148
|
-
expect('1'.to_money('USD')).to eq Money.new(1_00, 'USD')
|
|
149
|
-
expect('1'.to_money('TND')).to eq Money.new(1_000, 'TND')
|
|
150
|
-
expect('1'.to_money('JPY')).to eq Money.new(1, 'JPY')
|
|
151
|
-
expect('1.5'.to_money('KWD').cents).to eq 1_500
|
|
152
|
-
end
|
|
153
|
-
|
|
154
|
-
it 'respects Money.rounding_mode' do
|
|
155
|
-
expect('1.009'.to_money).to eq(Money.new(1_01))
|
|
156
|
-
|
|
157
|
-
Money.rounding_mode(BigDecimal::ROUND_DOWN) do
|
|
158
|
-
expect('1.009'.to_money).to eq(Money.new(1_00))
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
expect('1.001'.to_money).to eq(Money.new(1_00))
|
|
162
|
-
|
|
163
|
-
Money.rounding_mode(BigDecimal::ROUND_UP) do
|
|
164
|
-
expect('1.001'.to_money).to eq(Money.new(1_01))
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
it 'produces results similar to Money.from_amount for all the rounding edge cases' do
|
|
169
|
-
(1_000..1_010).each do |amount|
|
|
170
|
-
amount = amount.to_f / 1000
|
|
171
|
-
|
|
172
|
-
expect(amount.to_s.to_money).to eq(Money.from_amount(amount))
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
describe '#to_currency' do
|
|
178
|
-
it 'converts String to Currency' do
|
|
179
|
-
expect('USD'.to_currency).to eq Money::Currency.new('USD')
|
|
180
|
-
expect('EUR'.to_currency).to eq Money::Currency.new('EUR')
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
it 'raises Money::Currency::UnknownCurrency with unknown Currency' do
|
|
184
|
-
expect { 'XXX'.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
|
185
|
-
expect { ' '.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
describe Hash do
|
|
191
|
-
describe '#to_money' do
|
|
192
|
-
context 'when currency is present in hash' do
|
|
193
|
-
subject(:hash) { {cents: 5, currency: 'EUR'} }
|
|
194
|
-
|
|
195
|
-
it 'converts Hash to Money using key from hash' do
|
|
196
|
-
expect(hash.to_money).to eq(Money.new(5, 'EUR'))
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
context 'when currency is passed as argument' do
|
|
201
|
-
subject(:hash) { {cents: 10} }
|
|
202
|
-
|
|
203
|
-
it 'converts Hash to Money using passed currency argument' do
|
|
204
|
-
expect(hash.to_money('JPY')).to eq(Money.new(10, 'JPY'))
|
|
205
|
-
end
|
|
206
|
-
end
|
|
207
|
-
|
|
208
|
-
context 'when currency argument is a hash' do
|
|
209
|
-
subject(:hash) { {cents: 100, currency: {iso_code: 'EUR'}} }
|
|
210
|
-
|
|
211
|
-
it 'converts Hash to Money using iso_code from currency hash' do
|
|
212
|
-
expect(hash.to_money).to eq(Money.new(100, 'EUR'))
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
context 'when no currency is passed' do
|
|
217
|
-
subject(:hash) { {cents: 123} }
|
|
218
|
-
|
|
219
|
-
before { allow(Money).to receive(:default_currency).and_return('USD') }
|
|
220
|
-
|
|
221
|
-
it 'converts Hash to Money using default value' do
|
|
222
|
-
expect(hash.to_money('USD')).to eq(Money.new(123, 'USD'))
|
|
223
|
-
end
|
|
224
|
-
end
|
|
225
|
-
|
|
226
|
-
context "when key name is 'fractional'" do
|
|
227
|
-
subject(:hash) { {fractional: 100} }
|
|
228
|
-
|
|
229
|
-
it 'converts Hash to Money, interpreting fractional as cents' do
|
|
230
|
-
expect(hash.to_money).to eq(Money.new(100, 'USD'))
|
|
231
|
-
end
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
context 'when Money to_hash is used' do
|
|
235
|
-
subject(:hash) { { cents: 100, currency_iso: 'SGD' } }
|
|
236
|
-
|
|
237
|
-
it 'converts Hash to Money, interpreting fractional as cents' do
|
|
238
|
-
expect(hash.to_money).to eq(Money.new(100, 'SGD'))
|
|
239
|
-
end
|
|
240
|
-
end
|
|
241
|
-
end
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
describe Symbol do
|
|
245
|
-
describe '#to_currency' do
|
|
246
|
-
it 'converts Symbol to Currency' do
|
|
247
|
-
expect(:usd.to_currency).to eq Money::Currency.new('USD')
|
|
248
|
-
expect(:ars.to_currency).to eq Money::Currency.new('ARS')
|
|
249
|
-
end
|
|
250
|
-
|
|
251
|
-
it 'is case-insensitive' do
|
|
252
|
-
expect(:EUR.to_currency).to eq Money::Currency.new('EUR')
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
it 'raises Money::Currency::UnknownCurrency with unknown Currency' do
|
|
256
|
-
expect { :XXX.to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
|
257
|
-
expect { :" ".to_currency }.to raise_error(Money::Currency::UnknownCurrency)
|
|
258
|
-
end
|
|
259
|
-
end
|
|
260
|
-
end
|
|
261
|
-
end
|