monetize 1.7.0 → 1.8.0
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 +4 -4
- data/CHANGELOG.md +40 -19
- data/lib/monetize.rb +7 -181
- data/lib/{collection.rb → monetize/collection.rb} +0 -1
- data/lib/monetize/parser.rb +185 -0
- data/lib/monetize/version.rb +1 -1
- data/monetize.gemspec +1 -1
- data/spec/monetize_spec.rb +7 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a6b1289754e6b83edb823b89e503d483cef3702
|
4
|
+
data.tar.gz: 7a8101c6719d5fb91f79ec85be449520ada30225
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd40b6b99cc913a0e8f65bad08f7d1ab0ad06a0117f5b1038e4fb43a559b04a0812bdd28e0826005f979f565ba9e6a404fa73e617457c7d5ea0c0d43b8480f93
|
7
|
+
data.tar.gz: e356b03bf3a1c5ad46ab0725e1c99e1bd52723e451f28e1c68f3b6dbfe3529d21a5750046a595f0c7c2b7f6de10c71f1abfa02d10caa3ef71d056ec04a4e0085
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.8.0
|
4
|
+
- Money version updated to 6.11.2
|
5
|
+
- Fix rounding problem in `.from_bigdecimal`
|
6
|
+
|
3
7
|
## 1.7.0
|
8
|
+
- Add South Korean Won currency
|
4
9
|
- Money version updated to 6.9
|
5
10
|
- Coveralls version update to 0.8.20
|
6
11
|
- Add South Korean Won currency
|
@@ -11,47 +16,63 @@
|
|
11
16
|
- Money version updated to 6.8
|
12
17
|
|
13
18
|
## 1.5.0
|
19
|
+
- Fix issue where parsing a Money object resulted in a Money object with its currency set to `Money.default_currency`,
|
20
|
+
rather than the currency that it was sent in as.
|
14
21
|
- Add extra currencies:
|
15
22
|
- Azerbaijani manat
|
16
23
|
- Chinese yuan
|
17
24
|
- Czech koruna
|
18
25
|
- Hungarian forint
|
19
|
-
-
|
26
|
+
- Indian rupee
|
20
27
|
- Russian ruble
|
21
28
|
- Turkish Lira
|
22
29
|
- Ukrainian Hryvnia
|
23
30
|
- Swiss Frank
|
24
31
|
- Polish Zloty
|
25
32
|
- Kazakhstani Tenge
|
26
|
-
-
|
27
|
-
- Fix issue with loosing precision on BigDecimal input
|
33
|
+
- Fix issue with losing precision on BigDecimal input
|
28
34
|
- Add Swedish krona
|
29
|
-
-
|
35
|
+
- Exclude ambiguous kr symbol from parsing
|
30
36
|
- Fix JPY parsing
|
31
|
-
-
|
37
|
+
- Subclass all errors to Monetize::Error
|
32
38
|
- Fix ruby 1.9.3 compatibility
|
33
39
|
- Suppress errors when using parse. Use `parse!` instead
|
34
40
|
- Strip currency symbol prefix when parsing input
|
35
41
|
|
36
42
|
## 1.4.0
|
43
|
+
- Fixed support for <code>Money.infinite_precision = true</code> in .to_money
|
44
|
+
- Add Rubocop config to project
|
45
|
+
- Reformat code to adapt to Rubocop guidelines
|
46
|
+
- Add config setting to always enforce currency delimiters
|
47
|
+
- Add rake console task
|
37
48
|
- Required Forwardable on Collection to resolve NameError [\#44](https://github.com/RubyMoney/monetize/issues/44)
|
38
49
|
- Add capability to parse currency amounts given with suffixes (K, M, B, and T)
|
39
50
|
|
51
|
+
## 1.3.1
|
52
|
+
- Updated Money version dependency to 6.6
|
53
|
+
|
40
54
|
## 1.3.0
|
41
|
-
- Add NilClass extension
|
42
|
-
- Added correct parsing of Brazilian Real $R symbol
|
43
|
-
- Add testing task for Brazilian Real parsing
|
44
55
|
- Add Lira Sign (₤) as a symbol for GBP
|
45
56
|
|
46
|
-
## 1.
|
47
|
-
-
|
57
|
+
## 1.2.0
|
58
|
+
- Add support for parsing Yen symbol
|
59
|
+
- Add `Monetize.parse_collection` and `Monetize::Collection` class for parsing multiple values
|
60
|
+
- Add parsing of C$ for Canadian Dollar
|
61
|
+
- Add NilClass extension
|
62
|
+
- Add Hash extension
|
48
63
|
|
49
|
-
##
|
50
|
-
-
|
51
|
-
-
|
52
|
-
-
|
53
|
-
|
54
|
-
|
55
|
-
-
|
56
|
-
|
57
|
-
|
64
|
+
## 1.1.0
|
65
|
+
- Add :assume_from_symbol option to #parse
|
66
|
+
- Enable #parse to detect currency with signed amounts
|
67
|
+
- Updated Money version dependency to 6.5.0
|
68
|
+
|
69
|
+
## 1.0.0
|
70
|
+
- Updated Money version dependency to 6.4.0
|
71
|
+
|
72
|
+
## 0.4.1
|
73
|
+
- Updated Money version dependency to 6.2.1
|
74
|
+
|
75
|
+
## 0.4.0
|
76
|
+
- Added correct parsing of Brazilian Real $R symbol
|
77
|
+
- Add testing task for Brazilian Real parsing
|
78
|
+
- Updated Money version dependency to 6.2.0
|
data/lib/monetize.rb
CHANGED
@@ -4,41 +4,10 @@ require 'money'
|
|
4
4
|
require 'monetize/core_extensions'
|
5
5
|
require 'monetize/errors'
|
6
6
|
require 'monetize/version'
|
7
|
-
require '
|
7
|
+
require 'monetize/parser'
|
8
|
+
require 'monetize/collection'
|
8
9
|
|
9
10
|
module Monetize
|
10
|
-
CURRENCY_SYMBOLS = {
|
11
|
-
'$' => 'USD',
|
12
|
-
'€' => 'EUR',
|
13
|
-
'£' => 'GBP',
|
14
|
-
'₤' => 'GBP',
|
15
|
-
'R$' => 'BRL',
|
16
|
-
'R' => 'ZAR',
|
17
|
-
'¥' => 'JPY',
|
18
|
-
'C$' => 'CAD',
|
19
|
-
'₼' => 'AZN',
|
20
|
-
'元' => 'CNY',
|
21
|
-
'Kč' => 'CZK',
|
22
|
-
'Ft' => 'HUF',
|
23
|
-
'₹' => 'INR',
|
24
|
-
'₽' => 'RUB',
|
25
|
-
'₺' => 'TRY',
|
26
|
-
'₴' => 'UAH',
|
27
|
-
'Fr' => 'CHF',
|
28
|
-
'zł' => 'PLN',
|
29
|
-
'₸' => 'KZT',
|
30
|
-
"₩" => 'KRW',
|
31
|
-
}
|
32
|
-
|
33
|
-
MULTIPLIER_SUFFIXES = {
|
34
|
-
'K' => 3,
|
35
|
-
'M' => 6,
|
36
|
-
'B' => 9,
|
37
|
-
'T' => 12
|
38
|
-
}
|
39
|
-
MULTIPLIER_SUFFIXES.default = 0
|
40
|
-
MULTIPLIER_REGEXP = Regexp.new(format('^(.*?\d)(%s)\b([^\d]*)$', MULTIPLIER_SUFFIXES.keys.join('|')), 'i')
|
41
|
-
|
42
11
|
# Class methods
|
43
12
|
class << self
|
44
13
|
# @attr_accessor [true, false] assume_from_symbol Use this to enable the
|
@@ -62,19 +31,10 @@ module Monetize
|
|
62
31
|
return input if input.is_a?(Money)
|
63
32
|
return from_numeric(input, currency) if input.is_a?(Numeric)
|
64
33
|
|
65
|
-
|
66
|
-
|
67
|
-
computed_currency = if options.fetch(:assume_from_symbol) { assume_from_symbol }
|
68
|
-
compute_currency(input)
|
69
|
-
else
|
70
|
-
input[/[A-Z]{2,3}/]
|
71
|
-
end
|
72
|
-
|
73
|
-
currency = computed_currency || currency || Money.default_currency
|
74
|
-
currency = Money::Currency.wrap(currency)
|
34
|
+
parser = Monetize::Parser.new(input, currency, options)
|
35
|
+
currency_from_input = Money::Currency.wrap(parser.parse_currency)
|
75
36
|
|
76
|
-
|
77
|
-
Money.new(fractional, currency)
|
37
|
+
Money.new(parser.parse_cents(currency_from_input), currency_from_input)
|
78
38
|
rescue Money::Currency::UnknownCurrency => e
|
79
39
|
fail ParseError, e.message
|
80
40
|
end
|
@@ -101,10 +61,7 @@ module Monetize
|
|
101
61
|
end
|
102
62
|
|
103
63
|
def self.from_bigdecimal(value, currency = Money.default_currency)
|
104
|
-
|
105
|
-
value *= currency.subunit_to_unit
|
106
|
-
value = value.round unless Money.infinite_precision
|
107
|
-
Money.new(value, currency)
|
64
|
+
Money.from_amount(value, currency)
|
108
65
|
end
|
109
66
|
|
110
67
|
def self.from_numeric(value, currency = Money.default_currency)
|
@@ -120,137 +77,6 @@ module Monetize
|
|
120
77
|
end
|
121
78
|
|
122
79
|
def self.extract_cents(input, currency = Money.default_currency)
|
123
|
-
|
124
|
-
|
125
|
-
num = input.gsub(/(?:^#{currency.symbol}|[^\d.,'-]+)/, '')
|
126
|
-
|
127
|
-
negative, num = extract_sign(num)
|
128
|
-
|
129
|
-
num.chop! if num.match(/[\.|,]$/)
|
130
|
-
|
131
|
-
major, minor = extract_major_minor(num, currency)
|
132
|
-
|
133
|
-
major, minor = apply_multiplier(multiplier_exp, major.to_i, minor)
|
134
|
-
|
135
|
-
cents = major.to_i * currency.subunit_to_unit
|
136
|
-
|
137
|
-
cents += set_minor_precision(minor, currency)
|
138
|
-
|
139
|
-
apply_sign(negative, cents)
|
140
|
-
end
|
141
|
-
|
142
|
-
private
|
143
|
-
|
144
|
-
def self.apply_multiplier(multiplier_exp, major, minor)
|
145
|
-
major *= 10**multiplier_exp
|
146
|
-
minor = minor.to_s + ('0' * multiplier_exp)
|
147
|
-
shift = minor[0...multiplier_exp].to_i
|
148
|
-
major += shift
|
149
|
-
minor = (minor[multiplier_exp..-1] || '')
|
150
|
-
[major, minor]
|
151
|
-
end
|
152
|
-
|
153
|
-
def self.apply_sign(negative, cents)
|
154
|
-
negative ? cents * -1 : cents
|
155
|
-
end
|
156
|
-
|
157
|
-
def self.contains_currency_symbol?(amount)
|
158
|
-
amount =~ currency_symbol_regex
|
159
|
-
end
|
160
|
-
|
161
|
-
def self.compute_currency(amount)
|
162
|
-
if contains_currency_symbol?(amount)
|
163
|
-
matches = amount.match(currency_symbol_regex)
|
164
|
-
CURRENCY_SYMBOLS[matches[:symbol]]
|
165
|
-
else
|
166
|
-
amount[/[A-Z]{2,3}/]
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
def self.extract_major_minor(num, currency)
|
171
|
-
used_delimiters = num.scan(/[^\d]/).uniq
|
172
|
-
|
173
|
-
case used_delimiters.length
|
174
|
-
when 0
|
175
|
-
[num, 0]
|
176
|
-
when 2
|
177
|
-
thousands_separator, decimal_mark = used_delimiters
|
178
|
-
split_major_minor(num.gsub(thousands_separator, ''), decimal_mark)
|
179
|
-
when 1
|
180
|
-
extract_major_minor_with_single_delimiter(num, currency, used_delimiters.first)
|
181
|
-
else
|
182
|
-
fail ParseError, 'Invalid amount'
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
def self.extract_major_minor_with_single_delimiter(num, currency, delimiter)
|
187
|
-
if delimiter == currency.decimal_mark
|
188
|
-
split_major_minor(num, delimiter)
|
189
|
-
elsif enforce_currency_delimiters and delimiter == currency.thousands_separator
|
190
|
-
[num.gsub(delimiter, ''), 0]
|
191
|
-
else
|
192
|
-
extract_major_minor_with_tentative_delimiter(num, delimiter)
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
def self.extract_major_minor_with_tentative_delimiter(num, delimiter)
|
197
|
-
if num.scan(delimiter).length > 1
|
198
|
-
# Multiple matches; treat as thousands separator
|
199
|
-
[num.gsub(delimiter, ''), '00']
|
200
|
-
else
|
201
|
-
possible_major, possible_minor = split_major_minor(num, delimiter)
|
202
|
-
|
203
|
-
if possible_minor.length != 3 or possible_major.length > 3 or delimiter == '.'
|
204
|
-
# Doesn't look like thousands separator
|
205
|
-
[possible_major, possible_minor]
|
206
|
-
else
|
207
|
-
["#{possible_major}#{possible_minor}", '00']
|
208
|
-
end
|
209
|
-
end
|
210
|
-
end
|
211
|
-
|
212
|
-
def self.extract_multiplier(input)
|
213
|
-
if (matches = MULTIPLIER_REGEXP.match(input))
|
214
|
-
multiplier_suffix = matches[2].upcase
|
215
|
-
[MULTIPLIER_SUFFIXES[multiplier_suffix], "#{$1}#{$3}"]
|
216
|
-
else
|
217
|
-
[0, input]
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
def self.extract_sign(input)
|
222
|
-
result = (input =~ /^-+(.*)$/ or input =~ /^(.*)-+$/) ? [true, $1] : [false, input]
|
223
|
-
fail ParseError, 'Invalid amount (hyphen)' if result[1].include?('-')
|
224
|
-
result
|
225
|
-
end
|
226
|
-
|
227
|
-
def self.regex_safe_symbols
|
228
|
-
CURRENCY_SYMBOLS.keys.map { |key| Regexp.escape(key) }.join('|')
|
229
|
-
end
|
230
|
-
|
231
|
-
def self.set_minor_precision(minor, currency)
|
232
|
-
if Money.infinite_precision
|
233
|
-
(BigDecimal.new(minor) / (10**minor.size)) * currency.subunit_to_unit
|
234
|
-
elsif minor.size < currency.decimal_places
|
235
|
-
(minor + ('0' * currency.decimal_places))[0, currency.decimal_places].to_i
|
236
|
-
elsif minor.size > currency.decimal_places
|
237
|
-
if minor[currency.decimal_places, 1].to_i >= 5
|
238
|
-
minor[0, currency.decimal_places].to_i + 1
|
239
|
-
else
|
240
|
-
minor[0, currency.decimal_places].to_i
|
241
|
-
end
|
242
|
-
else
|
243
|
-
minor.to_i
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
def self.split_major_minor(num, delimiter)
|
248
|
-
major, minor = num.split(delimiter)
|
249
|
-
minor = '00' unless minor
|
250
|
-
[major, minor]
|
251
|
-
end
|
252
|
-
|
253
|
-
def self.currency_symbol_regex
|
254
|
-
/\A[\+|\-]?(?<symbol>#{regex_safe_symbols})/
|
80
|
+
Monetize::Parser.new(input).parse_cents(currency)
|
255
81
|
end
|
256
82
|
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monetize
|
4
|
+
class Parser
|
5
|
+
CURRENCY_SYMBOLS = {
|
6
|
+
'$' => 'USD',
|
7
|
+
'€' => 'EUR',
|
8
|
+
'£' => 'GBP',
|
9
|
+
'₤' => 'GBP',
|
10
|
+
'R$' => 'BRL',
|
11
|
+
'R' => 'ZAR',
|
12
|
+
'¥' => 'JPY',
|
13
|
+
'C$' => 'CAD',
|
14
|
+
'₼' => 'AZN',
|
15
|
+
'元' => 'CNY',
|
16
|
+
'Kč' => 'CZK',
|
17
|
+
'Ft' => 'HUF',
|
18
|
+
'₹' => 'INR',
|
19
|
+
'₽' => 'RUB',
|
20
|
+
'₺' => 'TRY',
|
21
|
+
'₴' => 'UAH',
|
22
|
+
'Fr' => 'CHF',
|
23
|
+
'zł' => 'PLN',
|
24
|
+
'₸' => 'KZT',
|
25
|
+
"₩" => 'KRW',
|
26
|
+
}
|
27
|
+
|
28
|
+
MULTIPLIER_SUFFIXES = { 'K' => 3, 'M' => 6, 'B' => 9, 'T' => 12 }
|
29
|
+
MULTIPLIER_SUFFIXES.default = 0
|
30
|
+
MULTIPLIER_REGEXP = Regexp.new(format('^(.*?\d)(%s)\b([^\d]*)$', MULTIPLIER_SUFFIXES.keys.join('|')), 'i')
|
31
|
+
|
32
|
+
def initialize(input, fallback_currency = Money.default_currency, options = {})
|
33
|
+
@input = input.to_s.strip
|
34
|
+
@fallback_currency = fallback_currency
|
35
|
+
@options = options
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_cents(currency)
|
39
|
+
multiplier_exp, input = extract_multiplier
|
40
|
+
|
41
|
+
num = input.gsub(/(?:^#{currency.symbol}|[^\d.,'-]+)/, '')
|
42
|
+
|
43
|
+
negative, num = extract_sign(num)
|
44
|
+
|
45
|
+
num.chop! if num.match(/[\.|,]$/)
|
46
|
+
|
47
|
+
major, minor = extract_major_minor(num, currency)
|
48
|
+
|
49
|
+
major, minor = apply_multiplier(multiplier_exp, major.to_i, minor)
|
50
|
+
|
51
|
+
cents = major.to_i * currency.subunit_to_unit
|
52
|
+
|
53
|
+
cents += set_minor_precision(minor, currency)
|
54
|
+
|
55
|
+
apply_sign(negative, cents)
|
56
|
+
end
|
57
|
+
|
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
|
64
|
+
|
65
|
+
computed_currency || fallback_currency || Money.default_currency
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
attr_reader :input, :fallback_currency, :options
|
71
|
+
|
72
|
+
def apply_multiplier(multiplier_exp, major, minor)
|
73
|
+
major *= 10**multiplier_exp
|
74
|
+
minor = minor.to_s + ('0' * multiplier_exp)
|
75
|
+
shift = minor[0...multiplier_exp].to_i
|
76
|
+
major += shift
|
77
|
+
minor = (minor[multiplier_exp..-1] || '')
|
78
|
+
[major, minor]
|
79
|
+
end
|
80
|
+
|
81
|
+
def apply_sign(negative, cents)
|
82
|
+
negative ? cents * -1 : cents
|
83
|
+
end
|
84
|
+
|
85
|
+
def contains_currency_symbol?
|
86
|
+
input =~ currency_symbol_regex
|
87
|
+
end
|
88
|
+
|
89
|
+
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
|
96
|
+
end
|
97
|
+
|
98
|
+
def extract_major_minor(num, currency)
|
99
|
+
used_delimiters = num.scan(/[^\d]/).uniq
|
100
|
+
|
101
|
+
case used_delimiters.length
|
102
|
+
when 0
|
103
|
+
[num, 0]
|
104
|
+
when 2
|
105
|
+
thousands_separator, decimal_mark = used_delimiters
|
106
|
+
split_major_minor(num.gsub(thousands_separator, ''), decimal_mark)
|
107
|
+
when 1
|
108
|
+
extract_major_minor_with_single_delimiter(num, currency, used_delimiters.first)
|
109
|
+
else
|
110
|
+
fail ParseError, 'Invalid amount'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def extract_major_minor_with_single_delimiter(num, currency, delimiter)
|
115
|
+
if delimiter == currency.decimal_mark
|
116
|
+
split_major_minor(num, delimiter)
|
117
|
+
elsif Monetize.enforce_currency_delimiters and delimiter == currency.thousands_separator
|
118
|
+
[num.gsub(delimiter, ''), 0]
|
119
|
+
else
|
120
|
+
extract_major_minor_with_tentative_delimiter(num, delimiter)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def extract_major_minor_with_tentative_delimiter(num, delimiter)
|
125
|
+
if num.scan(delimiter).length > 1
|
126
|
+
# Multiple matches; treat as thousands separator
|
127
|
+
[num.gsub(delimiter, ''), '00']
|
128
|
+
else
|
129
|
+
possible_major, possible_minor = split_major_minor(num, delimiter)
|
130
|
+
|
131
|
+
if possible_minor.length != 3 or possible_major.length > 3 or delimiter == '.'
|
132
|
+
# Doesn't look like thousands separator
|
133
|
+
[possible_major, possible_minor]
|
134
|
+
else
|
135
|
+
["#{possible_major}#{possible_minor}", '00']
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def extract_multiplier
|
141
|
+
if (matches = MULTIPLIER_REGEXP.match(input))
|
142
|
+
multiplier_suffix = matches[2].upcase
|
143
|
+
[MULTIPLIER_SUFFIXES[multiplier_suffix], "#{$1}#{$3}"]
|
144
|
+
else
|
145
|
+
[0, input]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def extract_sign(input)
|
150
|
+
result = (input =~ /^-+(.*)$/ or input =~ /^(.*)-+$/) ? [true, $1] : [false, input]
|
151
|
+
fail ParseError, 'Invalid amount (hyphen)' if result[1].include?('-')
|
152
|
+
result
|
153
|
+
end
|
154
|
+
|
155
|
+
def regex_safe_symbols
|
156
|
+
CURRENCY_SYMBOLS.keys.map { |key| Regexp.escape(key) }.join('|')
|
157
|
+
end
|
158
|
+
|
159
|
+
def set_minor_precision(minor, currency)
|
160
|
+
if Money.infinite_precision
|
161
|
+
(BigDecimal.new(minor) / (10**minor.size)) * currency.subunit_to_unit
|
162
|
+
elsif minor.size < currency.decimal_places
|
163
|
+
(minor + ('0' * currency.decimal_places))[0, currency.decimal_places].to_i
|
164
|
+
elsif minor.size > currency.decimal_places
|
165
|
+
if minor[currency.decimal_places, 1].to_i >= 5
|
166
|
+
minor[0, currency.decimal_places].to_i + 1
|
167
|
+
else
|
168
|
+
minor[0, currency.decimal_places].to_i
|
169
|
+
end
|
170
|
+
else
|
171
|
+
minor.to_i
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def split_major_minor(num, delimiter)
|
176
|
+
major, minor = num.split(delimiter)
|
177
|
+
minor = '00' unless minor
|
178
|
+
[major, minor]
|
179
|
+
end
|
180
|
+
|
181
|
+
def currency_symbol_regex
|
182
|
+
/\A[\+|\-]?(?<symbol>#{regex_safe_symbols})/
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
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.11.2'
|
23
23
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
25
25
|
spec.add_development_dependency 'rake'
|
data/spec/monetize_spec.rb
CHANGED
@@ -56,7 +56,7 @@ describe Monetize do
|
|
56
56
|
Monetize.assume_from_symbol = false
|
57
57
|
end
|
58
58
|
|
59
|
-
Monetize::CURRENCY_SYMBOLS.each_pair do |symbol, iso_code|
|
59
|
+
Monetize::Parser::CURRENCY_SYMBOLS.each_pair do |symbol, iso_code|
|
60
60
|
context iso_code do
|
61
61
|
let(:currency) { Money::Currency.find(iso_code) }
|
62
62
|
let(:amount) { 5_95 }
|
@@ -430,6 +430,12 @@ describe Monetize do
|
|
430
430
|
expect(Monetize.from_bigdecimal(BigDecimal.new('1'), 'JPY')).to eq Money.new(1, 'JPY')
|
431
431
|
end
|
432
432
|
|
433
|
+
it 'respects rounding mode when rounding amount to the nearest cent' do
|
434
|
+
amount = BigDecimal.new('1.005')
|
435
|
+
|
436
|
+
expect(Monetize.from_bigdecimal(amount, 'USD')).to eq Money.from_amount(amount, 'USD')
|
437
|
+
end
|
438
|
+
|
433
439
|
it 'accepts a currency options' do
|
434
440
|
m = Monetize.from_bigdecimal(BigDecimal.new('1'))
|
435
441
|
expect(m.currency).to eq Money.default_currency
|
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.8.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:
|
11
|
+
date: 2018-05-03 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:
|
19
|
+
version: 6.11.2
|
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:
|
26
|
+
version: 6.11.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -83,8 +83,8 @@ files:
|
|
83
83
|
- LICENSE.txt
|
84
84
|
- README.md
|
85
85
|
- Rakefile
|
86
|
-
- lib/collection.rb
|
87
86
|
- lib/monetize.rb
|
87
|
+
- lib/monetize/collection.rb
|
88
88
|
- lib/monetize/core_extensions.rb
|
89
89
|
- lib/monetize/core_extensions/hash.rb
|
90
90
|
- lib/monetize/core_extensions/nil_class.rb
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/monetize/core_extensions/string.rb
|
93
93
|
- lib/monetize/core_extensions/symbol.rb
|
94
94
|
- lib/monetize/errors.rb
|
95
|
+
- lib/monetize/parser.rb
|
95
96
|
- lib/monetize/version.rb
|
96
97
|
- monetize.gemspec
|
97
98
|
- spec/core_extensions_spec.rb
|