monetize 1.9.3 → 1.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +34 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +0 -2
- data/LICENSE +21 -0
- data/README.md +0 -1
- data/lib/monetize/core_extensions/hash.rb +11 -2
- data/lib/monetize/parser.rb +20 -8
- data/lib/monetize/version.rb +1 -1
- data/spec/core_extensions_spec.rb +10 -2
- data/spec/monetize_spec.rb +36 -0
- data/spec/spec_helper.rb +0 -2
- metadata +8 -9
- data/.coveralls.yml +0 -1
- data/.travis.yml +0 -21
- data/LICENSE.txt +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fec107ece48dc97d0e602c4176dc4bee8e9dd7be4172c0a507df4efa941ddc12
|
4
|
+
data.tar.gz: d15c4dd9bfc270948d69e6ffb9483baa9cde97743111d0fb68b3f6f1db8d6c9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bae292db1e6349b333248bab902017130ee85741e0321e8429216c83ca17c79003f36355eaced3e21fa8e9969df5a29f8fe538fe79ecef7f951b882f8cafe10
|
7
|
+
data.tar.gz: f5cc392537c1d8487c3117c680f9e49d0d38ec0db67fcda73594a4a7ce0dfddd8224338eaf5e97da880a86a5e31b4181fb287b9dd8b079f47ec104fd0edbfd49
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ main ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ main ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
23
|
+
|
24
|
+
steps:
|
25
|
+
- uses: actions/checkout@v2
|
26
|
+
- name: Set up Ruby
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
+
uses: ruby/setup-ruby@v1
|
30
|
+
with:
|
31
|
+
ruby-version: ${{ matrix.ruby-version }}
|
32
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
33
|
+
- name: Run tests
|
34
|
+
run: bundle exec rspec spec
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.12.0
|
4
|
+
- Update `to_money` to match money-rails to_hash.
|
5
|
+
|
6
|
+
## 1.11.0
|
7
|
+
- When parsing a string assume a single separator to be a decimal mark when number starts with 0
|
8
|
+
|
9
|
+
## 1.10.0
|
10
|
+
- When using the `assume_from_symbol` option, the currency in the input string will be used over the assumed currency based on symbol. For example, `$1.05 CAD` will use `CAD` instead of `USD`.
|
11
|
+
|
12
|
+
## 1.9.4
|
13
|
+
- Fix symbol parsing that are surrounded by other characters
|
14
|
+
|
3
15
|
## 1.9.3
|
4
16
|
- Fix input parsing when currency symbol after the amount
|
5
17
|
|
data/Gemfile
CHANGED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Shane Emmons
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -3,7 +3,6 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/monetize.svg)](http://badge.fury.io/rb/monetize)
|
4
4
|
[![Build Status](https://travis-ci.org/RubyMoney/monetize.svg?branch=master)](https://travis-ci.org/RubyMoney/monetize)
|
5
5
|
[![Code Climate](https://codeclimate.com/github/RubyMoney/monetize.svg)](https://codeclimate.com/github/RubyMoney/monetize)
|
6
|
-
[![Coverage Status](https://coveralls.io/repos/RubyMoney/monetize/badge.svg)](https://coveralls.io/r/RubyMoney/monetize)
|
7
6
|
[![Dependency Status](https://gemnasium.com/RubyMoney/monetize.svg)](https://gemnasium.com/RubyMoney/monetize)
|
8
7
|
[![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT)
|
9
8
|
|
@@ -2,7 +2,16 @@
|
|
2
2
|
|
3
3
|
class Hash
|
4
4
|
def to_money(currency = nil)
|
5
|
-
|
6
|
-
|
5
|
+
money_hash = self.respond_to?(:with_indifferent_access) ? self.with_indifferent_access : self
|
6
|
+
|
7
|
+
hash_currency = if money_hash[:currency].is_a?(Hash)
|
8
|
+
money_hash[:currency][:iso_code]
|
9
|
+
elsif money_hash[:currency_iso] && !money_hash[:currency_iso].empty?
|
10
|
+
money_hash[:currency_iso]
|
11
|
+
else
|
12
|
+
money_hash[:currency]
|
13
|
+
end
|
14
|
+
|
15
|
+
Money.new(money_hash[:cents] || money_hash[:fractional], hash_currency || currency || Money.default_currency)
|
7
16
|
end
|
8
17
|
end
|
data/lib/monetize/parser.rb
CHANGED
@@ -50,7 +50,7 @@ module Monetize
|
|
50
50
|
|
51
51
|
major, minor = extract_major_minor(num, currency)
|
52
52
|
|
53
|
-
amount =
|
53
|
+
amount = to_big_decimal([major, minor].join(DEFAULT_DECIMAL_MARK))
|
54
54
|
amount = apply_multiplier(multiplier_exp, amount)
|
55
55
|
amount = apply_sign(negative, amount)
|
56
56
|
|
@@ -59,12 +59,19 @@ module Monetize
|
|
59
59
|
|
60
60
|
private
|
61
61
|
|
62
|
+
def to_big_decimal(value)
|
63
|
+
BigDecimal(value)
|
64
|
+
rescue ::ArgumentError => err
|
65
|
+
fail ParseError, err.message
|
66
|
+
end
|
67
|
+
|
62
68
|
attr_reader :input, :fallback_currency, :options
|
63
69
|
|
64
70
|
def parse_currency
|
65
71
|
computed_currency = nil
|
66
|
-
computed_currency =
|
67
|
-
computed_currency ||=
|
72
|
+
computed_currency = input[/[A-Z]{2,3}/]
|
73
|
+
computed_currency ||= compute_currency if assume_from_symbol?
|
74
|
+
|
68
75
|
|
69
76
|
computed_currency || fallback_currency || Money.default_currency
|
70
77
|
end
|
@@ -82,8 +89,8 @@ module Monetize
|
|
82
89
|
end
|
83
90
|
|
84
91
|
def compute_currency
|
85
|
-
|
86
|
-
CURRENCY_SYMBOLS[
|
92
|
+
match = input.match(currency_symbol_regex)
|
93
|
+
CURRENCY_SYMBOLS[match.to_s] if match
|
87
94
|
end
|
88
95
|
|
89
96
|
def extract_major_minor(num, currency)
|
@@ -119,8 +126,13 @@ module Monetize
|
|
119
126
|
else
|
120
127
|
possible_major, possible_minor = split_major_minor(num, delimiter)
|
121
128
|
|
122
|
-
|
123
|
-
|
129
|
+
# Doesn't look like thousands separator
|
130
|
+
is_decimal_mark = possible_minor.length != 3 ||
|
131
|
+
possible_major.length > 3 ||
|
132
|
+
possible_major.to_i == 0 ||
|
133
|
+
delimiter == '.'
|
134
|
+
|
135
|
+
if is_decimal_mark
|
124
136
|
[possible_major, possible_minor]
|
125
137
|
else
|
126
138
|
["#{possible_major}#{possible_minor}", '00']
|
@@ -153,7 +165,7 @@ module Monetize
|
|
153
165
|
end
|
154
166
|
|
155
167
|
def currency_symbol_regex
|
156
|
-
/(
|
168
|
+
/(?<![A-Z])(#{regex_safe_symbols})(?![A-Z])/i
|
157
169
|
end
|
158
170
|
end
|
159
171
|
end
|
data/lib/monetize/version.rb
CHANGED
@@ -7,7 +7,7 @@ require 'monetize/core_extensions'
|
|
7
7
|
describe Monetize, 'core extensions' do
|
8
8
|
describe NilClass do
|
9
9
|
describe '#to_money' do
|
10
|
-
it '
|
10
|
+
it 'works as documented' do
|
11
11
|
money = nil.to_money
|
12
12
|
expect(money.cents).to eq 0
|
13
13
|
expect(money.currency).to eq Money.default_currency
|
@@ -22,7 +22,7 @@ describe Monetize, 'core extensions' do
|
|
22
22
|
|
23
23
|
describe Numeric do
|
24
24
|
describe '#to_money' do
|
25
|
-
it '
|
25
|
+
it 'works as documented' do
|
26
26
|
money = 1234.to_money
|
27
27
|
expect(money.cents).to eq 1_234_00
|
28
28
|
expect(money.currency).to eq Money.default_currency
|
@@ -230,6 +230,14 @@ describe Monetize, 'core extensions' do
|
|
230
230
|
expect(hash.to_money).to eq(Money.new(100, 'USD'))
|
231
231
|
end
|
232
232
|
end
|
233
|
+
|
234
|
+
context 'when Money to_hash is used' do
|
235
|
+
subject(:hash) { { cents: 100, currency_iso: 'SGD' } }
|
236
|
+
|
237
|
+
it 'converts Hash to Money, interpreting fractional as cents' do
|
238
|
+
expect(hash.to_money).to eq(Money.new(100, 'SGD'))
|
239
|
+
end
|
240
|
+
end
|
233
241
|
end
|
234
242
|
end
|
235
243
|
|
data/spec/monetize_spec.rb
CHANGED
@@ -118,6 +118,15 @@ describe Monetize do
|
|
118
118
|
expect(Monetize.parse('L9.99')).to eq Money.new(999, 'USD')
|
119
119
|
end
|
120
120
|
|
121
|
+
it 'should use provided currency over symbol' do
|
122
|
+
expect(Monetize.parse('$1.05 CAD')).to eq Money.new(105, 'CAD')
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'ignores ZAR symbols that is part of a text' do
|
126
|
+
expect(Monetize.parse('EUR 9.99')).to eq Money.new(999, 'EUR')
|
127
|
+
expect(Monetize.parse('9.99 EUR')).to eq Money.new(999, 'EUR')
|
128
|
+
end
|
129
|
+
|
121
130
|
context 'negatives' do
|
122
131
|
it 'ignores the ambiguous kr symbol' do
|
123
132
|
# Could mean either of DKK, EEK, ISK, NOK, SEK
|
@@ -311,6 +320,29 @@ describe Monetize do
|
|
311
320
|
expect('€8.883.331,0034'.to_money('EU4')).to eq Money.new(8_883_331_0034, 'EU4')
|
312
321
|
# rubocop:enable Style/NumericLiterals
|
313
322
|
end
|
323
|
+
|
324
|
+
it 'parses strings deducing a decimal mark when a single delimiter is used and major is 0' do
|
325
|
+
expect('$0,4'.to_money('BAR')).to eq Money.new(4000, 'BAR')
|
326
|
+
expect('€0.4'.to_money('EU4')).to eq Money.new(4000, 'EU4')
|
327
|
+
|
328
|
+
expect('$0,04'.to_money('BAR')).to eq Money.new(400, 'BAR')
|
329
|
+
expect('€0.04'.to_money('EU4')).to eq Money.new(400, 'EU4')
|
330
|
+
|
331
|
+
expect('$0,004'.to_money('BAR')).to eq Money.new(40, 'BAR')
|
332
|
+
expect('€0.004'.to_money('EU4')).to eq Money.new(40, 'EU4')
|
333
|
+
|
334
|
+
expect('$0,0004'.to_money('BAR')).to eq Money.new(4, 'BAR')
|
335
|
+
expect('€0.0004'.to_money('EU4')).to eq Money.new(4, 'EU4')
|
336
|
+
|
337
|
+
expect('$0,0024'.to_money('BAR')).to eq Money.new(24, 'BAR')
|
338
|
+
expect('€0.0024'.to_money('EU4')).to eq Money.new(24, 'EU4')
|
339
|
+
|
340
|
+
expect('$0,0324'.to_money('BAR')).to eq Money.new(324, 'BAR')
|
341
|
+
expect('€0.0324'.to_money('EU4')).to eq Money.new(324, 'EU4')
|
342
|
+
|
343
|
+
expect('$0,5324'.to_money('BAR')).to eq Money.new(5324, 'BAR')
|
344
|
+
expect('€0.5324'.to_money('EU4')).to eq Money.new(5324, 'EU4')
|
345
|
+
end
|
314
346
|
end
|
315
347
|
end
|
316
348
|
|
@@ -324,6 +356,10 @@ describe Monetize do
|
|
324
356
|
it 'raises ArgumentError when unable to detect polarity' do
|
325
357
|
expect { Monetize.parse!('-$5.95-') }.to raise_error Monetize::ParseError
|
326
358
|
end
|
359
|
+
|
360
|
+
it 'raises ArgumentError with invalid format' do
|
361
|
+
expect { Monetize.parse!('11..0') }.to raise_error Monetize::ParseError
|
362
|
+
end
|
327
363
|
end
|
328
364
|
|
329
365
|
describe '.parse_collection' do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: monetize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Emmons
|
8
8
|
- Anthony Dmitriyev
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-01-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: money
|
@@ -75,14 +75,13 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
-
- ".
|
78
|
+
- ".github/workflows/ruby.yml"
|
79
79
|
- ".gitignore"
|
80
80
|
- ".rubocop.yml"
|
81
|
-
- ".travis.yml"
|
82
81
|
- CHANGELOG.md
|
83
82
|
- CONTRIBUTING.md
|
84
83
|
- Gemfile
|
85
|
-
- LICENSE
|
84
|
+
- LICENSE
|
86
85
|
- README.md
|
87
86
|
- Rakefile
|
88
87
|
- lib/monetize.rb
|
@@ -107,7 +106,7 @@ metadata:
|
|
107
106
|
changelog_uri: https://github.com/RubyMoney/monetize/blob/master/CHANGELOG.md
|
108
107
|
source_code_uri: https://github.com/RubyMoney/monetize/
|
109
108
|
bug_tracker_uri: https://github.com/RubyMoney/monetize/issues
|
110
|
-
post_install_message:
|
109
|
+
post_install_message:
|
111
110
|
rdoc_options: []
|
112
111
|
require_paths:
|
113
112
|
- lib
|
@@ -122,8 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
121
|
- !ruby/object:Gem::Version
|
123
122
|
version: '0'
|
124
123
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
126
|
-
signing_key:
|
124
|
+
rubygems_version: 3.2.22
|
125
|
+
signing_key:
|
127
126
|
specification_version: 4
|
128
127
|
summary: A library for converting various objects into `Money` objects.
|
129
128
|
test_files:
|
data/.coveralls.yml
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
repo_token: pbKErt5rAW1iRYGMIAGGAAHfVRmiBzxfd
|
data/.travis.yml
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
sudo: false
|
2
|
-
language:
|
3
|
-
- ruby
|
4
|
-
- java
|
5
|
-
rvm:
|
6
|
-
- 1.9.3
|
7
|
-
- 2.0.0
|
8
|
-
- 2.1.10
|
9
|
-
- 2.2.10
|
10
|
-
- 2.3.8
|
11
|
-
- 2.4.6
|
12
|
-
- 2.5.5
|
13
|
-
- 2.6.3
|
14
|
-
- jruby-9.0.5.0
|
15
|
-
jdk: openjdk8
|
16
|
-
before_install:
|
17
|
-
- gem install bundler --version '~> 1.17'
|
18
|
-
script: bundle exec rspec spec
|
19
|
-
notifications:
|
20
|
-
recipients:
|
21
|
-
- shane@emmons.io
|
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2013 Shane Emmons
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|