money 6.13.1 → 6.13.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/.travis.yml +1 -1
- data/CHANGELOG.md +8 -0
- data/Gemfile +1 -0
- data/README.md +29 -0
- data/config/currency_iso.json +2 -2
- data/lib/money/locale_backend/i18n.rb +2 -1
- data/lib/money/money.rb +4 -1
- data/lib/money/money/allocation.rb +5 -2
- data/lib/money/money/formatting_rules.rb +6 -4
- data/lib/money/version.rb +1 -1
- data/money.gemspec +1 -1
- data/spec/locale_backend/i18n_spec.rb +9 -1
- data/spec/money/allocation_spec.rb +5 -0
- data/spec/money_spec.rb +14 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7913c2be69c77f43c10cb0a6262dadeccbbc9a9f
|
4
|
+
data.tar.gz: bb7de5d50d2a8e7185ebb4a87e5a60a637602e62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f92d1aef2013a80abb618b49ce72d07406a93678b0ce183ce2c6051c397ccc24dd121d21eda707fab2dae042b369e83cf1deb6e45db15770b1a417b989014fe6
|
7
|
+
data.tar.gz: eeb2491fca41a8a3c858bad2314c4c8d3a29384f7f0da9e836b871071110f321c76ea697222f6b47903839f344bfbdcf85e0636b59a80dca1381bfa29aaa1b59
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 6.13.2
|
4
|
+
- Prevent Money initialization with non-finite amounts
|
5
|
+
- Convert the fractional value of a Money object to BigDecimal when initializing
|
6
|
+
- Offer replacements for currency position deprecations
|
7
|
+
- Fix Peruvian Sol symbol
|
8
|
+
- Lock i18n to <= 1.2.0 for older (< 2.3) rubies
|
9
|
+
- Prevent Divide By Zero in `Money#allocate`
|
10
|
+
|
3
11
|
## 6.13.1
|
4
12
|
- Add bolívar soberano (VES)
|
5
13
|
- Deprecate bolívar fuerte (VEF)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -392,6 +392,35 @@ m = Money.new('123', :gbp) # => #<Money fractional:123 currency:GBP>
|
|
392
392
|
m.format(symbol: m.currency.to_s + ' ') # => "GBP 1.23"
|
393
393
|
```
|
394
394
|
|
395
|
+
## Rounding
|
396
|
+
|
397
|
+
By default, `Money` objects are rounded to the nearest cent and the additional precision is not preserved:
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
Money.from_amount(2.34567).format #=> "$2.35"
|
401
|
+
```
|
402
|
+
|
403
|
+
To retain the additional precision, you will also need to set `infinite_precision` to `true`.
|
404
|
+
|
405
|
+
```ruby
|
406
|
+
Money.infinite_precision = true
|
407
|
+
Money.from_amount(2.34567).format #=> "$2.34567"
|
408
|
+
```
|
409
|
+
|
410
|
+
To round to the nearest cent (or anything more precise), you can use the `round` method. However, note that the `round` method on a `Money` object does not work the same way as a normal Ruby `Float` object. Money's `round` method accepts different arguments. The first argument to the round method is the rounding mode, while the second argument is the level of precision relative to the cent.
|
411
|
+
|
412
|
+
```
|
413
|
+
# Float
|
414
|
+
2.34567.round #=> 2
|
415
|
+
2.34567.round(2) #=> 2.35
|
416
|
+
|
417
|
+
# Money
|
418
|
+
Money.infinite_precision = true
|
419
|
+
Money.new(2.34567).format #=> "$0.0234567"
|
420
|
+
Money.new(2.34567).round.format #=> "$0.02"
|
421
|
+
Money.new(2.34567).round(BigDecimal::ROUND_HALF_UP, 2).format #=> "$0.0235"
|
422
|
+
```
|
423
|
+
|
395
424
|
## Ruby on Rails
|
396
425
|
|
397
426
|
To integrate money in a Rails application use [money-rails](https://github.com/RubyMoney/money-rails).
|
data/config/currency_iso.json
CHANGED
@@ -1647,12 +1647,12 @@
|
|
1647
1647
|
"priority": 100,
|
1648
1648
|
"iso_code": "PEN",
|
1649
1649
|
"name": "Peruvian Sol",
|
1650
|
-
"symbol": "S
|
1650
|
+
"symbol": "S/",
|
1651
1651
|
"alternate_symbols": [],
|
1652
1652
|
"subunit": "Céntimo",
|
1653
1653
|
"subunit_to_unit": 100,
|
1654
1654
|
"symbol_first": true,
|
1655
|
-
"html_entity": "S
|
1655
|
+
"html_entity": "S/",
|
1656
1656
|
"decimal_mark": ".",
|
1657
1657
|
"thousands_separator": ",",
|
1658
1658
|
"iso_numeric": "604",
|
data/lib/money/money.rb
CHANGED
@@ -276,10 +276,13 @@ class Money
|
|
276
276
|
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
|
277
277
|
#
|
278
278
|
def initialize(obj, currency = Money.default_currency, bank = Money.default_bank)
|
279
|
-
@fractional = obj.respond_to?(:fractional) ? obj.fractional :
|
279
|
+
@fractional = as_d(obj.respond_to?(:fractional) ? obj.fractional : obj)
|
280
280
|
@currency = obj.respond_to?(:currency) ? obj.currency : Currency.wrap(currency)
|
281
281
|
@currency ||= Money.default_currency
|
282
282
|
@bank = obj.respond_to?(:bank) ? obj.bank : bank
|
283
|
+
|
284
|
+
# BigDecimal can be Infinity and NaN, money of that amount does not make sense
|
285
|
+
raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
|
283
286
|
end
|
284
287
|
|
285
288
|
# Assuming using a currency using dollars:
|
@@ -24,8 +24,11 @@ class Money
|
|
24
24
|
parts_sum = parts.inject(0, :+)
|
25
25
|
part = parts.pop
|
26
26
|
|
27
|
-
current_split =
|
28
|
-
|
27
|
+
current_split = 0
|
28
|
+
if parts_sum > 0
|
29
|
+
current_split = remaining_amount * part / parts_sum
|
30
|
+
current_split = current_split.truncate if whole_amounts
|
31
|
+
end
|
29
32
|
|
30
33
|
result.unshift current_split
|
31
34
|
remaining_amount -= current_split
|
@@ -103,15 +103,18 @@ class Money
|
|
103
103
|
|
104
104
|
def warn_about_deprecated_rules(rules)
|
105
105
|
if rules.has_key?(:symbol_position)
|
106
|
-
|
106
|
+
position = rules[:symbol_position]
|
107
|
+
template = position == :before ? '%u %n' : '%n %u'
|
108
|
+
|
109
|
+
warn "[DEPRECATION] `symbol_position: :#{position}` is deprecated - you can replace it with `format: #{template}`"
|
107
110
|
end
|
108
111
|
|
109
112
|
if rules.has_key?(:symbol_before_without_space)
|
110
|
-
warn
|
113
|
+
warn "[DEPRECATION] `symbol_before_without_space:` option is deprecated - you can replace it with `format: '%u%n'`"
|
111
114
|
end
|
112
115
|
|
113
116
|
if rules.has_key?(:symbol_after_without_space)
|
114
|
-
warn
|
117
|
+
warn "[DEPRECATION] `symbol_after_without_space:` option is deprecated - you can replace it with `format: '%n%u'`"
|
115
118
|
end
|
116
119
|
|
117
120
|
if rules.has_key?(:html)
|
@@ -121,7 +124,6 @@ class Money
|
|
121
124
|
if rules.has_key?(:html_wrap_symbol)
|
122
125
|
warn "[DEPRECATION] `html_wrap_symbol` is deprecated - use `html_wrap` instead. Please note that `html_wrap` will wrap all parts of currency."
|
123
126
|
end
|
124
|
-
|
125
127
|
end
|
126
128
|
end
|
127
129
|
end
|
data/lib/money/version.rb
CHANGED
data/money.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_dependency 'i18n', [">= 0.6.4", '<= 2']
|
18
18
|
|
19
|
-
s.add_development_dependency "bundler"
|
19
|
+
s.add_development_dependency "bundler"
|
20
20
|
s.add_development_dependency "rake"
|
21
21
|
s.add_development_dependency "rspec", "~> 3.4"
|
22
22
|
s.add_development_dependency "yard", "~> 0.9.11"
|
@@ -21,7 +21,7 @@ describe Money::LocaleBackend::I18n do
|
|
21
21
|
before do
|
22
22
|
I18n.locale = :de
|
23
23
|
I18n.backend.store_translations(:de, number: {
|
24
|
-
currency: { format: { delimiter: '.', separator: ',' } }
|
24
|
+
currency: { format: { delimiter: '.', separator: ',', unit: '$' } }
|
25
25
|
})
|
26
26
|
end
|
27
27
|
|
@@ -32,6 +32,10 @@ describe Money::LocaleBackend::I18n do
|
|
32
32
|
it 'returns decimal_mark based on the current locale' do
|
33
33
|
expect(subject.lookup(:decimal_mark, nil)).to eq(',')
|
34
34
|
end
|
35
|
+
|
36
|
+
it 'returns symbol based on the current locale' do
|
37
|
+
expect(subject.lookup(:symbol, nil)).to eq('$')
|
38
|
+
end
|
35
39
|
end
|
36
40
|
|
37
41
|
context 'with number.format defined' do
|
@@ -57,6 +61,10 @@ describe Money::LocaleBackend::I18n do
|
|
57
61
|
it 'returns decimal_mark based on the current locale' do
|
58
62
|
expect(subject.lookup(:decimal_mark, nil)).to eq(nil)
|
59
63
|
end
|
64
|
+
|
65
|
+
it 'returns symbol based on the current locale' do
|
66
|
+
expect(subject.lookup(:symbol, nil)).to eq(nil)
|
67
|
+
end
|
60
68
|
end
|
61
69
|
end
|
62
70
|
end
|
@@ -87,6 +87,11 @@ describe Money::Allocation do
|
|
87
87
|
expect(described_class.generate(10, [1, 1, 2])).to eq([3, 2, 5])
|
88
88
|
expect(described_class.generate(100, [1, 1, 1])).to eq([34, 33, 33])
|
89
89
|
end
|
90
|
+
|
91
|
+
it 'handles zero arguments' do
|
92
|
+
expect(described_class.generate(100, [1, 1, 0])).to eq([50, 50, 0])
|
93
|
+
expect(described_class.generate(100, [0, 1, 1])).to eq([0, 50, 50])
|
94
|
+
end
|
90
95
|
end
|
91
96
|
|
92
97
|
context 'fractional amounts' do
|
data/spec/money_spec.rb
CHANGED
@@ -92,6 +92,20 @@ describe Money do
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
context 'non-finite value is given' do
|
96
|
+
let(:error) { 'must be initialized with a finite value' }
|
97
|
+
|
98
|
+
it 'raises an error when trying to initialize with Infinity' do
|
99
|
+
expect { Money.new('Infinity') }.to raise_error(ArgumentError, error)
|
100
|
+
expect { Money.new(BigDecimal('Infinity')) }.to raise_error(ArgumentError, error)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'raises an error when trying to initialize with NaN' do
|
104
|
+
expect { Money.new('NaN') }.to raise_error(ArgumentError, error)
|
105
|
+
expect { Money.new(BigDecimal('NaN')) }.to raise_error(ArgumentError, error)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
95
109
|
context "with infinite_precision", :infinite_precision do
|
96
110
|
context 'given the initializing value is 1.50' do
|
97
111
|
let(:initializing_value) { 1.50 }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.13.
|
4
|
+
version: 6.13.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Emmons
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -34,16 +34,16 @@ dependencies:
|
|
34
34
|
name: bundler
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '
|
39
|
+
version: '0'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '
|
46
|
+
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|