monetize 1.9.1 → 1.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c921047a6f291ae61ceb19db130de7b0288d8e2
4
- data.tar.gz: f48eb20c8582b1453ef6114df30793f9367d934e
3
+ metadata.gz: e6d46a5e6d4c8b7e1dec430cfeb98a98536bb336
4
+ data.tar.gz: 523799519e83fca200b5d813a0ca010ab2ae090e
5
5
  SHA512:
6
- metadata.gz: 9b4a49126c30cfd92968d6fed6cd43a806e5cda807f8dfd0c8956d434e0e3a3331191d9e228e5648c4d11157f01d3c3fcdb72f72f21e52f96002ab803b70265a
7
- data.tar.gz: 571ec0ec170014d408f4ba0208a1b40f04cdf3436b2939e7d43b922cfe2ce3e981fb5a51a6dc79eab8d07ccad4ecfbc270548e15b4293428df87dbc0c912d277
6
+ metadata.gz: a88d6ce931f8c0d37653c3067a76caa4be8f5adbc10d78e2c1ee5d8b8089163b38926506c5e1a3524192b3d7e745a22f1d48d9773570239ce11f5a0fc122ee1d
7
+ data.tar.gz: d83465b93fdf95a3cb25ecfa805e694a775dab789766b046076948382cb01f64aeb78484b53e9afa60fd557a317fbfcd3ac51208559693c8196e9228a27c9ea6
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.9.2
4
+ - Respect Money.rounding_mode when parsing strings
5
+ - Deprecate Monetize.extract_cents
6
+
3
7
  ## 1.9.1
4
8
  - Ruby 2.6 support
5
9
 
@@ -31,9 +31,9 @@ module Monetize
31
31
  return from_numeric(input, currency) if input.is_a?(Numeric)
32
32
 
33
33
  parser = Monetize::Parser.new(input, currency, options)
34
- currency_from_input = Money::Currency.wrap(parser.parse_currency)
34
+ amount, currency = parser.parse
35
35
 
36
- Money.new(parser.parse_cents(currency_from_input), currency_from_input)
36
+ Money.from_amount(amount, currency)
37
37
  rescue Money::Currency::UnknownCurrency => e
38
38
  fail ParseError, e.message
39
39
  end
@@ -66,7 +66,10 @@ module Monetize
66
66
  end
67
67
 
68
68
  def extract_cents(input, currency = Money.default_currency)
69
- Monetize::Parser.new(input).parse_cents(currency)
69
+ warn '[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents'
70
+
71
+ money = parse(input, currency)
72
+ money.cents if money
70
73
  end
71
74
  end
72
75
  end
@@ -29,13 +29,17 @@ module Monetize
29
29
  MULTIPLIER_SUFFIXES.default = 0
30
30
  MULTIPLIER_REGEXP = Regexp.new(format('^(.*?\d)(%s)\b([^\d]*)$', MULTIPLIER_SUFFIXES.keys.join('|')), 'i')
31
31
 
32
+ DEFAULT_DECIMAL_MARK = '.'.freeze
33
+
32
34
  def initialize(input, fallback_currency = Money.default_currency, options = {})
33
35
  @input = input.to_s.strip
34
36
  @fallback_currency = fallback_currency
35
37
  @options = options
36
38
  end
37
39
 
38
- def parse_cents(currency)
40
+ def parse
41
+ currency = Money::Currency.wrap(parse_currency)
42
+
39
43
  multiplier_exp, input = extract_multiplier
40
44
 
41
45
  num = input.gsub(/(?:^#{currency.symbol}|[^\d.,'-]+)/, '')
@@ -46,14 +50,16 @@ module Monetize
46
50
 
47
51
  major, minor = extract_major_minor(num, currency)
48
52
 
49
- major, minor = apply_multiplier(multiplier_exp, major.to_i, minor)
53
+ amount = BigDecimal([major, minor].join(DEFAULT_DECIMAL_MARK))
54
+ amount = apply_multiplier(multiplier_exp, amount)
55
+ amount = apply_sign(negative, amount)
50
56
 
51
- cents = major.to_i * currency.subunit_to_unit
57
+ [amount, currency]
58
+ end
52
59
 
53
- cents += set_minor_precision(minor, currency)
60
+ private
54
61
 
55
- apply_sign(negative, cents)
56
- end
62
+ attr_reader :input, :fallback_currency, :options
57
63
 
58
64
  def parse_currency
59
65
  computed_currency = nil
@@ -63,25 +69,16 @@ module Monetize
63
69
  computed_currency || fallback_currency || Money.default_currency
64
70
  end
65
71
 
66
- private
67
-
68
- attr_reader :input, :fallback_currency, :options
69
-
70
72
  def assume_from_symbol?
71
73
  options.fetch(:assume_from_symbol) { Monetize.assume_from_symbol }
72
74
  end
73
75
 
74
- def apply_multiplier(multiplier_exp, major, minor)
75
- major *= 10**multiplier_exp
76
- minor = minor.to_s + ('0' * multiplier_exp)
77
- shift = minor[0...multiplier_exp].to_i
78
- major += shift
79
- minor = (minor[multiplier_exp..-1] || '')
80
- [major, minor]
76
+ def apply_multiplier(multiplier_exp, amount)
77
+ amount * 10**multiplier_exp
81
78
  end
82
79
 
83
- def apply_sign(negative, cents)
84
- negative ? cents * -1 : cents
80
+ def apply_sign(negative, amount)
81
+ negative ? amount * -1 : amount
85
82
  end
86
83
 
87
84
  def compute_currency
@@ -150,22 +147,6 @@ module Monetize
150
147
  CURRENCY_SYMBOLS.keys.map { |key| Regexp.escape(key) }.join('|')
151
148
  end
152
149
 
153
- def set_minor_precision(minor, currency)
154
- if Money.infinite_precision
155
- (BigDecimal(minor) / (10**minor.size)) * currency.subunit_to_unit
156
- elsif minor.size < currency.decimal_places
157
- (minor + ('0' * currency.decimal_places))[0, currency.decimal_places].to_i
158
- elsif minor.size > currency.decimal_places
159
- if minor[currency.decimal_places, 1].to_i >= 5
160
- minor[0, currency.decimal_places].to_i + 1
161
- else
162
- minor[0, currency.decimal_places].to_i
163
- end
164
- else
165
- minor.to_i
166
- end
167
- end
168
-
169
150
  def split_major_minor(num, delimiter)
170
151
  major, minor = num.split(delimiter)
171
152
  [major, minor || '00']
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Monetize
4
- VERSION = '1.9.1'
4
+ VERSION = '1.9.2'
5
5
  end
@@ -7,8 +7,8 @@ require 'English'
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'monetize'
9
9
  spec.version = Monetize::VERSION
10
- spec.authors = ['Shane Emmons']
11
- spec.email = ['shane@emmons.io']
10
+ spec.authors = ['Shane Emmons', 'Anthony Dmitriyev']
11
+ spec.email = ['shane@emmons.io', 'anthony.dmitriyev@gmail.com']
12
12
  spec.description = 'A library for converting various objects into `Money` objects.'
13
13
  spec.summary = 'A library for converting various objects into `Money` objects.'
14
14
  spec.homepage = 'https://github.com/RubyMoney/monetize'
@@ -67,7 +67,8 @@ describe Monetize, 'core extensions' do
67
67
  '-1,000' => Money.new(-1_000_00),
68
68
  '1,000.5' => Money.new(1_000_50),
69
69
  '1,000.51' => Money.new(1_000_51),
70
- '1,000.505' => 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
71
72
  '1,000.504' => Money.new(1_000_50),
72
73
  '1,000.0000' => Money.new(1_000_00),
73
74
  '1,000.5000' => Money.new(1_000_50),
@@ -149,6 +150,28 @@ describe Monetize, 'core extensions' do
149
150
  expect('1'.to_money('JPY')).to eq Money.new(1, 'JPY')
150
151
  expect('1.5'.to_money('KWD').cents).to eq 1_500
151
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
152
175
  end
153
176
 
154
177
  describe '#to_currency' do
@@ -520,6 +520,20 @@ describe Monetize do
520
520
  end
521
521
 
522
522
  describe '.extract_cents' do
523
+ it 'is deprecated' do
524
+ allow(Monetize).to receive(:warn)
525
+
526
+ Monetize.extract_cents('100')
527
+
528
+ expect(Monetize)
529
+ .to have_received(:warn)
530
+ .with('[DEPRECATION] Monetize.extract_cents is deprecated. Use Monetize.parse().cents')
531
+ end
532
+
533
+ it 'extracts cents from a given string' do
534
+ expect(Monetize.extract_cents('10.99')).to eq(1099)
535
+ end
536
+
523
537
  it "correctly treats pipe marks '|' in input (regression test)" do
524
538
  expect(Monetize.extract_cents('100|0')).to eq Monetize.extract_cents('100!0')
525
539
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.1
4
+ version: 1.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
+ - Anthony Dmitriyev
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2019-02-16 00:00:00.000000000 Z
12
+ date: 2019-07-06 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: money
@@ -69,6 +70,7 @@ dependencies:
69
70
  description: A library for converting various objects into `Money` objects.
70
71
  email:
71
72
  - shane@emmons.io
73
+ - anthony.dmitriyev@gmail.com
72
74
  executables: []
73
75
  extensions: []
74
76
  extra_rdoc_files: []