active_currency 0.1.0 → 0.2.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/README.md +25 -5
- data/app/models/active_currency/rate.rb +13 -3
- data/db/migrate/20180911202100_create_active_currency_rates.rb +1 -1
- data/db/migrate/20180914064834_add_rates.rb +1 -1
- data/lib/active_currency/database_store.rb +1 -1
- data/lib/active_currency/memory_rate_store.rb +20 -0
- data/lib/active_currency/migration.rb +10 -0
- data/lib/active_currency/version.rb +1 -1
- data/lib/active_currency.rb +2 -0
- metadata +9 -9
- data/Rakefile +0 -37
- data/app/models/active_currency/application_record.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75b316711129b3a675bad41c3531bd43f3921323
|
4
|
+
data.tar.gz: 00e65292dbe3783ab0d760c54fc4932a8bde18a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '043295dcad9a8bc822bc29a30cc9cdc1348fc3f1fb9a21b244a55bccb7b2efda4ec66c18ab5afeeb10dec13f70e29f6f1347095e4fb9c1e6133c996ec615fb8e'
|
7
|
+
data.tar.gz: d74ea8f17afe55780d610918895efe3c8b59d6abab49c75c16b292f7635f9cab2d5bc8e369ece2c53f35c2537e201468d33e46f864bb61e525147b53f6bff41d
|
data/README.md
CHANGED
@@ -18,6 +18,8 @@ provides the following advantages:
|
|
18
18
|
- When fetching the current rate, it uses your application cache in order not
|
19
19
|
to have to do a database query.
|
20
20
|
|
21
|
+
To fetch the rates, it uses the [eu_central_bank] gem.
|
22
|
+
|
21
23
|
## Usage
|
22
24
|
|
23
25
|
Store the current rate regularly by calling in a scheduled job (using something
|
@@ -36,10 +38,10 @@ You can then exchange money by using the Money gem:
|
|
36
38
|
Or look up the currency rate:
|
37
39
|
|
38
40
|
```rb
|
39
|
-
ActiveCurrency::Rate.
|
41
|
+
ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago)
|
40
42
|
# => 1.151
|
41
43
|
ActiveCurrency::Rate.where(from: 'EUR', to: 'USD').pluck(:value)
|
42
|
-
# => [1.162, 1.162, 1.161, 1.
|
44
|
+
# => [1.162, 1.162, 1.161, 1.161, 1.163, …]
|
43
45
|
```
|
44
46
|
|
45
47
|
## Installation
|
@@ -48,8 +50,7 @@ Add these lines to your application's `Gemfile`:
|
|
48
50
|
|
49
51
|
```rb
|
50
52
|
# Store and retrieve the currency from the database.
|
51
|
-
gem 'active_currency'
|
52
|
-
git: 'git@github.com:sunny/active_currency.git'
|
53
|
+
gem 'active_currency'
|
53
54
|
```
|
54
55
|
|
55
56
|
And in `config/initializers/money.rb`:
|
@@ -64,6 +65,23 @@ end
|
|
64
65
|
Then call `bundle exec rake db:migrate` to create the table that holds
|
65
66
|
the currency rates and fill it for the first time.
|
66
67
|
|
68
|
+
## Tests
|
69
|
+
|
70
|
+
In your app test suite you may not want to have to fill your database to be
|
71
|
+
able to exchange currencies.
|
72
|
+
|
73
|
+
For that, you can in `config/initializers/money.rb`:
|
74
|
+
|
75
|
+
```rb
|
76
|
+
if Rails.env.test?
|
77
|
+
rate_store = ActiveCurrency::MemoryRateStore.new.tap do |store|
|
78
|
+
store.add_rate('USD', 'EUR', 0.5)
|
79
|
+
store.add_rate('EUR', 'USD', 1.5)
|
80
|
+
end
|
81
|
+
config.default_bank = Money::Bank::VariableExchange.new(rate_store)
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
67
85
|
## Contributing
|
68
86
|
|
69
87
|
Please file issues and pull requests
|
@@ -72,10 +90,12 @@ Please file issues and pull requests
|
|
72
90
|
In developemnt, launch specs and code linter by calling:
|
73
91
|
|
74
92
|
```sh
|
75
|
-
bundle exec rake
|
93
|
+
BUNDLE_GEMFILE=Gemfile-rails3.2 bundle exec rspec && bundle exec rake
|
76
94
|
```
|
77
95
|
|
78
96
|
## License
|
79
97
|
|
80
98
|
The gem is available as open source under the terms of the
|
81
99
|
[MIT License](http://opensource.org/licenses/MIT).
|
100
|
+
|
101
|
+
[eu_central_bank]: https://github.com/RubyMoney/eu_central_bank
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveCurrency
|
4
|
-
class Rate <
|
4
|
+
class Rate < ActiveRecord::Base
|
5
5
|
validates :from,
|
6
6
|
:to,
|
7
7
|
inclusion: { in: ->(_v) { Money.default_bank.store.currencies } },
|
@@ -10,8 +10,8 @@ module ActiveCurrency
|
|
10
10
|
|
11
11
|
scope :before, ->(date) { where(arel_table[:created_at].lt(date)) }
|
12
12
|
|
13
|
-
def self.
|
14
|
-
scope = date ? before(date) :
|
13
|
+
def self.value_for(from, to, date = nil)
|
14
|
+
scope = date ? before(date) : all_scope
|
15
15
|
|
16
16
|
scope
|
17
17
|
.where(from: from, to: to)
|
@@ -19,5 +19,15 @@ module ActiveCurrency
|
|
19
19
|
.last
|
20
20
|
&.value
|
21
21
|
end
|
22
|
+
|
23
|
+
# DEPRECATED
|
24
|
+
def self.current_value_for(from, to, date = nil)
|
25
|
+
value_for(from, to, date)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Scope retrocompatibility for Rails 3.2.
|
29
|
+
def self.all_scope
|
30
|
+
respond_to?(:scoped) ? scoped : all
|
31
|
+
end
|
22
32
|
end
|
23
33
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveCurrency
|
4
|
+
class MemoryRateStore < Money::RatesStore::Memory
|
5
|
+
def initialize(**)
|
6
|
+
@currencies = Set.new
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def currencies
|
11
|
+
@currencies.to_a
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_rate(from, to, value)
|
15
|
+
@currencies << from
|
16
|
+
@currencies << to
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveCurrency
|
4
|
+
# Helps support previous version of Rails in migrations.
|
5
|
+
if Rails.version > '4.1'
|
6
|
+
class Migration < ActiveRecord::Migration[4.2]; end
|
7
|
+
else
|
8
|
+
class Migration < ActiveRecord::Migration; end
|
9
|
+
end
|
10
|
+
end
|
data/lib/active_currency.rb
CHANGED
@@ -3,10 +3,12 @@
|
|
3
3
|
require 'money-rails'
|
4
4
|
require 'eu_central_bank'
|
5
5
|
|
6
|
+
require 'active_currency/migration'
|
6
7
|
require 'active_currency/engine'
|
7
8
|
require 'active_currency/database_store'
|
8
9
|
require 'active_currency/cacheable_store'
|
9
10
|
require 'active_currency/rate_store'
|
11
|
+
require 'active_currency/memory_rate_store'
|
10
12
|
require 'active_currency/add_rates'
|
11
13
|
|
12
14
|
module ActiveCurrency
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_currency
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sunny Ripert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.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: '3.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: money-rails
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,8 +131,6 @@ extra_rdoc_files: []
|
|
131
131
|
files:
|
132
132
|
- MIT-LICENSE
|
133
133
|
- README.md
|
134
|
-
- Rakefile
|
135
|
-
- app/models/active_currency/application_record.rb
|
136
134
|
- app/models/active_currency/rate.rb
|
137
135
|
- db/migrate/20180911202100_create_active_currency_rates.rb
|
138
136
|
- db/migrate/20180914064834_add_rates.rb
|
@@ -141,6 +139,8 @@ files:
|
|
141
139
|
- lib/active_currency/cacheable_store.rb
|
142
140
|
- lib/active_currency/database_store.rb
|
143
141
|
- lib/active_currency/engine.rb
|
142
|
+
- lib/active_currency/memory_rate_store.rb
|
143
|
+
- lib/active_currency/migration.rb
|
144
144
|
- lib/active_currency/rate_store.rb
|
145
145
|
- lib/active_currency/version.rb
|
146
146
|
homepage: https://github.com/sunny/active_currency
|
@@ -163,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
163
|
version: '0'
|
164
164
|
requirements: []
|
165
165
|
rubyforge_project:
|
166
|
-
rubygems_version: 2.
|
166
|
+
rubygems_version: 2.5.2.3
|
167
167
|
signing_key:
|
168
168
|
specification_version: 4
|
169
169
|
summary: Rails plugin to store your currency regularly
|
data/Rakefile
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
begin
|
4
|
-
require 'bundler/setup'
|
5
|
-
rescue LoadError
|
6
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
-
end
|
8
|
-
|
9
|
-
require 'rdoc/task'
|
10
|
-
|
11
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
-
rdoc.rdoc_dir = 'rdoc'
|
13
|
-
rdoc.title = 'ActiveCurrency'
|
14
|
-
rdoc.options << '--line-numbers'
|
15
|
-
rdoc.rdoc_files.include('README.md')
|
16
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
-
end
|
18
|
-
|
19
|
-
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
|
20
|
-
load 'rails/tasks/engine.rake'
|
21
|
-
|
22
|
-
load 'rails/tasks/statistics.rake'
|
23
|
-
|
24
|
-
require 'bundler/gem_tasks'
|
25
|
-
|
26
|
-
begin
|
27
|
-
require 'rspec/core/rake_task'
|
28
|
-
RSpec::Core::RakeTask.new(:spec)
|
29
|
-
rescue LoadError
|
30
|
-
puts 'RSpec load error'
|
31
|
-
end
|
32
|
-
|
33
|
-
require 'rubocop/rake_task'
|
34
|
-
|
35
|
-
RuboCop::RakeTask.new
|
36
|
-
|
37
|
-
task default: %i[spec rubocop]
|