minting 1.5.1 → 1.6.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/README.md +5 -5
- data/doc/agents/recommendations.md +1 -1
- data/lib/minting/data/currencies.yaml +6 -0
- data/lib/minting/mint/currency.rb +10 -19
- data/lib/minting/mint/currency_store.rb +8 -35
- data/lib/minting/mint/mint.rb +30 -0
- data/lib/minting/mint/parser.rb +77 -0
- data/lib/minting/mint/refinements.rb +1 -3
- data/lib/minting/mint/registry.rb +7 -41
- data/lib/minting/mint/world_currencies.rb +17 -0
- data/lib/minting/mint.rb +11 -0
- data/lib/minting/money/allocation.rb +1 -1
- data/lib/minting/money/arithmetics.rb +1 -3
- data/lib/minting/money/comparable.rb +5 -3
- data/lib/minting/money/constructors.rb +1 -1
- data/lib/minting/version.rb +1 -1
- metadata +4 -3
- data/lib/minting/money/parse.rb +0 -79
- data/lib/minting/money.rb +0 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 293a64ebe3fd00ed8f6d062b596234550f85ead517b7f8094648dd9502be2f86
|
|
4
|
+
data.tar.gz: 3a98bb94ce601340b9cced8b275ee0be3e578d7e3cf781245cdd0fe5ee8038f1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fe587b6b5180c831b1e32092dfe9857ae37516f7fd274215be27e21af1263e99a93725f019e3be96962c5f20c13519326fc2b18b81b88e22aab43dc0d93a9b49
|
|
7
|
+
data.tar.gz: b0b8ca0b0dd832668e7b37dfbcca6e3606c673496c8149de90e9d2ffd21a166205e37ae9b455e36b1a71b07729a1d8c5bac074022991d45906ec7c410ec73740
|
data/README.md
CHANGED
|
@@ -148,7 +148,7 @@ price.clamp(min_price, 100) #=> [USD 75.00]
|
|
|
148
148
|
|
|
149
149
|
**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.
|
|
150
150
|
|
|
151
|
-
**Custom currencies** — `Mint.register_currency
|
|
151
|
+
**Custom currencies** — `Mint.register_currency`, Only registered currency codes and symbolos are recoginized by the parser.
|
|
152
152
|
|
|
153
153
|
**Built-in currencies** — ISO-style codes ship in `lib/minting/data/currencies.yaml` and load when the registry is first accessed.
|
|
154
154
|
|
|
@@ -188,10 +188,10 @@ gem install minting
|
|
|
188
188
|
## Parsing strings
|
|
189
189
|
|
|
190
190
|
```ruby
|
|
191
|
-
Mint
|
|
192
|
-
Mint
|
|
193
|
-
Mint
|
|
194
|
-
Mint
|
|
191
|
+
Mint.parse('$19.99') #=> [USD 19.99]
|
|
192
|
+
Mint.parse('19,99 €') #=> [EUR 19.99]
|
|
193
|
+
Mint.parse('1.234,56', 'EUR') #=> [EUR 1234.56]
|
|
194
|
+
Mint.parse('USD 1,234.56') #=> [USD 1234.56]
|
|
195
195
|
```
|
|
196
196
|
|
|
197
197
|
- Pass a currency code when the string has no symbol or code.
|
|
@@ -208,7 +208,7 @@ property test:
|
|
|
208
208
|
|
|
209
209
|
```ruby
|
|
210
210
|
m = Mint.money(9.99, 'USD')
|
|
211
|
-
assert_equal m, Mint
|
|
211
|
+
assert_equal m, Mint.parse(m.inspect.delete_prefix('[').delete_suffix(']'))
|
|
212
212
|
```
|
|
213
213
|
|
|
214
214
|
Make it a one-liner or a property-based test with 10–20 random
|
|
@@ -4,11 +4,16 @@ module Mint
|
|
|
4
4
|
# Represents a specific currency unit, identified by ISO 4217 alphabetic code
|
|
5
5
|
#
|
|
6
6
|
# @see https://www.iso.org/iso-4217-currency-codes.html
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
Currency = Data.define(:code, :subunit, :symbol, :priority, :country, :name,
|
|
8
|
+
:fractional_multiplier, :minimum_amount) do
|
|
9
|
+
def initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil)
|
|
10
|
+
subunit = subunit.to_i
|
|
11
|
+
priority = priority.to_i
|
|
12
|
+
fractional_multiplier = 10**subunit
|
|
13
|
+
minimum_amount = Rational(1, fractional_multiplier)
|
|
14
|
+
super(code:, subunit:, symbol:, priority:, country:, name:,
|
|
15
|
+
fractional_multiplier:, minimum_amount:)
|
|
16
|
+
end
|
|
12
17
|
|
|
13
18
|
def inspect = "<Currency:(#{code} #{symbol} #{subunit} #{name})>"
|
|
14
19
|
|
|
@@ -16,19 +21,5 @@ module Mint
|
|
|
16
21
|
# 1. Converts to Rational
|
|
17
22
|
# 2. Rounds to respect currency subunit
|
|
18
23
|
def normalize_amount(amount) = amount.to_r.round(subunit)
|
|
19
|
-
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
def initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil)
|
|
23
|
-
@code = code
|
|
24
|
-
@subunit = subunit.to_i
|
|
25
|
-
@symbol = symbol
|
|
26
|
-
@priority = priority.to_i
|
|
27
|
-
@country = country
|
|
28
|
-
@name = name
|
|
29
|
-
@fractional_multiplier = 10**@subunit
|
|
30
|
-
@minimum_amount = Rational(1, fractional_multiplier)
|
|
31
|
-
freeze
|
|
32
|
-
end
|
|
33
24
|
end
|
|
34
25
|
end
|
|
@@ -4,25 +4,24 @@ require 'yaml'
|
|
|
4
4
|
|
|
5
5
|
# Mint currency store (internal)
|
|
6
6
|
module Mint
|
|
7
|
-
# Internal currency
|
|
7
|
+
# Internal currency registry
|
|
8
8
|
# Manages the registry cache and currency symbol lookups.
|
|
9
|
-
module
|
|
9
|
+
module Registry
|
|
10
|
+
module_function
|
|
11
|
+
|
|
10
12
|
# Returns the hash of all registered currencies.
|
|
11
13
|
#
|
|
12
14
|
# @return [Hash{String => Currency}] registered currencies mapped by code
|
|
13
15
|
# @api private
|
|
14
|
-
def
|
|
15
|
-
@currencies ||=
|
|
16
|
-
registry = { 'XXX' => Currency.new(code: 'XXX', name: 'No currency', symbol: '¤') }
|
|
17
|
-
load_currencies(registry)
|
|
18
|
-
end
|
|
16
|
+
def currencies
|
|
17
|
+
@currencies ||= Mint.world_currencies.dup
|
|
19
18
|
end
|
|
20
19
|
|
|
21
20
|
# Registered symbols sorted for detection: longest match wins, then parser priority.
|
|
22
21
|
#
|
|
23
22
|
# @return [Array<Array<String, Currency>>] sorted symbol-to-currency mappings
|
|
24
23
|
# @api private
|
|
25
|
-
def
|
|
24
|
+
def currency_symbols
|
|
26
25
|
@currency_symbols ||= begin
|
|
27
26
|
currencies.values
|
|
28
27
|
.reject { |currency| currency.symbol.empty? }
|
|
@@ -35,34 +34,8 @@ module Mint
|
|
|
35
34
|
# Called when currencies are registered.
|
|
36
35
|
#
|
|
37
36
|
# @api private
|
|
38
|
-
def
|
|
37
|
+
def invalidate_symbols_cache
|
|
39
38
|
@currency_symbols = nil
|
|
40
39
|
end
|
|
41
|
-
|
|
42
|
-
# Loads currencies from YAML file into the registry.
|
|
43
|
-
#
|
|
44
|
-
# @param registry [Hash] the registry hash to populate
|
|
45
|
-
# @return [Hash] the populated registry
|
|
46
|
-
# @api private
|
|
47
|
-
def self.load_currencies(registry)
|
|
48
|
-
base = File.expand_path('../data', __dir__)
|
|
49
|
-
path = File.join(base, 'currencies.yaml')
|
|
50
|
-
|
|
51
|
-
data = YAML.load_file(path)
|
|
52
|
-
data.each do |entry|
|
|
53
|
-
code = entry['code']
|
|
54
|
-
registry[code] = Currency.new(
|
|
55
|
-
code: code,
|
|
56
|
-
subunit: entry['subunit'],
|
|
57
|
-
symbol: entry['symbol'],
|
|
58
|
-
priority: entry['priority'],
|
|
59
|
-
country: entry['country'],
|
|
60
|
-
name: entry['name']
|
|
61
|
-
)
|
|
62
|
-
end
|
|
63
|
-
registry
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
private_class_method :load_currencies
|
|
67
40
|
end
|
|
68
41
|
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Mint currency registration and factory (public API)
|
|
4
|
+
module Mint
|
|
5
|
+
# Unknown currency excpetion
|
|
6
|
+
class UnknownCurreny < StandardError
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Creates a new {Money} instance with the given amount and currency code.
|
|
10
|
+
#
|
|
11
|
+
# @param amount [Numeric] the financial value
|
|
12
|
+
# @param currency_code [Currency, String] Currency code
|
|
13
|
+
# @return [Money] the instantiated Money object
|
|
14
|
+
# @raise [ArgumentError] if the currency code is not registered
|
|
15
|
+
def self.money(amount, currency_code) = Money.create(amount, currency_code)
|
|
16
|
+
|
|
17
|
+
# Finds a registered currency by its code, symbol,
|
|
18
|
+
# or retrieves it directly if already a Currency object.
|
|
19
|
+
#
|
|
20
|
+
# @param currency [String, Currency] the currency identifier or object
|
|
21
|
+
# @return [Currency, nil] the registered Currency instance or nil if not found
|
|
22
|
+
def self.currency(currency)
|
|
23
|
+
case currency
|
|
24
|
+
when nil then nil
|
|
25
|
+
when Currency then currency
|
|
26
|
+
when String then Registry.currencies[currency]
|
|
27
|
+
else raise ArgumentError, "currency must be [Currency] ot [String] (#{currency})"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mint
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
# Parses a human-readable money string into a {Money} object.
|
|
7
|
+
#
|
|
8
|
+
# @param input [String] Amount input, optionally including a currency symbol or code
|
|
9
|
+
# @param currency [String, Symbol, Currency, nil] ISO code when not present in +input+
|
|
10
|
+
# @return [Money]
|
|
11
|
+
# @raise [ArgumentError] when +input+ is invalid or currency cannot be determined
|
|
12
|
+
#
|
|
13
|
+
# @example With explicit currency
|
|
14
|
+
# Money.parse('19.99', 'USD') #=> [USD 19.99]
|
|
15
|
+
# Money.parse('1.234,56', 'EUR') #=> [EUR 1234.56]
|
|
16
|
+
#
|
|
17
|
+
# @example With symbol or code in the string
|
|
18
|
+
# Money.parse('$19.99') #=> [USD 19.99]
|
|
19
|
+
# Money.parse('19,99 €') #=> [EUR 19.99]
|
|
20
|
+
# Money.parse('USD 1,234.56') #=> [USD 1234.56]
|
|
21
|
+
def parse(input, currency = nil)
|
|
22
|
+
raise ArgumentError, 'input must be a String' unless input.is_a?(String)
|
|
23
|
+
|
|
24
|
+
input = input.strip
|
|
25
|
+
raise ArgumentError, 'input cannot be empty' if input.empty?
|
|
26
|
+
|
|
27
|
+
currency = Mint.currency(currency) || parse_currency(input)
|
|
28
|
+
raise ArgumentError, "Currency [#{currency}] not registered" unless currency
|
|
29
|
+
|
|
30
|
+
amount = currency.normalize_amount(parse_amount(input))
|
|
31
|
+
Mint::Money.new(amount, currency)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# Extracts a numeric value from input that should only contain an amount.
|
|
37
|
+
def parse_amount(input)
|
|
38
|
+
# Remove any charater that is not a digit, comma or period
|
|
39
|
+
numeric = input.scan(/[\d.,-]/).join
|
|
40
|
+
numeric = normalize_separators(numeric)
|
|
41
|
+
Rational(numeric)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Converts locale-specific decimal/thousand separators into a plain decimal string.
|
|
45
|
+
def normalize_separators(numeric)
|
|
46
|
+
case [numeric.count(','), numeric.count('.')]
|
|
47
|
+
in [0, 0] | [0, 1] then numeric # Nothing to normalize (e.g. "1500" or "34.21").
|
|
48
|
+
in [1, 0] then numeric.tr(',', '.') # Only one comma: decimal (e.g. 19,99 or 1,234).
|
|
49
|
+
in [c, p] if c > 1 && p > 1 # Both separators appear multiple times
|
|
50
|
+
raise ArgumentError, "could not distinguish decimal and thousand separators in '#{numeric}'"
|
|
51
|
+
in [c, p] if c > 0 && p > 0 # Commas and dots: the rightmost one is the decimal separator.
|
|
52
|
+
if numeric.rindex(',') > numeric.rindex('.')
|
|
53
|
+
numeric.delete('.').tr(',', '.')
|
|
54
|
+
else
|
|
55
|
+
numeric.delete(',')
|
|
56
|
+
end
|
|
57
|
+
else # Multiple of the same separator only (e.g. 1,234,567) — all are thousands.
|
|
58
|
+
numeric.delete(',.')
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def parse_currency(input)
|
|
63
|
+
case input
|
|
64
|
+
when nil then return nil
|
|
65
|
+
when String
|
|
66
|
+
# Prefer an explicit ISO 4217 code (e.g. "USD 1,234.56") over symbol matching.
|
|
67
|
+
currency = Mint.currency(input[/\b([A-Z_]+)\b/, 1])
|
|
68
|
+
return currency if currency
|
|
69
|
+
|
|
70
|
+
# Fall back to registered symbols, longest first (HK$ before $).
|
|
71
|
+
Mint.currency_symbols.each do |symbol, currency|
|
|
72
|
+
return currency if input.include?(symbol)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
raise ArgumentError, 'Currency could not be detected'
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -2,61 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
# Mint currency registration and factory (public API)
|
|
4
4
|
module Mint
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
# @param amount [Numeric] the financial value
|
|
8
|
-
# @param currency_code [String] the ISO currency code
|
|
9
|
-
# @return [Money] the instantiated Money object
|
|
10
|
-
# @raise [ArgumentError] if the currency code is not registered
|
|
11
|
-
def self.money(amount, currency_code)
|
|
12
|
-
currency = currency(currency_code)
|
|
13
|
-
return Money.create(amount, currency) if currency
|
|
14
|
-
|
|
15
|
-
raise ArgumentError, "[#{currency.inspect}] is not a registered currency."
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# Finds a registered currency by its code, symbol,
|
|
19
|
-
# or retrieves it directly if already a Currency object.
|
|
20
|
-
#
|
|
21
|
-
# @param currency [String, Currency] the currency identifier or object
|
|
22
|
-
# @return [Currency, nil] the registered Currency instance or nil if not found
|
|
23
|
-
def self.currency(currency)
|
|
24
|
-
currency.is_a?(Currency) ? currency : CurrencyStore.currencies[currency]
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
# Registers a new currency if not already registered.
|
|
28
|
-
#
|
|
29
|
-
# @param code [String] the unique currency code (e.g. 'USD', 'EUR')
|
|
30
|
-
# @param subunit [Integer] the decimal subunit precision (defaults to 2)
|
|
31
|
-
# @param symbol [String] the display symbol (defaults to '')
|
|
32
|
-
# @param priority [Integer] parser precedence priority (defaults to 0)
|
|
33
|
-
# @return [Currency] the registered or existing Currency instance
|
|
34
|
-
# @raise [ArgumentError] if the code layout is invalid or register throws an error
|
|
35
|
-
def self.register_currency(code:, subunit: 0, symbol: '', priority: 0)
|
|
36
|
-
CurrencyStore.currencies[code] || register_currency!(code:, subunit:, symbol:, priority:)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
# Strictly registers a new currency, raising a KeyError if already registered.
|
|
5
|
+
# Registers a new currency, raising a KeyError if already registered.
|
|
40
6
|
#
|
|
41
7
|
# @param code [String] the unique currency code
|
|
42
|
-
# @param subunit [Integer] the decimal subunit precision
|
|
8
|
+
# @param subunit [Integer] the decimal subunit precision, defaults to 0
|
|
43
9
|
# @param symbol [String] the display symbol
|
|
44
10
|
# @param priority [Integer] parser precedence priority
|
|
45
11
|
# @return [Currency] the newly registered Currency instance
|
|
46
12
|
# @raise [ArgumentError] if the code contains invalid characters
|
|
47
13
|
# @raise [KeyError] if the currency code is already registered
|
|
48
|
-
def self.register_currency
|
|
14
|
+
def self.register_currency(code:, subunit: 0, symbol: '', priority: 0)
|
|
49
15
|
raise ArgumentError, 'Currency code must be String' unless code.is_a? String
|
|
50
16
|
unless code.match?(/^[A-Z_]+$/)
|
|
51
17
|
raise ArgumentError,
|
|
52
|
-
"Currency code must only letters or '_' ('USD',, 'MY_COIN')"
|
|
18
|
+
"Currency code must have only letters or '_' ('USD',, 'MY_COIN')"
|
|
53
19
|
end
|
|
54
20
|
|
|
55
|
-
currencies =
|
|
21
|
+
currencies = Registry.currencies
|
|
56
22
|
raise KeyError, "Currency: #{code} already registered" if currencies[code]
|
|
57
23
|
|
|
58
24
|
currency = currencies[code] = Currency.new(code:, subunit:, symbol:, priority:)
|
|
59
|
-
|
|
25
|
+
Registry.invalidate_symbols_cache
|
|
60
26
|
currency
|
|
61
27
|
end
|
|
62
28
|
|
|
@@ -66,6 +32,6 @@ module Mint
|
|
|
66
32
|
# @return [Array<Array<String, Currency>>] sorted symbol-to-currency mappings
|
|
67
33
|
# @api private
|
|
68
34
|
def self.currency_symbols
|
|
69
|
-
|
|
35
|
+
Registry.currency_symbols
|
|
70
36
|
end
|
|
71
37
|
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mint
|
|
4
|
+
module_function
|
|
5
|
+
|
|
6
|
+
# Loads ISO world currencies from YAML file into the registry.
|
|
7
|
+
#
|
|
8
|
+
# @return [Hash{String => Currency}] ISO-4217 world currencies mapped by code
|
|
9
|
+
# @api private
|
|
10
|
+
def world_currencies
|
|
11
|
+
@world_currencies ||= begin
|
|
12
|
+
path = File.join(File.expand_path('../data', __dir__), 'currencies.yaml')
|
|
13
|
+
|
|
14
|
+
YAML.load_file(path).to_h { |entry| [entry['code'], Currency.new(**entry.transform_keys(&:to_sym))] }
|
|
15
|
+
end.freeze
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/minting/mint.rb
CHANGED
|
@@ -2,5 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
require 'minting/mint/currency'
|
|
4
4
|
require 'minting/mint/currency_store'
|
|
5
|
+
require 'minting/mint/mint'
|
|
6
|
+
require 'minting/mint/parser'
|
|
5
7
|
require 'minting/mint/registry'
|
|
6
8
|
require 'minting/mint/refinements'
|
|
9
|
+
require 'minting/mint/world_currencies'
|
|
10
|
+
require 'minting/money/allocation'
|
|
11
|
+
require 'minting/money/arithmetics'
|
|
12
|
+
require 'minting/money/coercion'
|
|
13
|
+
require 'minting/money/comparable'
|
|
14
|
+
require 'minting/money/constructors'
|
|
15
|
+
require 'minting/money/conversion'
|
|
16
|
+
require 'minting/money/formatting'
|
|
17
|
+
require 'minting/money/money'
|
|
@@ -18,7 +18,7 @@ module Mint
|
|
|
18
18
|
raise ArgumentError, 'Proportions total must not be zero' if whole.zero?
|
|
19
19
|
|
|
20
20
|
subunit = currency.subunit
|
|
21
|
-
amounts = proportions.map { |rate| (amount * rate
|
|
21
|
+
amounts = proportions.map { |rate| Rational(amount * rate, whole).round(subunit) }
|
|
22
22
|
allocate_left_over!(amounts: amounts, left_over: amount - amounts.sum)
|
|
23
23
|
end
|
|
24
24
|
|
|
@@ -53,9 +53,7 @@ module Mint
|
|
|
53
53
|
# Unary negation operator. Returns a new {Money} instance with the inverted sign.
|
|
54
54
|
#
|
|
55
55
|
# @return [Money] negated Money instance
|
|
56
|
-
def -@
|
|
57
|
-
mint(-amount)
|
|
58
|
-
end
|
|
56
|
+
def -@ = mint(-amount)
|
|
59
57
|
|
|
60
58
|
# Performs multiplication of the monetary value by a standard scalar Numeric.
|
|
61
59
|
#
|
|
@@ -7,9 +7,11 @@ module Mint
|
|
|
7
7
|
|
|
8
8
|
# @return true if both are zero, or both have same amount and same currency
|
|
9
9
|
def ==(other)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
case other
|
|
11
|
+
when 0 then zero?
|
|
12
|
+
when Mint::Money then amount == other.amount && currency == other.currency
|
|
13
|
+
else false
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
def eql?(other)
|
|
@@ -5,7 +5,7 @@ module Mint
|
|
|
5
5
|
class Money
|
|
6
6
|
# Creates a new Money immutable object with the specified amount and currency
|
|
7
7
|
# @param amount [Numeric] The monetary amount
|
|
8
|
-
# @param currency [Currency] The currency object
|
|
8
|
+
# @param currency [Currency, String] The currency code or currency object
|
|
9
9
|
# @raise [ArgumentError] If amount is not numeric or currency is invalid
|
|
10
10
|
def self.create(amount, currency)
|
|
11
11
|
raise ArgumentError, 'amount must be Numeric' unless amount.is_a?(Numeric)
|
data/lib/minting/version.rb
CHANGED
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.
|
|
4
|
+
version: 1.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gilson Ferraz
|
|
@@ -44,9 +44,11 @@ files:
|
|
|
44
44
|
- lib/minting/mint.rb
|
|
45
45
|
- lib/minting/mint/currency.rb
|
|
46
46
|
- lib/minting/mint/currency_store.rb
|
|
47
|
+
- lib/minting/mint/mint.rb
|
|
48
|
+
- lib/minting/mint/parser.rb
|
|
47
49
|
- lib/minting/mint/refinements.rb
|
|
48
50
|
- lib/minting/mint/registry.rb
|
|
49
|
-
- lib/minting/
|
|
51
|
+
- lib/minting/mint/world_currencies.rb
|
|
50
52
|
- lib/minting/money/allocation.rb
|
|
51
53
|
- lib/minting/money/arithmetics.rb
|
|
52
54
|
- lib/minting/money/coercion.rb
|
|
@@ -55,7 +57,6 @@ files:
|
|
|
55
57
|
- lib/minting/money/conversion.rb
|
|
56
58
|
- lib/minting/money/formatting.rb
|
|
57
59
|
- lib/minting/money/money.rb
|
|
58
|
-
- lib/minting/money/parse.rb
|
|
59
60
|
- lib/minting/version.rb
|
|
60
61
|
- minting.gemspec
|
|
61
62
|
homepage: https://github.com/gferraz/minting
|
data/lib/minting/money/parse.rb
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Mint
|
|
4
|
-
# Money parser
|
|
5
|
-
class Money
|
|
6
|
-
# Parses a human-readable money string into a {Money} object.
|
|
7
|
-
#
|
|
8
|
-
# @param input [String] Amount input, optionally including a currency symbol or code
|
|
9
|
-
# @param currency [String, Symbol, Currency, nil] ISO code when not present in +input+
|
|
10
|
-
# @return [Money]
|
|
11
|
-
# @raise [ArgumentError] when +input+ is invalid or currency cannot be determined
|
|
12
|
-
#
|
|
13
|
-
# @example With explicit currency
|
|
14
|
-
# Money.parse('19.99', 'USD') #=> [USD 19.99]
|
|
15
|
-
# Money.parse('1.234,56', 'EUR') #=> [EUR 1234.56]
|
|
16
|
-
#
|
|
17
|
-
# @example With symbol or code in the string
|
|
18
|
-
# Money.parse('$19.99') #=> [USD 19.99]
|
|
19
|
-
# Money.parse('19,99 €') #=> [EUR 19.99]
|
|
20
|
-
# Money.parse('USD 1,234.56') #=> [USD 1234.56]
|
|
21
|
-
def self.parse(input, currency = nil)
|
|
22
|
-
raise ArgumentError, 'input must be a String' unless input.is_a?(String)
|
|
23
|
-
|
|
24
|
-
input = input.strip
|
|
25
|
-
raise ArgumentError, 'input cannot be empty' if input.empty?
|
|
26
|
-
|
|
27
|
-
currency = parse_currency(currency) || parse_currency(input)
|
|
28
|
-
raise ArgumentError, "Currency [#{currency}] not registered" unless currency
|
|
29
|
-
|
|
30
|
-
amount = currency.normalize_amount(parse_amount(input))
|
|
31
|
-
new(amount, currency)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# Extracts a numeric value from input that should only contain an amount.
|
|
35
|
-
def self.parse_amount(input)
|
|
36
|
-
# Remove any charater that is not a digit, comma or period
|
|
37
|
-
numeric = input.scan(/[\d.,-]/).join
|
|
38
|
-
numeric = normalize_separators(numeric)
|
|
39
|
-
Rational(numeric)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# Converts locale-specific decimal/thousand separators into a plain decimal string.
|
|
43
|
-
def self.normalize_separators(numeric)
|
|
44
|
-
case [numeric.count(','), numeric.count('.')]
|
|
45
|
-
in [0, 0] | [0, 1] then numeric # Nothing to normalize (e.g. "1500" or "34.21").
|
|
46
|
-
in [1, 0] then numeric.tr(',', '.') # Only one comma: decimal (e.g. 19,99 or 1,234).
|
|
47
|
-
in [c, p] if c > 1 && p > 1 # Both separators appear multiple times
|
|
48
|
-
raise ArgumentError, "could not distinguish decimal and thousand separators in '#{numeric}'"
|
|
49
|
-
in [c, p] if c > 0 && p > 0 # Commas and dots: the rightmost one is the decimal separator.
|
|
50
|
-
if numeric.rindex(',') > numeric.rindex('.')
|
|
51
|
-
numeric.delete('.').tr(',', '.')
|
|
52
|
-
else
|
|
53
|
-
numeric.delete(',')
|
|
54
|
-
end
|
|
55
|
-
else # Multiple of the same separator only (e.g. 1,234,567) — all are thousands.
|
|
56
|
-
numeric.delete(',.')
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def self.parse_currency(input)
|
|
61
|
-
case input
|
|
62
|
-
when NilClass, Mint::Currency then return input
|
|
63
|
-
when String
|
|
64
|
-
# Prefer an explicit ISO 4217 code (e.g. "USD 1,234.56") over symbol matching.
|
|
65
|
-
currency = Mint.currency(input[/\b([A-Z]+)\b/, 1])
|
|
66
|
-
return currency if currency
|
|
67
|
-
|
|
68
|
-
# Fall back to registered symbols, longest first (HK$ before $).
|
|
69
|
-
Mint.currency_symbols.each do |symbol, currency|
|
|
70
|
-
return currency if input.include?(symbol)
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
raise ArgumentError, 'currency could not be detected; pass a currency code as the second argument'
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
private_class_method :parse_amount, :normalize_separators,
|
|
77
|
-
:parse_currency
|
|
78
|
-
end
|
|
79
|
-
end
|
data/lib/minting/money.rb
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'minting/money/parse'
|
|
4
|
-
require 'minting/money/allocation'
|
|
5
|
-
require 'minting/money/arithmetics'
|
|
6
|
-
require 'minting/money/coercion'
|
|
7
|
-
require 'minting/money/comparable'
|
|
8
|
-
require 'minting/money/constructors'
|
|
9
|
-
require 'minting/money/conversion'
|
|
10
|
-
require 'minting/money/formatting'
|
|
11
|
-
require 'minting/money/money'
|