active_currency 1.1.0 → 1.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
  SHA256:
3
- metadata.gz: deaf57273ac8a06081edea671655192a53a560f5b4d2573818e1b3998d892f94
4
- data.tar.gz: 78a851b36ae4a92fda99e8dd768685b0e5627069e75bbc07b349154823549c61
3
+ metadata.gz: cfc5f4cd4fb66fb4bf8792dacfd16014f2bb5bb44d603a4b5cd72985999b1729
4
+ data.tar.gz: 20995e8b1c9b3d4935bc1e6f1d9a2832fd0cd703e4fcb60de44b413e78995dae
5
5
  SHA512:
6
- metadata.gz: 502199ad5fa78e5b7710ef17e82de1e1f9e3ff68758a9193321c15524c636b2bec34c7f9839079122693f9c3044821aec37e862745279dbe34d9c2e45e97dfeb
7
- data.tar.gz: 1f733721568ddacbc5a99335b761dee893191415a147c6639c0996f6d95a3df6fb373eebae298260906ddf704dbe9b3be0120857d845b9aa88ef0719ae06ca0e
6
+ metadata.gz: 8804e7be48d851288b48efb05d7dbba0c57b62d3b71937d5e375c98de4485bbcab9a2de042abe4614e37d77cfc0a709b310faca4d6245156373c4924c4311b80
7
+ data.tar.gz: 3843245a24efe3d2eec8ae19a834b4d7e1e7b82d48c7203c5f41326e894c42228b524c6379f38beee9732448587b58392f814894dfbc75a1b5199d344a9290a0
data/README.md CHANGED
@@ -84,6 +84,9 @@ bank.app_id = '…'
84
84
  ActiveCurrency::AddRates.call(%w[EUR USD], bank: bank)
85
85
  ```
86
86
 
87
+ The first currency you give to `AddRates` is considered the default currency
88
+ against which other currency rates will be guessed if they are unavailable.
89
+
87
90
  ## Tests
88
91
 
89
92
  In your app test suite you may not want to have to fill your database to be
@@ -128,12 +131,17 @@ Then:
128
131
 
129
132
  ```sh
130
133
  gem install bundler:1.17.2
131
- BUNDLE_GEMFILE=Gemfile-rails3.2 bundle update active_currency
132
- BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update active_currency
134
+ BUNDLE_GEMFILE=Gemfile-rails3.2 bundle update
135
+ BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update
133
136
 
134
137
  gem install bundler:2.1.2
135
- BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update active_currency
136
- BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update active_currency
138
+ BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update
139
+ BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update
140
+
141
+ BUNDLE_GEMFILE=Gemfile-rails3.2 bundle exec rake
142
+ BUNDLE_GEMFILE=Gemfile-rails4.2 bundle exec rake
143
+ BUNDLE_GEMFILE=Gemfile-rails5.2 bundle exec rake
144
+ BUNDLE_GEMFILE=Gemfile-rails6.0 bundle exec rake
137
145
 
138
146
  git add CHANGELOG.md lib/active_currency/version.rb Gemfile-rails*.lock
139
147
  git commit -m v`ruby -r./lib/active_currency/version <<< 'puts ActiveCurrency::VERSION'`
@@ -11,10 +11,13 @@ module ActiveCurrency
11
11
  }
12
12
 
13
13
  def self.value_for(from, to, date = nil)
14
- scope = date ? before(date) : all_scope
14
+ from = Money::Currency.new(from)
15
+ to = Money::Currency.new(to)
16
+ return 1 if from == to
15
17
 
18
+ scope = date ? before(date) : all_scope
16
19
  scope
17
- .where(from: from, to: to)
20
+ .where(from: from.iso_code, to: to.iso_code)
18
21
  .order(:created_at)
19
22
  .last
20
23
  &.value
@@ -4,16 +4,18 @@ module ActiveCurrency
4
4
  # Store the latest currency rates.
5
5
  class AddRates
6
6
  def initialize(currencies, bank: EuCentralBank.new)
7
- @currencies = currencies
7
+ @currencies = currencies.map(&:to_s).map(&:upcase)
8
8
  @bank = bank
9
9
  end
10
10
 
11
11
  def call
12
12
  bank.update_rates
13
13
 
14
- other_currencies.each do |to|
15
- store_rate(to)
14
+ rates_hash.each do |(from, to), rate|
15
+ store.add_rate(from, to, rate)
16
16
  end
17
+
18
+ nil
17
19
  end
18
20
 
19
21
  def self.call(currencies, *options)
@@ -22,38 +24,40 @@ module ActiveCurrency
22
24
 
23
25
  private
24
26
 
25
- attr_accessor :bank
26
-
27
- def currencies
28
- @currencies.map(&:to_s).map(&:upcase)
29
- end
30
-
31
- def other_currencies
32
- currencies.drop(1)
33
- end
34
-
35
- def from
36
- @from ||= currencies.first
37
- end
27
+ attr_accessor :bank, :currencies
38
28
 
39
29
  def store
40
30
  @store ||= ActiveCurrency::RateStore.new
41
31
  end
42
32
 
43
- def store_rate(to)
44
- rate, inverse = get_rates(to)
33
+ def rates_hash
34
+ currencies.each_with_object({}) do |from, hash|
35
+ currencies.each do |to|
36
+ next if from == to
45
37
 
46
- store.add_rate(from, to, rate)
47
- store.add_rate(to, from, inverse)
38
+ hash[[from, to]] = get_rate(hash, from, to)
39
+ end
40
+ end
48
41
  end
49
42
 
50
- def get_rates(to)
43
+ def get_rate(hash, from, to)
51
44
  rate = bank.get_rate(from, to)
52
- raise "Unknown rate between #{from} and #{to}" if rate.nil? || rate.zero?
45
+ return rate if rate
46
+
47
+ # Inverse rate (not so good)
48
+ inverse = hash[[to, from]]
49
+ return 1.fdiv(inverse) if inverse
53
50
 
54
- inverse = bank.get_rate(to, from) || 1.fdiv(rate)
51
+ # Rate going through the first currency (desperate)
52
+ from_main = hash[[from, main_currency]]
53
+ to_main = hash[[main_currency, to]]
54
+ return from_main * to_main if from_main && to_main
55
+
56
+ raise "Unknown rate between #{from} and #{to}"
57
+ end
55
58
 
56
- [rate, inverse]
59
+ def main_currency
60
+ currencies.first
57
61
  end
58
62
  end
59
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCurrency
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_currency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.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: 2019-12-27 00:00:00.000000000 Z
11
+ date: 2020-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
139
153
  description: Store your currency.
140
154
  email:
141
155
  - sunny@sunfox.org