latinum 1.7.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/latinum/bank.rb +14 -4
- data/lib/latinum/currencies/global.rb +13 -1
- data/lib/latinum/formatters.rb +17 -3
- data/lib/latinum/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d47aa25865b0ac4f8a71378e34f3ab3d2747cbcc53028aa39cf5db59ee7c283
|
4
|
+
data.tar.gz: 73244a39809482164e21800ac18a18831cd57c625e9a98d4036fc9a13e02ec71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af1494045eaecbf9ca00635bd7f9e02355554116d4227dc4fc8ef61326ab0be480b8891624352ad5f89639db482a0d7ed101e38ec47276523f93ab66d30059e1
|
7
|
+
data.tar.gz: f03166e50246ff382aa4423c3e1438d5fbfefb66f5da4c6cd043a40e7f64282e37eb1c27e424c0f290f9aa30f790746bf0d9813eec3b21818e5be65c3350f1e9
|
data/lib/latinum/bank.rb
CHANGED
@@ -124,18 +124,28 @@ module Latinum
|
|
124
124
|
parts = string.strip.split(/\s+/, 2)
|
125
125
|
|
126
126
|
if parts.size == 2
|
127
|
-
return
|
127
|
+
return parse_named_resource(parts[1], parts[0])
|
128
128
|
else
|
129
129
|
# Lookup the named symbol, e.g. '$', and get the highest priority name:
|
130
130
|
symbol = @symbols.fetch(string.gsub(/[\-\.,0-9]/, ''), []).last
|
131
131
|
|
132
132
|
if symbol
|
133
|
-
|
133
|
+
name = symbol.last.to_s
|
134
134
|
elsif default_name
|
135
|
-
|
135
|
+
name = default_name
|
136
136
|
else
|
137
|
-
raise ArgumentError
|
137
|
+
raise ArgumentError, "Could not parse #{string}, could not determine resource name!"
|
138
138
|
end
|
139
|
+
|
140
|
+
return parse_named_resource(name, string)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
private def parse_named_resource(name, value)
|
145
|
+
if formatter = @formatters[name]
|
146
|
+
return Resource.new(formatter.parse(value), name)
|
147
|
+
else
|
148
|
+
raise ArgumentError, "No formatter found for #{name}!"
|
139
149
|
end
|
140
150
|
end
|
141
151
|
|
@@ -76,7 +76,7 @@ module Latinum
|
|
76
76
|
:name => 'EUR',
|
77
77
|
:description => 'Euro',
|
78
78
|
:formatter => Formatters::DecimalCurrencyFormatter,
|
79
|
-
#:
|
79
|
+
#:delimiter => '.',
|
80
80
|
#:separator => ','
|
81
81
|
}
|
82
82
|
|
@@ -90,6 +90,18 @@ module Latinum
|
|
90
90
|
:formatter => Formatters::DecimalCurrencyFormatter
|
91
91
|
}
|
92
92
|
|
93
|
+
# @name Global[:BRL]
|
94
|
+
# @attribute [Hash] The Brazilian Real configuration.
|
95
|
+
Global[:BRL] = {
|
96
|
+
:precision => 2,
|
97
|
+
:symbol => 'R$',
|
98
|
+
:name => 'BRL',
|
99
|
+
:description => 'Brazilian Real',
|
100
|
+
:formatter => Formatters::DecimalCurrencyFormatter,
|
101
|
+
:delimiter => '.',
|
102
|
+
:separator => ','
|
103
|
+
}
|
104
|
+
|
93
105
|
# @name Global[:BTC]
|
94
106
|
# @attribute [Hash] The Bitcoin configuration.
|
95
107
|
Global[:BTC] = {
|
data/lib/latinum/formatters.rb
CHANGED
@@ -28,6 +28,12 @@ module Latinum
|
|
28
28
|
@name = name
|
29
29
|
end
|
30
30
|
|
31
|
+
# Parse a string into an amount.
|
32
|
+
# @returns [BigDecimal] The parsed amount.
|
33
|
+
def parse(string)
|
34
|
+
BigDecimal(string)
|
35
|
+
end
|
36
|
+
|
31
37
|
# Formats the amount using a general notation.
|
32
38
|
# e.g. "5.0 NZD".
|
33
39
|
# @returns [String] The formatted string.
|
@@ -55,7 +61,7 @@ module Latinum
|
|
55
61
|
def initialize(**options)
|
56
62
|
@symbol = options[:symbol] || '$'
|
57
63
|
@separator = options[:separator] || '.'
|
58
|
-
@
|
64
|
+
@delimiter = options[:delimiter] || ','
|
59
65
|
@places = options[:precision] || 2
|
60
66
|
@zero = options[:zero] || '0'
|
61
67
|
|
@@ -66,7 +72,15 @@ module Latinum
|
|
66
72
|
return amount.round(@places)
|
67
73
|
end
|
68
74
|
|
69
|
-
#
|
75
|
+
# Parse a string into an amount using the configured separator and delimiter.
|
76
|
+
# @returns [BigDecimal] The parsed amount.
|
77
|
+
def parse(string)
|
78
|
+
BigDecimal(
|
79
|
+
string.gsub(/[^\-0-9#{@separator}]/, '').gsub(@separator, '.')
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Formats the amount using the configured symbol, separator, delimiter, and places.
|
70
84
|
# e.g. "$5,000.00 NZD". Rounds the amount to the specified number of decimal places.
|
71
85
|
# @returns [String] The formatted string.
|
72
86
|
def format(amount, places: @places, **options)
|
@@ -87,7 +101,7 @@ module Latinum
|
|
87
101
|
groups.unshift(integral[0...remainder]) if remainder > 0
|
88
102
|
|
89
103
|
symbol = options.fetch(:symbol, @symbol)
|
90
|
-
value = "#{sign}#{symbol}#{groups.join(@
|
104
|
+
value = "#{sign}#{symbol}#{groups.join(@delimiter)}"
|
91
105
|
|
92
106
|
name = options.fetch(:name, @name)
|
93
107
|
suffix = name ? " #{name}" : ''
|
data/lib/latinum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: latinum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
requirements: []
|
103
|
-
rubygems_version: 3.1.
|
103
|
+
rubygems_version: 3.1.6
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
106
|
summary: Provides immutable resource and money computations.
|