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 +4 -4
- data/.travis.yml +4 -3
- data/CHANGELOG.md +5 -0
- data/lib/monetize.rb +37 -47
- data/lib/monetize/parser.rb +14 -21
- data/lib/monetize/version.rb +1 -1
- data/monetize.gemspec +1 -1
- data/spec/monetize_spec.rb +0 -8
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e26348a04058d672c804c80a19beb2c4615f9a33
|
4
|
+
data.tar.gz: 68697cc2d144f4e6ce17e7784e0dcb5188a482fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 452be2be436b0ea57611a2a32b3230486f8f3de40cb74e4286931528a6db05fd2cddc114ca5373be7a92f4680a0f50a3992af373920b09f65c233fd0f9cb0007
|
7
|
+
data.tar.gz: db4134e4c44bb2b58fd12d6c7ff2d5e3259b76ba6971215cbdd61759d715cf98b014066f165dfea5cf79ac5c7997937ca1fc3ff0ceae8ce604424c41370def1f
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/lib/monetize.rb
CHANGED
@@ -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
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
def parse(input, currency = Money.default_currency, options = {})
|
24
|
+
parse! input, currency, options
|
25
|
+
rescue Error
|
26
|
+
nil
|
27
|
+
end
|
29
28
|
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
33
|
+
parser = Monetize::Parser.new(input, currency, options)
|
34
|
+
currency_from_input = Money::Currency.wrap(parser.parse_currency)
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
41
|
+
def parse_collection(input, currency = Money.default_currency, options = {})
|
42
|
+
Collection.parse(input, currency, options)
|
43
|
+
end
|
45
44
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
55
|
+
def from_float(value, currency = Money.default_currency)
|
56
|
+
Money.from_amount(value, currency)
|
57
|
+
end
|
62
58
|
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
def from_bigdecimal(value, currency = Money.default_currency)
|
60
|
+
Money.from_amount(value, currency)
|
61
|
+
end
|
66
62
|
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
80
|
-
|
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
|
data/lib/monetize/parser.rb
CHANGED
@@ -42,7 +42,7 @@ module Monetize
|
|
42
42
|
|
43
43
|
negative, num = extract_sign(num)
|
44
44
|
|
45
|
-
num.chop! if num
|
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 =
|
60
|
-
|
61
|
-
|
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
|
-
|
91
|
-
|
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
|
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
|
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 =~ /^-+(.*)$/
|
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
|
178
|
-
[major, minor]
|
171
|
+
[major, minor || '00']
|
179
172
|
end
|
180
173
|
|
181
174
|
def currency_symbol_regex
|
data/lib/monetize/version.rb
CHANGED
data/monetize.gemspec
CHANGED
@@ -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.
|
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'
|
data/spec/monetize_spec.rb
CHANGED
@@ -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.
|
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-
|
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.
|
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.
|
26
|
+
version: '6.12'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|