strict_money 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f1d0d98c2cc60330eaf48ae5b1fa13f98d7348bfa237e335aae89237e1c8fdc
4
- data.tar.gz: 7c3859e13cf3ae630d269b60e083ca5aa669f231e07c212c10bca735c81bff9c
3
+ metadata.gz: 9c04c871ef580891d4b2051ea13c674f8270e7227cde9f747586674b3a221e69
4
+ data.tar.gz: 77a8e7ae2871b230300d6a5dc3367e44c1bf9c405783a1dfbd6f7fb8e5e9b80a
5
5
  SHA512:
6
- metadata.gz: 3eb49d358d72a98917f2b31a0f84ebc911d5e7e814798514a9a330b8319f78ed2084d00af43866908110fd5eef62f3bccccded7e133436bb1fc107a76775a68d
7
- data.tar.gz: a0ae54dc06c07bf04c5feea5a778ec1b34aed59f6bbd8e18a43a8d5a098181257880320d3e72d8fbf749700393d870eee19f8354365efad5e03a442ec92092fd
6
+ metadata.gz: 1279f1239a1b19e15eb6f183704fefe0453530e764a1139cc4774e85ad7ba2101dac31a0f2338d8c96c7f0b56ef7a81ddae7a827da4ad4269a9542d824378546
7
+ data.tar.gz: 1835a5ce764a0c68a61500a395f935bca7dd15047bc7da30abe63e98045484cf975c4e08bf0314df1e7b3810900f58570144e7e69ae17b595e1f3165e18fc9b3
data/lib/strict_money.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "strict_money/amount"
2
2
  require "strict_money/configuration"
3
+ require "strict_money/historical_amount"
3
4
  require "strict_money/version"
4
5
  require "strict_money/railtie" if defined?(Rails::Railtie)
5
6
  require "forwardable"
@@ -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
@@ -1,3 +1,3 @@
1
1
  module StrictMoney
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
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.3.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: 2019-12-17 00:00:00.000000000 Z
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