minting 1.9.4 → 1.9.5
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/README.md +0 -5
- data/lib/minting/currency/currency.rb +5 -3
- data/lib/minting/mint/dsl/numeric.rb +14 -16
- data/lib/minting/mint/dsl/string.rb +7 -9
- data/lib/minting/mint/mint.rb +8 -3
- data/lib/minting/money/constructors.rb +5 -11
- 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: 429ae46c67aa4b6bb032a9e9be85cbf02b2ee0576cce83c0ad4f36aa0c46e08c
|
|
4
|
+
data.tar.gz: a5a1c37ad5f6b8ced5e62323803431cd7385577c7a15963b0a29c92f7f7907b8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad956434a0dd03f554a638466d44ce049cd7b16e1592f07507954d331d32b5ede3a3f9cb635b6fe9ad6204fe44f840240cbd6d8d49d5c18afcf7b4401328ecab
|
|
7
|
+
data.tar.gz: 5c81f05414ea2c8f4e6b68daaf5a48a76ab7109c68581246333d99e488d9eec3faf5f5e6dc7e8293f2dfb2d6fb51f01b43ddc49770fde62cb49c05288496a7be
|
data/README.md
CHANGED
|
@@ -57,9 +57,6 @@ require 'minting'
|
|
|
57
57
|
# Create money
|
|
58
58
|
ten = Mint.money(10, 'USD') #=> [USD 10.00]
|
|
59
59
|
|
|
60
|
-
# Create money using Numeric refinements
|
|
61
|
-
using Mint
|
|
62
|
-
|
|
63
60
|
1.dollar == Mint.money(1, 'USD') #=> true
|
|
64
61
|
ten = 10.dollars #=> [USD 10.00]
|
|
65
62
|
4.to_money('USD') #=> [USD 4.00]
|
|
@@ -213,8 +210,6 @@ Modes: `:half_up` (default), `:half_down`, `:floor`, `:ceil`, `:truncate`, `:dow
|
|
|
213
210
|
|
|
214
211
|
> **Performance note:** Rounding-mode support is not loaded by default — `require 'minting'` uses the fastest possible rounding (equivalent to `:half_up`) with zero dispatch overhead. The first call to `Mint.with_rounding` loads the rounding module and patches `Currency#normalize_amount`, adding ~10–35 ns per money creation or mutation. If your application never uses custom rounding modes (the common case), there is **no performance cost**.
|
|
215
212
|
|
|
216
|
-
**Refinements** — `10.dollars` and similar helpers require `using Mint` in the current scope (see Usage above).
|
|
217
|
-
|
|
218
213
|
**Division** — `money / 5` returns new `Money`; `money / other_money` returns a numeric ratio, not money.
|
|
219
214
|
|
|
220
215
|
**Zero equality** — Any zero amount is considered equal across currencies and to numeric zero (`Mint.money(0, 'USD') == Mint.money(0, 'EUR')` is intentionally `true`). Non-zero amounts must match currency and value.
|
|
@@ -88,9 +88,11 @@ module Mint
|
|
|
88
88
|
#
|
|
89
89
|
# @param object [String, Currency, Money, nil] a currency code, object, or +nil+
|
|
90
90
|
# @return [Currency] the resolved Currency
|
|
91
|
-
# @raise [
|
|
91
|
+
# @raise [Mint::UnknownCurrency] if +object+ cannot be resolved into a
|
|
92
|
+
# registered currency. +Mint::UnknownCurrency+ inherits from +ArgumentError+,
|
|
93
|
+
# so existing +rescue ArgumentError+ handlers continue to work.
|
|
92
94
|
def Currency.resolve!(object)
|
|
93
|
-
resolve(object) or raise
|
|
95
|
+
resolve(object) or raise Mint::UnknownCurrency, "Could not resolve (#{object}) into a currency"
|
|
94
96
|
end
|
|
95
97
|
|
|
96
98
|
# Looks up a registered currency by its alpha code.
|
|
@@ -114,6 +116,6 @@ module Mint
|
|
|
114
116
|
#
|
|
115
117
|
# @param currency [String, Currency] a currency code or object
|
|
116
118
|
# @return [Money] a frozen zero-Money
|
|
117
|
-
# @raise [
|
|
119
|
+
# @raise [Mint::UnknownCurrency] if the currency can't be resolved
|
|
118
120
|
def Currency.zero(currency) = Registry.zero_for(Currency.resolve!(currency))
|
|
119
121
|
end
|
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def reais = Mint::Money.from(self, 'BRL')
|
|
3
|
+
# Core extension: adds money-conversion helpers to Numeric.
|
|
4
|
+
class Numeric
|
|
5
|
+
# @return [Money] self interpreted as BRL
|
|
6
|
+
def reais = Mint::Money.from(self, 'BRL')
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
# @return [Money] self interpreted as USD
|
|
9
|
+
def dollars = Mint::Money.from(self, 'USD')
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
# @return [Money] self interpreted as EUR
|
|
12
|
+
def euros = Mint::Money.from(self, 'EUR')
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
# @param currency [String, Symbol, Currency] target currency
|
|
15
|
+
# @return [Money] self interpreted as the given currency
|
|
16
|
+
def to_money(currency) = Mint::Money.from(self, currency)
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
end
|
|
18
|
+
alias dollar dollars
|
|
19
|
+
alias euro euros
|
|
20
|
+
alias mint to_money
|
|
23
21
|
end
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def to_money(currency) = Mint::Money.from(to_r, currency)
|
|
11
|
-
end
|
|
3
|
+
# Core extension: adds money-parsing helper to String.
|
|
4
|
+
class String
|
|
5
|
+
# Parses self as a numeric string and creates a Money in the given currency.
|
|
6
|
+
#
|
|
7
|
+
# @param currency [String, Symbol, Currency] target currency
|
|
8
|
+
# @return [Money]
|
|
9
|
+
def to_money(currency = nil) = Mint::Money.parse(self, currency)
|
|
12
10
|
end
|
data/lib/minting/mint/mint.rb
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
# Mint currency registration and factory (public API)
|
|
4
4
|
module Mint
|
|
5
|
-
#
|
|
6
|
-
|
|
5
|
+
# Raised when a currency cannot be resolved from a code or object.
|
|
6
|
+
#
|
|
7
|
+
# Inherits from +ArgumentError+ so existing +rescue ArgumentError+ handlers
|
|
8
|
+
# continue to work; rescue +Mint::UnknownCurrency+ for the specific case.
|
|
9
|
+
class UnknownCurrency < ArgumentError
|
|
7
10
|
end
|
|
8
11
|
|
|
9
12
|
# Creates a new {Money} instance with the given amount and currency code.
|
|
@@ -11,7 +14,9 @@ module Mint
|
|
|
11
14
|
# @param amount [Numeric] the financial value
|
|
12
15
|
# @param currency_code [Currency, String] Currency code
|
|
13
16
|
# @return [Money] the instantiated Money object
|
|
14
|
-
# @raise [ArgumentError] if the
|
|
17
|
+
# @raise [ArgumentError] if the amount is not a Numeric
|
|
18
|
+
# @raise [Mint::UnknownCurrency] if the currency code is not registered.
|
|
19
|
+
# +Mint::UnknownCurrency+ inherits from +ArgumentError+.
|
|
15
20
|
def self.money(amount, currency_code) = Money.from(amount, currency_code)
|
|
16
21
|
|
|
17
22
|
# @return [Hash{String => Currency}] the frozen world-currencies hash
|
|
@@ -7,7 +7,8 @@ module Mint
|
|
|
7
7
|
# @param amount [Numeric] The monetary amount
|
|
8
8
|
# @param currency [Currency, String] The currency code or currency object
|
|
9
9
|
# @return [Money] the new Money instance
|
|
10
|
-
# @raise [ArgumentError] If amount is not numeric
|
|
10
|
+
# @raise [ArgumentError] If amount is not numeric
|
|
11
|
+
# @raise [Mint::UnknownCurrency] If currency cannot be resolved
|
|
11
12
|
# @example
|
|
12
13
|
# Money.from(10, 'USD') #=> [USD 10.00]
|
|
13
14
|
def self.from(amount, currency)
|
|
@@ -61,16 +62,9 @@ module Mint
|
|
|
61
62
|
#
|
|
62
63
|
# @param currency [String, Currency] a currency code or object
|
|
63
64
|
# @return [Money] a frozen zero-Money
|
|
64
|
-
# @raise [
|
|
65
|
+
# @raise [Mint::UnknownCurrency] if the currency can't be resolved
|
|
65
66
|
def self.zero(currency) = Currency.resolve!(currency).zero
|
|
66
67
|
|
|
67
|
-
# Backwards-compatible alias for previous API
|
|
68
|
-
# TODO: deprecate in a future major release
|
|
69
|
-
def self.create(amount, currency)
|
|
70
|
-
warn 'Money.create is now deprecated. Use Money.from'
|
|
71
|
-
from(amount, currency)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
68
|
# Builds a Money from a subunit (smallest-unit) Integer amount.
|
|
75
69
|
# This is the inverse of {#subunits}: for USD, the subunit is
|
|
76
70
|
# 1 cent; for JPY it is 1 yen; for IQD it is 1 dinar (subunit 3).
|
|
@@ -79,8 +73,8 @@ module Mint
|
|
|
79
73
|
# smallest unit (e.g. cents). Must be an Integer to preserve exactness.
|
|
80
74
|
# @param currency [String, Symbol, Currency] the currency identifier
|
|
81
75
|
# @return [Money] the resulting Money instance
|
|
82
|
-
# @raise [ArgumentError] if +subunits+ is not an Integer
|
|
83
|
-
#
|
|
76
|
+
# @raise [ArgumentError] if +subunits+ is not an Integer
|
|
77
|
+
# @raise [Mint::UnknownCurrency] if +currency+ is not registered
|
|
84
78
|
#
|
|
85
79
|
# @example USD cents
|
|
86
80
|
# Money.from_subunits(123_456, 'USD') #=> [USD 1234.56]
|
data/lib/minting/version.rb
CHANGED