strict_money 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/strict_money.rb +1 -0
- data/lib/strict_money/historical_amount.rb +70 -0
- data/lib/strict_money/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c04c871ef580891d4b2051ea13c674f8270e7227cde9f747586674b3a221e69
|
4
|
+
data.tar.gz: 77a8e7ae2871b230300d6a5dc3367e44c1bf9c405783a1dfbd6f7fb8e5e9b80a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1279f1239a1b19e15eb6f183704fefe0453530e764a1139cc4774e85ad7ba2101dac31a0f2338d8c96c7f0b56ef7a81ddae7a827da4ad4269a9542d824378546
|
7
|
+
data.tar.gz: 1835a5ce764a0c68a61500a395f935bca7dd15047bc7da30abe63e98045484cf975c4e08bf0314df1e7b3810900f58570144e7e69ae17b595e1f3165e18fc9b3
|
data/lib/strict_money.rb
CHANGED
@@ -0,0 +1,70 @@
|
|
1
|
+
module StrictMoney
|
2
|
+
# A value object to describe one or more money amounts at a point in time
|
3
|
+
# when the exchange rate was known.
|
4
|
+
class HistoricalAmount
|
5
|
+
def self.zero(*currencies)
|
6
|
+
new(currencies.uniq.map { |currency| Amount.new(0, currency) })
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(amounts)
|
10
|
+
@amounts = amounts
|
11
|
+
if currencies != currencies.uniq
|
12
|
+
raise RedundantCurrencyError, currencies
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
[:-@, :*, :/, :round].each do |operator|
|
17
|
+
define_method(operator) do |*args|
|
18
|
+
new_amounts = @amounts.map { |amount|
|
19
|
+
amount.send(operator, *args)
|
20
|
+
}
|
21
|
+
self.class.new(new_amounts)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def <=>(other)
|
26
|
+
if other.is_a?(HistoricalAmount) && currencies == other.currencies
|
27
|
+
currency = currencies.first
|
28
|
+
as_float(currency) <=> other.as_float(currency)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def +(addend)
|
33
|
+
raise WrongCurrencyError unless addend.currencies == currencies
|
34
|
+
new_amounts = @amounts.map { |amount|
|
35
|
+
amount + addend.amount(amount.currency)
|
36
|
+
}
|
37
|
+
self.class.new(new_amounts)
|
38
|
+
end
|
39
|
+
|
40
|
+
def as_float(currency)
|
41
|
+
amount(currency).as_float(currency)
|
42
|
+
end
|
43
|
+
|
44
|
+
def as_json(_options = nil)
|
45
|
+
currencies.each_with_object({}) { |currency, result|
|
46
|
+
result[currency] = amount(currency).as_float(currency)
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def currencies
|
51
|
+
@amounts.map(&:currency).sort
|
52
|
+
end
|
53
|
+
|
54
|
+
def amount(currency)
|
55
|
+
@amounts.detect { |amount| amount.currency == currency }
|
56
|
+
end
|
57
|
+
|
58
|
+
def positive?
|
59
|
+
@amounts.all?(&:positive?)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Raised when HistoricalAmount is instantiated with more than one amount of
|
63
|
+
# a given currency.
|
64
|
+
RedundantCurrencyError = Class.new(StandardError)
|
65
|
+
|
66
|
+
# Raised when an method is called with another historical amount whose
|
67
|
+
# currencies are not the same.
|
68
|
+
WrongCurrencyError = Class.new(StandardError)
|
69
|
+
end
|
70
|
+
end
|
data/lib/strict_money/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strict_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francis Hwang
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- lib/strict_money.rb
|
103
103
|
- lib/strict_money/amount.rb
|
104
104
|
- lib/strict_money/configuration.rb
|
105
|
+
- lib/strict_money/historical_amount.rb
|
105
106
|
- lib/strict_money/orms/mongoid.rb
|
106
107
|
- lib/strict_money/orms/mongoid/amount.rb
|
107
108
|
- lib/strict_money/orms/mongoid/configuration.rb
|