minting 1.2.0 → 1.3.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/lib/minting/mint/currency.rb +4 -0
- data/lib/minting/mint/registry.rb +4 -4
- data/lib/minting/money/allocation.rb +3 -2
- data/lib/minting/money/arithmetics.rb +1 -1
- data/lib/minting/money/coercion.rb +6 -6
- data/lib/minting/money/money.rb +20 -5
- data/lib/minting/money/parse.rb +1 -1
- data/lib/minting/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a7530401c0a722596508aab5aa587bc53a548698308f9d09ae34fac2ec671784
|
|
4
|
+
data.tar.gz: abe0501b11d4f9c77e2054443c12e1d4a5e2707fabbfac71f5bd4b8a7f5a3fb5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 31db7b1c31b0f1329342162cb8f868e46d14e703df5dd36898dfeb74281d674bc0f7ba0b9b367d8568f0751c09f3a219dec172ed87cc8cc4d906606550492034
|
|
7
|
+
data.tar.gz: 7090942000722ce81b302237a6b71850ef8a8ec477ded927bb5914f1e198552512ae11b0b1f46d38dd44ae0d6b4a2b1e7d34dab3ec35ea107b800c38e60c7a94
|
|
@@ -9,9 +9,9 @@ module Mint
|
|
|
9
9
|
# @raise [ArgumentError] if the currency code is not registered
|
|
10
10
|
def self.money(amount, currency_code)
|
|
11
11
|
currency = currency(currency_code)
|
|
12
|
-
return Money.
|
|
12
|
+
return Money.create(amount, currency) if currency
|
|
13
13
|
|
|
14
|
-
raise ArgumentError, "
|
|
14
|
+
raise ArgumentError, "[#{currency.inspect}] is not a registered currency. Check Mint.currencies"
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
# Returns default zero, no currency money
|
|
@@ -66,9 +66,9 @@ module Mint
|
|
|
66
66
|
"Currency: #{code} already registered"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
-
currencies[code] = Currency.new(code:, subunit:, symbol:, priority:)
|
|
69
|
+
currency = currencies[code] = Currency.new(code:, subunit:, symbol:, priority:)
|
|
70
70
|
@currency_symbols = nil
|
|
71
|
-
|
|
71
|
+
currency
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
# Returns the hash of all registered currencies.
|
|
@@ -45,11 +45,12 @@ module Mint
|
|
|
45
45
|
|
|
46
46
|
def allocate_left_over!(amounts:, left_over:)
|
|
47
47
|
if left_over.nonzero?
|
|
48
|
-
minimum =
|
|
48
|
+
minimum = currency.minimum_amount
|
|
49
|
+
minimum = -minimum if left_over.negative?
|
|
49
50
|
last_slot = (left_over / minimum).to_i - 1
|
|
50
51
|
(0..last_slot).each { |slot| amounts[slot] += minimum }
|
|
51
52
|
end
|
|
52
|
-
amounts.map {
|
|
53
|
+
amounts.map { Money.new(it, currency) }
|
|
53
54
|
end
|
|
54
55
|
end
|
|
55
56
|
end
|
|
@@ -60,7 +60,7 @@ module Mint
|
|
|
60
60
|
# @return [Money] the multiplied Money instance
|
|
61
61
|
# @raise [TypeError] if multiplier is not Numeric or is a Money object
|
|
62
62
|
def *(multiplicand)
|
|
63
|
-
return mint(amount * multiplicand
|
|
63
|
+
return mint(amount * multiplicand) if multiplicand.is_a?(Numeric)
|
|
64
64
|
|
|
65
65
|
raise TypeError, "#{self} can't be multiplied by #{multiplicand}"
|
|
66
66
|
end
|
|
@@ -13,8 +13,6 @@ module Mint
|
|
|
13
13
|
# Coerced Number contains the arithmetic logic for numeric compatible ops.
|
|
14
14
|
# @private
|
|
15
15
|
class CoercedNumber
|
|
16
|
-
include Comparable
|
|
17
|
-
|
|
18
16
|
# @private
|
|
19
17
|
def initialize(value)
|
|
20
18
|
@value = value
|
|
@@ -44,19 +42,21 @@ module Mint
|
|
|
44
42
|
raise_coercion_error(:/, other)
|
|
45
43
|
end
|
|
46
44
|
|
|
47
|
-
#
|
|
45
|
+
# Only zero is dimensionless and comparable to Money.
|
|
46
|
+
# e.g. 0 < price is meaningful; 0.5 < price is not (what currency is 0.5?).
|
|
48
47
|
def <=>(other)
|
|
49
|
-
return nil if @value.nil? || other.nil?
|
|
50
48
|
return @value <=> other.amount if @value.zero? || other.zero?
|
|
51
49
|
|
|
52
50
|
raise_coercion_error(:<=>, other)
|
|
53
51
|
end
|
|
54
52
|
|
|
55
|
-
|
|
53
|
+
private
|
|
54
|
+
|
|
56
55
|
def raise_coercion_error(operation, operand)
|
|
57
56
|
raise TypeError,
|
|
58
|
-
"#{
|
|
57
|
+
"#{@value} #{operation} #{operand} : incompatible operands"
|
|
59
58
|
end
|
|
60
59
|
end
|
|
60
|
+
private_constant :CoercedNumber
|
|
61
61
|
end
|
|
62
62
|
end
|
data/lib/minting/money/money.rb
CHANGED
|
@@ -10,13 +10,16 @@ module Mint
|
|
|
10
10
|
# @param amount [Numeric] The monetary amount
|
|
11
11
|
# @param currency [Currency] The currency object
|
|
12
12
|
# @raise [ArgumentError] If amount is not numeric or currency is invalid
|
|
13
|
-
def
|
|
13
|
+
def self.create(amount, currency)
|
|
14
14
|
raise ArgumentError, 'amount must be Numeric' unless amount.is_a?(Numeric)
|
|
15
|
-
raise ArgumentError, 'currency must be a Currency object' unless currency.is_a?(Currency)
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
checked_currency = Mint.currency(currency)
|
|
17
|
+
unless checked_currency
|
|
18
|
+
raise ArgumentError,
|
|
19
|
+
"Currency not found (#{currency}). Check Mint.currencies"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
new(checked_currency.normalize_amount(amount), checked_currency)
|
|
20
23
|
end
|
|
21
24
|
|
|
22
25
|
# Returns the ISO 3-letter currency code string.
|
|
@@ -35,6 +38,7 @@ module Mint
|
|
|
35
38
|
# @param new_amount [Numeric] The new amount
|
|
36
39
|
# @return [Money] A new Money object or self
|
|
37
40
|
def mint(new_amount)
|
|
41
|
+
new_amount = new_amount.to_r.round(currency.subunit)
|
|
38
42
|
new_amount == amount ? self : Money.new(new_amount, currency)
|
|
39
43
|
end
|
|
40
44
|
|
|
@@ -50,5 +54,16 @@ module Mint
|
|
|
50
54
|
# @param other [Object] the target object to compare
|
|
51
55
|
# @return [Boolean] true if currencies match, false otherwise
|
|
52
56
|
def same_currency?(other) = other.respond_to?(:currency) && other.currency == currency
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# Initializes a new Money object with the given amount and currency.
|
|
61
|
+
# @param amount [Numeric] The monetary amount
|
|
62
|
+
# @param currency [Currency] The currency object
|
|
63
|
+
def initialize(amount, currency)
|
|
64
|
+
@amount = amount
|
|
65
|
+
@currency = currency
|
|
66
|
+
freeze
|
|
67
|
+
end
|
|
53
68
|
end
|
|
54
69
|
end
|
data/lib/minting/money/parse.rb
CHANGED
|
@@ -25,7 +25,7 @@ module Mint
|
|
|
25
25
|
currency = currency ? Mint.currency(currency) : parse_currency(input)
|
|
26
26
|
raise ArgumentError, "Currency [#{currency}] not registered" unless currency
|
|
27
27
|
|
|
28
|
-
amount = parse_amount(input)
|
|
28
|
+
amount = currency.normalize_amount(parse_amount(input))
|
|
29
29
|
new(amount, currency)
|
|
30
30
|
end
|
|
31
31
|
|
data/lib/minting/version.rb
CHANGED