active_currency 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '079217f3be4857bdc2856432a807341a94eea305'
4
- data.tar.gz: defcb92fec6e6124a0a5566c9f9c9040b7efe31a
3
+ metadata.gz: 75b316711129b3a675bad41c3531bd43f3921323
4
+ data.tar.gz: 00e65292dbe3783ab0d760c54fc4932a8bde18a2
5
5
  SHA512:
6
- metadata.gz: bddbb54d3fbbae7b918bcb2eb90e024e7413f5656fe887f46ee3577a3af2089d1a1993575aa126bf78f62ec504f36c78ccad684c6899b0a15dedc1c3ffa8d73a
7
- data.tar.gz: a0776af8effc2d92c9ff6df9a2e9679bf9cea1ae671002987202abe638c5a1b499a6d7817a6dcea534d16b33b2322efb8d0c1143559585f1c5c7e5964dae0309
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.current_value_for('EUR', 'USD', 10.days.ago)
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.61, 1.63, …]
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 < ApplicationRecord
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.current_value_for(from, to, date = nil)
14
- scope = date ? before(date) : all
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
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class CreateActiveCurrencyRates < ActiveRecord::Migration[5.0]
3
+ class CreateActiveCurrencyRates < ActiveCurrency::Migration
4
4
  def change
5
5
  create_table :active_currency_rates do |t|
6
6
  t.string :from
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class AddRates < ActiveRecord::Migration[5.0]
3
+ class AddRates < ActiveCurrency::Migration
4
4
  def up
5
5
  ActiveCurrency::AddRates.new.call
6
6
  end
@@ -5,7 +5,7 @@ module ActiveCurrency
5
5
  # value.
6
6
  class DatabaseStore
7
7
  def get_rate(from, to, date = nil)
8
- ActiveCurrency::Rate.current_value_for(from, to, date)
8
+ ActiveCurrency::Rate.value_for(from, to, date)
9
9
  end
10
10
 
11
11
  def add_rate(from, to, rate, date = nil)
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCurrency
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -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.1.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-14 00:00:00.000000000 Z
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: '5.0'
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: '5.0'
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.6.11
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]
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveCurrency
4
- class ApplicationRecord < ActiveRecord::Base
5
- self.abstract_class = true
6
- end
7
- end