danconia 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +52 -9
- data/danconia.gemspec +4 -3
- data/examples/currency_layer.rb +7 -2
- data/examples/single_currency.rb +14 -0
- data/lib/danconia/config.rb +2 -1
- data/lib/danconia/currency.rb +1 -1
- data/lib/danconia/exchanges/exchange.rb +2 -3
- data/lib/danconia/test_helpers.rb +1 -1
- data/lib/danconia/version.rb +1 -1
- data/spec/danconia/money_spec.rb +2 -2
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ebf1db97ca89c6a822b12279f873193c97d7e9f412aa0054967cf02e1d3f74d
|
4
|
+
data.tar.gz: da1e807e1d5e0c4ec0619f8d93a7a023c4e8361c4b48a3f16b138c4013122fed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1836de03b18616b074b76028d6ff6cf392e8148e294d80783a918836c9d75eb78aa2f6df666302a5d68435362f90091f3690daa7ca1c08bf1fa746bd4b0171ab
|
7
|
+
data.tar.gz: bc00add76fb57dd0b30ddc8c0a2c4ba5790335bc4f4ad5dd4e29c73b2445fe5b3cec06edd8df2dd5eec4b9abcadc158196f2cb8a146807dcb43a6952b67f5c68
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,26 +1,69 @@
|
|
1
1
|
# Danconia
|
2
2
|
|
3
|
-
|
3
|
+
A very simple money library for Ruby, backed by BigDecimal (no conversion to cents, i.e. "infinite precision") with support for external exchange rates services.
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/eeng/danconia.svg?branch=master)](https://travis-ci.org/eeng/danconia)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
|
9
|
+
```ruby
|
10
|
+
gem 'danconia'
|
11
|
+
```
|
10
12
|
|
11
|
-
|
13
|
+
## Basic Usage
|
12
14
|
|
13
|
-
|
15
|
+
If you only need to work with a single currency:
|
14
16
|
|
15
|
-
|
17
|
+
```ruby
|
18
|
+
# USD by default, but can be configured
|
19
|
+
m1 = Money(10.25) # => 10.25 USD
|
16
20
|
|
17
|
-
|
21
|
+
# Note that we keep all decimal places
|
22
|
+
m2 = m1 / 2 # => 5.125 USD
|
18
23
|
|
19
|
-
|
24
|
+
# Simple formatting by default
|
25
|
+
puts m2 # => $5.13
|
26
|
+
```
|
20
27
|
|
21
|
-
|
28
|
+
Please refer to `examples/single_currency.rb` for some configuration options.
|
22
29
|
|
23
|
-
|
30
|
+
## Multi-Currency Support
|
31
|
+
|
32
|
+
To handle multiple currencies you need to configure an `Exchange` in order to fetch the rates. For example, with [CurrencyLayer](https://currencylayer.com/):
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# This can be placed in a Rails initializer
|
36
|
+
Danconia.configure do |config|
|
37
|
+
config.default_exchange = Danconia::Exchanges::CurrencyLayer.new(access_key: '...')
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Then, download the exchange rates:
|
42
|
+
```ruby
|
43
|
+
# You should do this periodically to keep rates up to date
|
44
|
+
Danconia.config.default_exchange.update_rates!
|
45
|
+
```
|
46
|
+
|
47
|
+
And finally to convert between currencies:
|
48
|
+
```ruby
|
49
|
+
Money(9, 'JPY').exchange_to('ARS') # => 2.272401 ARS
|
50
|
+
```
|
51
|
+
|
52
|
+
By default, rates are stored in memory, but you can supply a store in the exchange constructor to save them elsewhere. Please refer to `examples/currency_layer.rb` for an ActiveRecord example.
|
53
|
+
|
54
|
+
## Active Record Integration
|
55
|
+
|
56
|
+
Given a `products` table with a decimal column `price` and a string column `price_currency` (optional), then you can use the `money` class method to automatically convert it to Money:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class Product < ActiveRecord::Base
|
60
|
+
money :price
|
61
|
+
end
|
62
|
+
|
63
|
+
Product.new(price: 30, price_currency: 'ARS').price # => 30 ARS
|
64
|
+
```
|
65
|
+
|
66
|
+
Currently, there is no option to customize the names of the columns but should be fairly simple to implement if needed.
|
24
67
|
|
25
68
|
## Contributing
|
26
69
|
|
data/danconia.gemspec
CHANGED
@@ -6,11 +6,12 @@ require 'danconia/version'
|
|
6
6
|
Gem::Specification.new do |gem|
|
7
7
|
gem.name = "danconia"
|
8
8
|
gem.version = Danconia::VERSION
|
9
|
-
gem.
|
10
|
-
gem.email =
|
9
|
+
gem.author = "Emmanuel Nicolau"
|
10
|
+
gem.email = "emmanicolau@gmail.com"
|
11
11
|
gem.description = %q{Multi-currency money library backed by BigDecimal}
|
12
12
|
gem.summary = %q{Multi-currency money library backed by BigDecimal}
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/eeng/danconia"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/examples/currency_layer.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# Remember to supply your CurrencyLayer key in the ACCESS_KEY environment variable to run this example
|
1
2
|
require 'danconia'
|
2
3
|
|
3
4
|
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
4
5
|
|
5
6
|
ActiveRecord::Schema.define do
|
7
|
+
# You can use this in a Rails migration
|
6
8
|
create_table :exchange_rates do |t|
|
7
9
|
t.string :pair, limit: 6
|
8
10
|
t.decimal :rate, precision: 12, scale: 6
|
@@ -11,10 +13,13 @@ ActiveRecord::Schema.define do
|
|
11
13
|
end
|
12
14
|
|
13
15
|
Danconia.configure do |config|
|
14
|
-
config.default_exchange = Danconia::Exchanges::CurrencyLayer.new(
|
16
|
+
config.default_exchange = Danconia::Exchanges::CurrencyLayer.new(
|
17
|
+
access_key: ENV['ACCESS_KEY'],
|
18
|
+
store: Danconia::Stores::ActiveRecord.new
|
19
|
+
)
|
15
20
|
end
|
16
21
|
|
17
|
-
# Periodically
|
22
|
+
# Periodically call this method to keep rates up to date
|
18
23
|
puts 'Updating dates with CurrencyLayer API...'
|
19
24
|
Danconia.config.default_exchange.update_rates!
|
20
25
|
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'danconia'
|
2
|
+
|
3
|
+
# USD is the default currency if no configuration is provided
|
4
|
+
puts (Money(10.25) / 2).inspect # => 5.125 USD
|
5
|
+
puts (Money(10.25) / 2).to_s # => $5.13
|
6
|
+
|
7
|
+
# Lets switch to other currency
|
8
|
+
Danconia.configure do |config|
|
9
|
+
config.default_currency = 'EUR'
|
10
|
+
config.available_currencies = [{code: 'EUR', symbol: '€'}]
|
11
|
+
end
|
12
|
+
|
13
|
+
puts Money(10.25).inspect # => 10.25 EUR
|
14
|
+
puts Money(10.25).to_s # => €10.25
|
data/lib/danconia/config.rb
CHANGED
@@ -14,11 +14,12 @@ module Danconia
|
|
14
14
|
end
|
15
15
|
|
16
16
|
class Config
|
17
|
-
attr_accessor :default_currency, :default_exchange
|
17
|
+
attr_accessor :default_currency, :default_exchange, :available_currencies
|
18
18
|
|
19
19
|
def initialize
|
20
20
|
@default_currency = 'USD'
|
21
21
|
@default_exchange = Exchanges::FixedRates.new
|
22
|
+
@available_currencies = []
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
data/lib/danconia/currency.rb
CHANGED
@@ -2,7 +2,7 @@ module Danconia
|
|
2
2
|
class Currency < Struct.new(:code, :symbol, :description, keyword_init: true)
|
3
3
|
def self.find code, exchange
|
4
4
|
return code if code.is_a? Currency
|
5
|
-
new
|
5
|
+
new Danconia.config.available_currencies.find { |c| c[:code] == code } || {code: code, symbol: '$'}
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
@@ -1,11 +1,10 @@
|
|
1
1
|
module Danconia
|
2
2
|
module Exchanges
|
3
3
|
class Exchange
|
4
|
-
attr_reader :store
|
4
|
+
attr_reader :store
|
5
5
|
|
6
|
-
def initialize store: Stores::InMemory.new
|
6
|
+
def initialize store: Stores::InMemory.new
|
7
7
|
@store = store
|
8
|
-
@currencies = currencies
|
9
8
|
end
|
10
9
|
|
11
10
|
def rate from, to
|
@@ -9,7 +9,7 @@ module Danconia
|
|
9
9
|
|
10
10
|
def with_rates rates
|
11
11
|
with_config do |config|
|
12
|
-
config.default_exchange = Exchanges::FixedRates.new rates: rates
|
12
|
+
config.default_exchange = Exchanges::FixedRates.new rates: rates
|
13
13
|
yield
|
14
14
|
end
|
15
15
|
end
|
data/lib/danconia/version.rb
CHANGED
data/spec/danconia/money_spec.rb
CHANGED
@@ -96,7 +96,7 @@ module Danconia
|
|
96
96
|
expect(Money(3.25).to_s).to eq '$3.25'
|
97
97
|
|
98
98
|
TestHelpers.with_config do |config|
|
99
|
-
config.
|
99
|
+
config.available_currencies = [{code: 'EUR', symbol: '€'}, {code: 'JPY', symbol: '¥'}]
|
100
100
|
|
101
101
|
expect(Money(1, 'EUR').to_s).to eq '€1.00'
|
102
102
|
expect(Money(1, 'JPY').to_s).to eq '¥1.00'
|
@@ -168,7 +168,7 @@ module Danconia
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def fake_exchange args = {}
|
171
|
-
double 'exchange', args.reverse_merge(rate: nil
|
171
|
+
double 'exchange', args.reverse_merge(rate: nil)
|
172
172
|
end
|
173
173
|
end
|
174
174
|
end
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emmanuel Nicolau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -109,8 +109,7 @@ dependencies:
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
description: Multi-currency money library backed by BigDecimal
|
112
|
-
email:
|
113
|
-
- emmanicolau@gmail.com
|
112
|
+
email: emmanicolau@gmail.com
|
114
113
|
executables: []
|
115
114
|
extensions: []
|
116
115
|
extra_rdoc_files: []
|
@@ -128,6 +127,7 @@ files:
|
|
128
127
|
- danconia.gemspec
|
129
128
|
- examples/currency_layer.rb
|
130
129
|
- examples/fixed_rates.rb
|
130
|
+
- examples/single_currency.rb
|
131
131
|
- lib/danconia.rb
|
132
132
|
- lib/danconia/config.rb
|
133
133
|
- lib/danconia/currency.rb
|
@@ -150,8 +150,9 @@ files:
|
|
150
150
|
- spec/danconia/stores/active_record_spec.rb
|
151
151
|
- spec/spec_helper.rb
|
152
152
|
- spec/support/database.rb
|
153
|
-
homepage:
|
154
|
-
licenses:
|
153
|
+
homepage: https://github.com/eeng/danconia
|
154
|
+
licenses:
|
155
|
+
- MIT
|
155
156
|
metadata: {}
|
156
157
|
post_install_message:
|
157
158
|
rdoc_options: []
|