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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b05d1cbbf6c714102f38f165effba77384e4fc5cebf691d4b9ab75082c22b992
4
- data.tar.gz: 2d5bb5aeb6e527a2d4419bcca35d79c426709797975e9ec38217558d875ea9b8
3
+ metadata.gz: a7530401c0a722596508aab5aa587bc53a548698308f9d09ae34fac2ec671784
4
+ data.tar.gz: abe0501b11d4f9c77e2054443c12e1d4a5e2707fabbfac71f5bd4b8a7f5a3fb5
5
5
  SHA512:
6
- metadata.gz: ce7311d97cbe60c2f71ba5f65d1313704084eebae842e012c2cd857230055ef56007575e8f89887f8ae157c004d205e43370ada8d2fc2b5f3ea00b7c21c8017a
7
- data.tar.gz: e4903099bb997667472ddc0c26ff07b3894bb9fa5122221c94045d84f2699e28d203646429e341a8119177a07212991f04d36c5806a12c8388766a3a8aebb6eb
6
+ metadata.gz: 31db7b1c31b0f1329342162cb8f868e46d14e703df5dd36898dfeb74281d674bc0f7ba0b9b367d8568f0751c09f3a219dec172ed87cc8cc4d906606550492034
7
+ data.tar.gz: 7090942000722ce81b302237a6b71850ef8a8ec477ded927bb5914f1e198552512ae11b0b1f46d38dd44ae0d6b4a2b1e7d34dab3ec35ea107b800c38e60c7a94
@@ -12,6 +12,10 @@ module Mint
12
12
  "<Currency:(#{code} #{symbol} #{subunit})>"
13
13
  end
14
14
 
15
+ def normalize_amount(amount)
16
+ amount.to_r.round(subunit)
17
+ end
18
+
15
19
  private
16
20
 
17
21
  def initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil)
@@ -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.new(amount, currency) if currency
12
+ return Money.create(amount, currency) if currency
13
13
 
14
- raise ArgumentError, "Currency [#{currency_code}] not registered. Check Mint.currencies"
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
- currencies[code]
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 = left_over.positive? ? currency.minimum_amount : -currency.minimum_amount
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 { mint it }
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.to_r) if multiplicand.is_a?(Numeric)
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
- # @private
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
- # @private
53
+ private
54
+
56
55
  def raise_coercion_error(operation, operand)
57
56
  raise TypeError,
58
- "#{self} #{operation} #{operand} : incompatible operands"
57
+ "#{@value} #{operation} #{operand} : incompatible operands"
59
58
  end
60
59
  end
60
+ private_constant :CoercedNumber
61
61
  end
62
62
  end
@@ -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 initialize(amount, currency)
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
- @amount = amount.to_r.round(currency.subunit)
18
- @currency = currency
19
- freeze
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
@@ -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
 
@@ -1,5 +1,5 @@
1
1
  # Root namespace for the Minting library.
2
2
  module Minting
3
3
  # Current version of the Minting gem.
4
- VERSION = '1.2.0'.freeze
4
+ VERSION = '1.3.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minting
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilson Ferraz