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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c055e958ac724d131bdbb261f18f6f8b6e6f475a77d633710077a0722c19ffc
4
- data.tar.gz: 258a4c2c4233bc9e030a9e5cec7b685c0f7563bb012427809a6c6901067f69f1
3
+ metadata.gz: 1d47aa25865b0ac4f8a71378e34f3ab3d2747cbcc53028aa39cf5db59ee7c283
4
+ data.tar.gz: 73244a39809482164e21800ac18a18831cd57c625e9a98d4036fc9a13e02ec71
5
5
  SHA512:
6
- metadata.gz: cd104cf3d2664d37fde830088298544795148a782ff3ad37f9265acfbbac0d1e159d5aba27cbb8dfbac1679f25771fb0a26e902835f3b3417c465defa117d3ea
7
- data.tar.gz: 6891feee50ec8cb81c7d6f99857ad555d8ad283a1d3729efdf4ed8f92204a3699beae4ef457e06d6e905d7890f5cca6464beb4f92c552da0853682ba3e671da5
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 Resource.new(parts[0].gsub(/[^\-\.0-9]/, ''), parts[1])
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
- return Resource.new(string.gsub(/[^\-\.0-9]/, ''), symbol.last.to_s)
133
+ name = symbol.last.to_s
134
134
  elsif default_name
135
- return Resource.new(string.gsub(/[^\-\.0-9]/, ''), default_name.to_s)
135
+ name = default_name
136
136
  else
137
- raise ArgumentError.new("Could not parse #{string}, could not determine currency!")
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
- #:delimeter => '.',
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] = {
@@ -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
- @delimeter = options[:delimeter] || ','
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
- # Formats the amount using the configured symbol, separator, delimeter, and places.
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(@delimeter)}"
104
+ value = "#{sign}#{symbol}#{groups.join(@delimiter)}"
91
105
 
92
106
  name = options.fetch(:name, @name)
93
107
  suffix = name ? " #{name}" : ''
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Latinum
24
- VERSION = "1.7.0"
24
+ VERSION = "1.7.1"
25
25
  end
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.0
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-03-14 00:00:00.000000000 Z
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.2
103
+ rubygems_version: 3.1.6
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Provides immutable resource and money computations.