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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4981b9f6d6c9b7162e5df6692e81cac6440edcca
4
- data.tar.gz: 96d7be00c3f275c0b8731807f4fa8955fade71bb
3
+ metadata.gz: 7913c2be69c77f43c10cb0a6262dadeccbbc9a9f
4
+ data.tar.gz: bb7de5d50d2a8e7185ebb4a87e5a60a637602e62
5
5
  SHA512:
6
- metadata.gz: 4943cebf00d5abf48ca73b52ffd18cc2324287ffada6513357a1d4146b52fc7f4742dd95937efe499f96989c853939543ba857e8af32346f65852cbb71e0c3dd
7
- data.tar.gz: 7b87dfa829e29eb266083e1b8d7df418c519273fb5fcab0095d6426b54cee22319303aa122825aa6f23ff635d32a28eb1644efa4f608c697c869c7b37405af0f
6
+ metadata.gz: f92d1aef2013a80abb618b49ce72d07406a93678b0ce183ce2c6051c397ccc24dd121d21eda707fab2dae042b369e83cf1deb6e45db15770b1a417b989014fe6
7
+ data.tar.gz: eeb2491fca41a8a3c858bad2314c4c8d3a29384f7f0da9e836b871071110f321c76ea697222f6b47903839f344bfbdcf85e0636b59a80dca1381bfa29aaa1b59
@@ -23,7 +23,7 @@ matrix:
23
23
  - rvm: rbx-3
24
24
  fast_finish: true
25
25
  before_install:
26
- - gem update bundler
26
+ - gem install bundler --version '~> 1.17'
27
27
  script: bundle exec rspec spec
28
28
  notifications:
29
29
  email:
@@ -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
@@ -12,5 +12,6 @@ elsif RUBY_VERSION =~ /^1/
12
12
  gem 'tins', '~> 1.6.0'
13
13
  gem 'term-ansicolor', '< 1.4'
14
14
  end
15
+ gem 'i18n', '<= 1.2.0' if RUBY_VERSION < '2.3'
15
16
 
16
17
  gemspec
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).
@@ -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",
@@ -5,7 +5,8 @@ class Money
5
5
  class I18n < Base
6
6
  KEY_MAP = {
7
7
  thousands_separator: :delimiter,
8
- decimal_mark: :separator
8
+ decimal_mark: :separator,
9
+ symbol: :unit
9
10
  }.freeze
10
11
 
11
12
  def initialize
@@ -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 : as_d(obj)
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 = remaining_amount * part / parts_sum
28
- current_split = current_split.truncate if whole_amounts
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
- warn '[DEPRECATION] `symbol_position:` option is deprecated - use `format` to specify the formatting template.'
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 '[DEPRECATION] `symbol_before_without_space:` option is deprecated - use `format` to specify the formatting template.'
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 '[DEPRECATION] `symbol_after_without_space:` option is deprecated - use `format` to specify the formatting template.'
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
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = '6.13.1'
2
+ VERSION = '6.13.2'
3
3
  end
@@ -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", "~> 1.3"
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
@@ -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.1
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: 2018-10-27 00:00:00.000000000 Z
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: '1.3'
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: '1.3'
46
+ version: '0'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement