danconia 0.2.3 → 0.2.8
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 -0
- data/Gemfile.lock +16 -16
- data/README.md +12 -0
- data/examples/currency_layer.rb +1 -1
- data/lib/danconia/currency.rb +1 -1
- data/lib/danconia/exchanges/exchange.rb +4 -2
- data/lib/danconia/money.rb +14 -17
- data/lib/danconia/stores/active_record.rb +1 -1
- data/lib/danconia/version.rb +1 -1
- data/spec/danconia/money_spec.rb +29 -7
- data/spec/danconia/stores/active_record_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc840f5913a010c41495c7fd843304d67565217acf7eb7c248be9bf4c17624f8
|
4
|
+
data.tar.gz: d8906df663f918a41d91525b6ac51de9f8ac6decef179c813fc30145be876c50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc66bbeaf202e6b288f79d4a6fb9b93cc66585771f9543c14a14980252f5a112eb4e6a2cad21d15e4132bda2400f74cdb5567eb9a47a4f7ac7b6161105c2ae9c
|
7
|
+
data.tar.gz: 222d602fb10b842955d921ede2c7bae8bba4fd56dcdb1bcbcc74c12f795959c58250141f05943b607f0de80cdb3a07dc7425219e38f534c375236a0029d29ce2
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,28 +1,27 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
danconia (0.2.
|
4
|
+
danconia (0.2.8)
|
5
5
|
activerecord (>= 3.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activemodel (
|
11
|
-
activesupport (=
|
12
|
-
activerecord (
|
13
|
-
activemodel (=
|
14
|
-
activesupport (=
|
15
|
-
|
16
|
-
activesupport (5.2.0)
|
10
|
+
activemodel (6.0.3.1)
|
11
|
+
activesupport (= 6.0.3.1)
|
12
|
+
activerecord (6.0.3.1)
|
13
|
+
activemodel (= 6.0.3.1)
|
14
|
+
activesupport (= 6.0.3.1)
|
15
|
+
activesupport (6.0.3.1)
|
17
16
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
17
|
i18n (>= 0.7, < 2)
|
19
18
|
minitest (~> 5.1)
|
20
19
|
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
21
21
|
addressable (2.5.2)
|
22
22
|
public_suffix (>= 2.0.2, < 4.0)
|
23
|
-
arel (9.0.0)
|
24
23
|
coderay (1.1.2)
|
25
|
-
concurrent-ruby (1.
|
24
|
+
concurrent-ruby (1.1.6)
|
26
25
|
crack (0.4.3)
|
27
26
|
safe_yaml (~> 1.0.0)
|
28
27
|
diff-lcs (1.3)
|
@@ -43,7 +42,7 @@ GEM
|
|
43
42
|
guard-compat (~> 1.1)
|
44
43
|
rspec (>= 2.99.0, < 4.0)
|
45
44
|
hashdiff (0.3.7)
|
46
|
-
i18n (1.
|
45
|
+
i18n (1.8.3)
|
47
46
|
concurrent-ruby (~> 1.0)
|
48
47
|
listen (3.1.5)
|
49
48
|
rb-fsevent (~> 0.9, >= 0.9.4)
|
@@ -51,7 +50,7 @@ GEM
|
|
51
50
|
ruby_dep (~> 1.2)
|
52
51
|
lumberjack (1.0.13)
|
53
52
|
method_source (0.9.0)
|
54
|
-
minitest (5.
|
53
|
+
minitest (5.14.1)
|
55
54
|
nenv (0.3.0)
|
56
55
|
notiffany (0.1.1)
|
57
56
|
nenv (~> 0.1)
|
@@ -60,7 +59,7 @@ GEM
|
|
60
59
|
coderay (~> 1.1.0)
|
61
60
|
method_source (~> 0.9.0)
|
62
61
|
public_suffix (3.0.2)
|
63
|
-
rake (
|
62
|
+
rake (13.0.1)
|
64
63
|
rb-fsevent (0.10.3)
|
65
64
|
rb-inotify (0.9.10)
|
66
65
|
ffi (>= 0.5.0, < 2)
|
@@ -80,15 +79,16 @@ GEM
|
|
80
79
|
ruby_dep (1.5.0)
|
81
80
|
safe_yaml (1.0.4)
|
82
81
|
shellany (0.0.1)
|
83
|
-
sqlite3 (1.
|
82
|
+
sqlite3 (1.4.1)
|
84
83
|
thor (0.20.0)
|
85
84
|
thread_safe (0.3.6)
|
86
|
-
tzinfo (1.2.
|
85
|
+
tzinfo (1.2.7)
|
87
86
|
thread_safe (~> 0.1)
|
88
87
|
webmock (3.4.2)
|
89
88
|
addressable (>= 2.3.6)
|
90
89
|
crack (>= 0.3.2)
|
91
90
|
hashdiff
|
91
|
+
zeitwerk (2.3.0)
|
92
92
|
|
93
93
|
PLATFORMS
|
94
94
|
ruby
|
@@ -103,4 +103,4 @@ DEPENDENCIES
|
|
103
103
|
webmock
|
104
104
|
|
105
105
|
BUNDLED WITH
|
106
|
-
1.
|
106
|
+
1.17.3
|
data/README.md
CHANGED
@@ -65,6 +65,18 @@ Product.new(price: 30, price_currency: 'ARS').price # => 30 ARS
|
|
65
65
|
|
66
66
|
Currently, there is no option to customize the names of the columns but should be fairly simple to implement if needed.
|
67
67
|
|
68
|
+
## Testing
|
69
|
+
|
70
|
+
There is a FixedRates exchange that can be used during testing to supply static rates (see `examples/fixed_rates.rb`). Another possibility is to use the following helper:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
require 'danconia/test_helpers'
|
74
|
+
|
75
|
+
Danconia::TestHelpers.with_rates 'USDARS' => 3 do
|
76
|
+
Money(2, 'USD').exchange_to('ARS') # => 6.0 ARS
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
68
80
|
## Contributing
|
69
81
|
|
70
82
|
1. Fork it
|
data/examples/currency_layer.rb
CHANGED
data/lib/danconia/currency.rb
CHANGED
@@ -8,7 +8,9 @@ module Danconia
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def rate from, to
|
11
|
-
if from ==
|
11
|
+
if from == to
|
12
|
+
1.0
|
13
|
+
elsif from == 'USD' and direct_rate = @store.direct_rate(from, to)
|
12
14
|
direct_rate
|
13
15
|
elsif to == 'USD' and inverse_rate = @store.direct_rate(to, from)
|
14
16
|
(1.0 / inverse_rate).round 6
|
@@ -28,4 +30,4 @@ module Danconia
|
|
28
30
|
end
|
29
31
|
end
|
30
32
|
end
|
31
|
-
end
|
33
|
+
end
|
data/lib/danconia/money.rb
CHANGED
@@ -13,13 +13,12 @@ module Danconia
|
|
13
13
|
@exchange = exchange
|
14
14
|
end
|
15
15
|
|
16
|
-
def format decimals: @decimals
|
17
|
-
|
16
|
+
def format decimals: @decimals, **other_options
|
17
|
+
opts = other_options.reverse_merge precision: decimals, unit: currency.symbol
|
18
|
+
ActiveSupport::NumberHelper.number_to_currency amount, opts
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
-
format
|
22
|
-
end
|
21
|
+
alias to_s format
|
23
22
|
|
24
23
|
def inspect
|
25
24
|
"#{amount} #{currency.code}"
|
@@ -46,23 +45,17 @@ module Danconia
|
|
46
45
|
amount <=> other
|
47
46
|
end
|
48
47
|
|
49
|
-
def exchange_to other_currency
|
48
|
+
def exchange_to other_currency, exchange: @exchange
|
50
49
|
other_currency = other_currency.presence && Currency.find(other_currency, exchange) || currency
|
51
|
-
rate =
|
52
|
-
clone_with amount * rate, other_currency
|
53
|
-
end
|
54
|
-
|
55
|
-
def exchange_rate_to to
|
56
|
-
from = currency.code
|
57
|
-
return 1 if from == to
|
58
|
-
exchange.rate from, to
|
50
|
+
rate = exchange.rate currency.code, other_currency.code
|
51
|
+
clone_with amount * rate, other_currency, exchange
|
59
52
|
end
|
60
53
|
|
61
54
|
%w(+ - * /).each do |op|
|
62
55
|
class_eval <<-EOR, __FILE__, __LINE__ + 1
|
63
56
|
def #{op} other
|
64
|
-
other = other.exchange_to(currency).amount if other.is_a? Money
|
65
|
-
clone_with
|
57
|
+
other = other.exchange_to(currency, exchange: @exchange).amount if other.is_a? Money
|
58
|
+
clone_with(amount #{op} other)
|
66
59
|
end
|
67
60
|
EOR
|
68
61
|
end
|
@@ -75,6 +68,10 @@ module Danconia
|
|
75
68
|
(self * 100).round
|
76
69
|
end
|
77
70
|
|
71
|
+
def as_json *args
|
72
|
+
amount.as_json *args
|
73
|
+
end
|
74
|
+
|
78
75
|
def default_currency?
|
79
76
|
currency.code == Danconia.config.default_currency
|
80
77
|
end
|
@@ -97,7 +94,7 @@ module Danconia
|
|
97
94
|
BigDecimal(object.to_s) rescue BigDecimal(0)
|
98
95
|
end
|
99
96
|
|
100
|
-
def clone_with amount, currency = @currency
|
97
|
+
def clone_with amount, currency = @currency, exchange = @exchange
|
101
98
|
Money.new amount, currency, decimals: decimals, exchange: exchange
|
102
99
|
end
|
103
100
|
end
|
data/lib/danconia/version.rb
CHANGED
data/spec/danconia/money_spec.rb
CHANGED
@@ -39,7 +39,7 @@ module Danconia
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'should exchange the other currency if it is different' do
|
42
|
-
expect(Money(1, 'ARS') + Money(1, 'USD'
|
42
|
+
expect(Money(1, 'ARS', exchange: fake_exchange(rate: 4)) + Money(1, 'USD')).to eq Money(5, 'ARS')
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'should return a new object with the same options' do
|
@@ -89,6 +89,10 @@ module Danconia
|
|
89
89
|
it 'allow to override the number of decimals' do
|
90
90
|
expect(Money(3.561, decimals: 3).format(decimals: 1)).to eq '$3.6'
|
91
91
|
end
|
92
|
+
|
93
|
+
it 'pass the options to the activesupport helper' do
|
94
|
+
expect(Money(2).format(format: '%n %u')).to eq '2.00 $'
|
95
|
+
end
|
92
96
|
end
|
93
97
|
|
94
98
|
context 'to_s' do
|
@@ -117,17 +121,29 @@ module Danconia
|
|
117
121
|
end
|
118
122
|
|
119
123
|
context 'exchange_to' do
|
120
|
-
it 'should use
|
121
|
-
expect(Money(2, 'USD', exchange: fake_exchange(rate: 3)).exchange_to('ARS')).to eq Money(6, 'ARS')
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'should use the default exchange if not set' do
|
124
|
+
it 'should use a default exchange if not overriden' do
|
125
125
|
TestHelpers.with_rates 'USDEUR' => 3, 'USDARS' => 4 do
|
126
126
|
expect(Money(2, 'USD').exchange_to('EUR')).to eq Money(6, 'EUR')
|
127
127
|
expect(Money(2, 'USD').exchange_to('ARS')).to eq Money(8, 'ARS')
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
+
it 'should allow to pass the exchange to the instance' do
|
132
|
+
expect(Money(2, 'USD', exchange: fake_exchange(rate: 3)).exchange_to('ARS')).to eq Money(6, 'ARS')
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should allow to pass the exchange when converting' do
|
136
|
+
expect(Money(2, 'USD').exchange_to('ARS', exchange: fake_exchange(rate: 4))).to eq Money(8, 'ARS')
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'when overriding the exchange, should preserve it in the new instances' do
|
140
|
+
m1 = Money(1, 'USD').exchange_to('ARS', exchange: fake_exchange(rate: 2))
|
141
|
+
m2 = m1 + Money(3, 'USD')
|
142
|
+
m3 = m2 * Money(1, 'USD')
|
143
|
+
expect(m2).to eq Money(8, 'ARS')
|
144
|
+
expect(m3).to eq Money(16, 'ARS')
|
145
|
+
end
|
146
|
+
|
131
147
|
it 'if no rate if found should raise error' do
|
132
148
|
expect { Money(2, 'USD').exchange_to('ARS') }.to raise_error Errors::ExchangeRateNotFound
|
133
149
|
end
|
@@ -167,8 +183,14 @@ module Danconia
|
|
167
183
|
end
|
168
184
|
end
|
169
185
|
|
186
|
+
context 'to_json' do
|
187
|
+
it 'should delegate to the amount' do
|
188
|
+
expect(Money(1).to_json).to eq '"1.0"'
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
170
192
|
def fake_exchange args = {}
|
171
|
-
double '
|
193
|
+
double 'Danconia::Exchanges::Exchange', args.reverse_merge(rate: nil)
|
172
194
|
end
|
173
195
|
end
|
174
196
|
end
|
@@ -7,7 +7,7 @@ module Danconia
|
|
7
7
|
it 'should create or update the rates' do
|
8
8
|
ExchangeRate.create! pair: 'USDEUR', rate: 2
|
9
9
|
expect { subject.save_rates 'USDEUR' => 3, 'USDARS' => 4 }.to change { ExchangeRate.count }.by 1
|
10
|
-
expect(subject.rates
|
10
|
+
expect(subject.rates).to eq('USDEUR' => 3, 'USDARS' => 4)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: danconia
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emmanuel Nicolau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|