monetize 1.8.0 → 1.9.0

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: 2a6b1289754e6b83edb823b89e503d483cef3702
4
- data.tar.gz: 7a8101c6719d5fb91f79ec85be449520ada30225
3
+ metadata.gz: e26348a04058d672c804c80a19beb2c4615f9a33
4
+ data.tar.gz: 68697cc2d144f4e6ce17e7784e0dcb5188a482fb
5
5
  SHA512:
6
- metadata.gz: bd40b6b99cc913a0e8f65bad08f7d1ab0ad06a0117f5b1038e4fb43a559b04a0812bdd28e0826005f979f565ba9e6a404fa73e617457c7d5ea0c0d43b8480f93
7
- data.tar.gz: e356b03bf3a1c5ad46ab0725e1c99e1bd52723e451f28e1c68f3b6dbfe3529d21a5750046a595f0c7c2b7f6de10c71f1abfa02d10caa3ef71d056ec04a4e0085
6
+ metadata.gz: 452be2be436b0ea57611a2a32b3230486f8f3de40cb74e4286931528a6db05fd2cddc114ca5373be7a92f4680a0f50a3992af373920b09f65c233fd0f9cb0007
7
+ data.tar.gz: db4134e4c44bb2b58fd12d6c7ff2d5e3259b76ba6971215cbdd61759d715cf98b014066f165dfea5cf79ac5c7997937ca1fc3ff0ceae8ce604424c41370def1f
@@ -4,9 +4,10 @@ rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
6
  - 2.1.10
7
- - 2.2.7
8
- - 2.3.4
9
- - 2.4.1
7
+ - 2.2.10
8
+ - 2.3.7
9
+ - 2.4.4
10
+ - 2.5.1
10
11
  - jruby-9.0.5.0
11
12
  script: bundle exec rspec spec
12
13
  notifications:
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.9.0
4
+ - Relax Money gem dependency to ~> 6.12
5
+ - Refactor `Monetize::Parser`
6
+ - Ruby 2.5 support
7
+
3
8
  ## 1.8.0
4
9
  - Money version updated to 6.11.2
5
10
  - Fix rounding problem in `.from_bigdecimal`
@@ -19,64 +19,54 @@ module Monetize
19
19
  # though, it will try to determine the correct separator by itself. Set this
20
20
  # to true to enforce the delimiters set in the currency all the time.
21
21
  attr_accessor :enforce_currency_delimiters
22
- end
23
22
 
24
- def self.parse(input, currency = Money.default_currency, options = {})
25
- parse! input, currency, options
26
- rescue Error
27
- nil
28
- end
23
+ def parse(input, currency = Money.default_currency, options = {})
24
+ parse! input, currency, options
25
+ rescue Error
26
+ nil
27
+ end
29
28
 
30
- def self.parse!(input, currency = Money.default_currency, options = {})
31
- return input if input.is_a?(Money)
32
- return from_numeric(input, currency) if input.is_a?(Numeric)
29
+ def parse!(input, currency = Money.default_currency, options = {})
30
+ return input if input.is_a?(Money)
31
+ return from_numeric(input, currency) if input.is_a?(Numeric)
33
32
 
34
- parser = Monetize::Parser.new(input, currency, options)
35
- currency_from_input = Money::Currency.wrap(parser.parse_currency)
33
+ parser = Monetize::Parser.new(input, currency, options)
34
+ currency_from_input = Money::Currency.wrap(parser.parse_currency)
36
35
 
37
- Money.new(parser.parse_cents(currency_from_input), currency_from_input)
38
- rescue Money::Currency::UnknownCurrency => e
39
- fail ParseError, e.message
40
- end
36
+ Money.new(parser.parse_cents(currency_from_input), currency_from_input)
37
+ rescue Money::Currency::UnknownCurrency => e
38
+ fail ParseError, e.message
39
+ end
41
40
 
42
- def self.parse_collection(input, currency = Money.default_currency, options = {})
43
- Collection.parse(input, currency, options)
44
- end
41
+ def parse_collection(input, currency = Money.default_currency, options = {})
42
+ Collection.parse(input, currency, options)
43
+ end
45
44
 
46
- def self.from_string(value, currency = Money.default_currency)
47
- value = BigDecimal.new(value.to_s)
48
- from_bigdecimal(value, currency)
49
- end
45
+ def from_string(value, currency = Money.default_currency)
46
+ value = BigDecimal.new(value.to_s)
47
+ Money.from_amount(value, currency)
48
+ end
50
49
 
51
- def self.from_fixnum(value, currency = Money.default_currency)
52
- currency = Money::Currency.wrap(currency)
53
- value *= currency.subunit_to_unit
54
- Money.new(value, currency)
55
- end
56
- singleton_class.send(:alias_method, :from_integer, :from_fixnum)
50
+ def from_fixnum(value, currency = Money.default_currency)
51
+ Money.from_amount(value, currency)
52
+ end
53
+ alias_method :from_integer, :from_fixnum
57
54
 
58
- def self.from_float(value, currency = Money.default_currency)
59
- value = BigDecimal.new(value.to_s)
60
- from_bigdecimal(value, currency)
61
- end
55
+ def from_float(value, currency = Money.default_currency)
56
+ Money.from_amount(value, currency)
57
+ end
62
58
 
63
- def self.from_bigdecimal(value, currency = Money.default_currency)
64
- Money.from_amount(value, currency)
65
- end
59
+ def from_bigdecimal(value, currency = Money.default_currency)
60
+ Money.from_amount(value, currency)
61
+ end
66
62
 
67
- def self.from_numeric(value, currency = Money.default_currency)
68
- case value
69
- when Integer
70
- from_fixnum(value, currency)
71
- when Numeric
72
- value = BigDecimal.new(value.to_s)
73
- from_bigdecimal(value, currency)
74
- else
75
- fail ArgumentError, "'value' should be a type of Numeric"
63
+ def from_numeric(value, currency = Money.default_currency)
64
+ fail ArgumentError, "'value' should be a type of Numeric" unless value.is_a?(Numeric)
65
+ Money.from_amount(value, currency)
76
66
  end
77
- end
78
67
 
79
- def self.extract_cents(input, currency = Money.default_currency)
80
- Monetize::Parser.new(input).parse_cents(currency)
68
+ def extract_cents(input, currency = Money.default_currency)
69
+ Monetize::Parser.new(input).parse_cents(currency)
70
+ end
81
71
  end
82
72
  end
@@ -42,7 +42,7 @@ module Monetize
42
42
 
43
43
  negative, num = extract_sign(num)
44
44
 
45
- num.chop! if num.match(/[\.|,]$/)
45
+ num.chop! if num =~ /[\.|,]$/
46
46
 
47
47
  major, minor = extract_major_minor(num, currency)
48
48
 
@@ -56,11 +56,9 @@ module Monetize
56
56
  end
57
57
 
58
58
  def parse_currency
59
- computed_currency = if options.fetch(:assume_from_symbol) { Monetize.assume_from_symbol }
60
- compute_currency
61
- else
62
- input[/[A-Z]{2,3}/]
63
- end
59
+ computed_currency = nil
60
+ computed_currency = compute_currency if assume_from_symbol?
61
+ computed_currency ||= input[/[A-Z]{2,3}/]
64
62
 
65
63
  computed_currency || fallback_currency || Money.default_currency
66
64
  end
@@ -69,6 +67,10 @@ module Monetize
69
67
 
70
68
  attr_reader :input, :fallback_currency, :options
71
69
 
70
+ def assume_from_symbol?
71
+ options.fetch(:assume_from_symbol) { Monetize.assume_from_symbol }
72
+ end
73
+
72
74
  def apply_multiplier(multiplier_exp, major, minor)
73
75
  major *= 10**multiplier_exp
74
76
  minor = minor.to_s + ('0' * multiplier_exp)
@@ -82,17 +84,9 @@ module Monetize
82
84
  negative ? cents * -1 : cents
83
85
  end
84
86
 
85
- def contains_currency_symbol?
86
- input =~ currency_symbol_regex
87
- end
88
-
89
87
  def compute_currency
90
- if contains_currency_symbol?
91
- matches = input.match(currency_symbol_regex)
92
- CURRENCY_SYMBOLS[matches[:symbol]]
93
- else
94
- input[/[A-Z]{2,3}/]
95
- end
88
+ matches = input.match(currency_symbol_regex)
89
+ CURRENCY_SYMBOLS[matches[:symbol]] if matches
96
90
  end
97
91
 
98
92
  def extract_major_minor(num, currency)
@@ -114,7 +108,7 @@ module Monetize
114
108
  def extract_major_minor_with_single_delimiter(num, currency, delimiter)
115
109
  if delimiter == currency.decimal_mark
116
110
  split_major_minor(num, delimiter)
117
- elsif Monetize.enforce_currency_delimiters and delimiter == currency.thousands_separator
111
+ elsif Monetize.enforce_currency_delimiters && delimiter == currency.thousands_separator
118
112
  [num.gsub(delimiter, ''), 0]
119
113
  else
120
114
  extract_major_minor_with_tentative_delimiter(num, delimiter)
@@ -128,7 +122,7 @@ module Monetize
128
122
  else
129
123
  possible_major, possible_minor = split_major_minor(num, delimiter)
130
124
 
131
- if possible_minor.length != 3 or possible_major.length > 3 or delimiter == '.'
125
+ if possible_minor.length != 3 || possible_major.length > 3 || delimiter == '.'
132
126
  # Doesn't look like thousands separator
133
127
  [possible_major, possible_minor]
134
128
  else
@@ -147,7 +141,7 @@ module Monetize
147
141
  end
148
142
 
149
143
  def extract_sign(input)
150
- result = (input =~ /^-+(.*)$/ or input =~ /^(.*)-+$/) ? [true, $1] : [false, input]
144
+ result = (input =~ /^-+(.*)$/ || input =~ /^(.*)-+$/) ? [true, $1] : [false, input]
151
145
  fail ParseError, 'Invalid amount (hyphen)' if result[1].include?('-')
152
146
  result
153
147
  end
@@ -174,8 +168,7 @@ module Monetize
174
168
 
175
169
  def split_major_minor(num, delimiter)
176
170
  major, minor = num.split(delimiter)
177
- minor = '00' unless minor
178
- [major, minor]
171
+ [major, minor || '00']
179
172
  end
180
173
 
181
174
  def currency_symbol_regex
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Monetize
4
- VERSION = '1.8.0'
4
+ VERSION = '1.9.0'
5
5
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency 'money', '~> 6.11.2'
22
+ spec.add_dependency 'money', '~> 6.12'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.3'
25
25
  spec.add_development_dependency 'rake'
@@ -483,14 +483,6 @@ describe Monetize do
483
483
  expect { Monetize.from_numeric('100') }.to raise_error(Monetize::ArgumentError)
484
484
  end
485
485
 
486
- it 'optimizes workload' do
487
- expect(Monetize).to receive(:from_fixnum).with(1, 'USD').and_return(Money.new(1_00, 'USD'))
488
- expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
489
- expect(Monetize).to receive(:from_bigdecimal).with(BigDecimal.new('1.0'), 'USD').
490
- and_return(Money.new(1_00, 'USD'))
491
- expect(Monetize.from_numeric(1.0, 'USD')).to eq Money.new(1_00, 'USD')
492
- end
493
-
494
486
  it 'respects :subunit_to_unit currency property' do
495
487
  expect(Monetize.from_numeric(1, 'USD')).to eq Money.new(1_00, 'USD')
496
488
  expect(Monetize.from_numeric(1, 'TND')).to eq Money.new(1_000, 'TND')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monetize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.0
4
+ version: 1.9.0
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-05-03 00:00:00.000000000 Z
11
+ date: 2018-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 6.11.2
19
+ version: '6.12'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 6.11.2
26
+ version: '6.12'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement