tiny-sharp-rb 0.0.1
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 +7 -0
- data/money-7.0.2/CHANGELOG.md +843 -0
- data/money-7.0.2/LICENSE +23 -0
- data/money-7.0.2/README.md +626 -0
- data/money-7.0.2/config/currency_backwards_compatible.json +225 -0
- data/money-7.0.2/config/currency_iso.json +2750 -0
- data/money-7.0.2/config/currency_non_iso.json +146 -0
- data/money-7.0.2/lib/money/bank/base.rb +130 -0
- data/money-7.0.2/lib/money/bank/single_currency.rb +26 -0
- data/money-7.0.2/lib/money/bank/variable_exchange.rb +285 -0
- data/money-7.0.2/lib/money/currency/heuristics.rb +11 -0
- data/money-7.0.2/lib/money/currency/loader.rb +28 -0
- data/money-7.0.2/lib/money/currency.rb +477 -0
- data/money-7.0.2/lib/money/locale_backend/base.rb +9 -0
- data/money-7.0.2/lib/money/locale_backend/currency.rb +13 -0
- data/money-7.0.2/lib/money/locale_backend/errors.rb +8 -0
- data/money-7.0.2/lib/money/locale_backend/i18n.rb +28 -0
- data/money-7.0.2/lib/money/money/allocation.rb +87 -0
- data/money-7.0.2/lib/money/money/arithmetic.rb +346 -0
- data/money-7.0.2/lib/money/money/constructors.rb +86 -0
- data/money-7.0.2/lib/money/money/formatter.rb +361 -0
- data/money-7.0.2/lib/money/money/formatting_rules.rb +92 -0
- data/money-7.0.2/lib/money/money/locale_backend.rb +20 -0
- data/money-7.0.2/lib/money/money.rb +664 -0
- data/money-7.0.2/lib/money/rates_store/memory.rb +122 -0
- data/money-7.0.2/lib/money/version.rb +5 -0
- data/money-7.0.2/lib/money.rb +9 -0
- data/money-7.0.2/money.gemspec +32 -0
- data/tiny-sharp-rb.gemspec +12 -0
- metadata +69 -0
data/money-7.0.2/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2005 Tobias Lutke
|
|
4
|
+
Copyright (c) 2008 Phusion
|
|
5
|
+
Copyright (c) 2025 Shane Emmons
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
# RubyMoney - Money
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/money)
|
|
4
|
+
[](https://github.com/RubyMoney/money/actions/workflows/ruby.yml)
|
|
5
|
+
[](https://rubymoney.github.io/money/)
|
|
6
|
+
[](https://opensource.org/license/MIT)
|
|
7
|
+
|
|
8
|
+
⚠️ Please read the [upgrade guides](#upgrade-guides) before upgrading to a new major version.
|
|
9
|
+
|
|
10
|
+
If you miss String parsing, check out the new [monetize gem](https://github.com/RubyMoney/monetize).
|
|
11
|
+
|
|
12
|
+
## Contributing
|
|
13
|
+
|
|
14
|
+
See the [Contribution Guidelines](https://github.com/RubyMoney/money/blob/main/CONTRIBUTING.md)
|
|
15
|
+
|
|
16
|
+
## Introduction
|
|
17
|
+
|
|
18
|
+
A Ruby Library for dealing with money and currency conversion.
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
- Provides a `Money` class which encapsulates all information about a certain
|
|
23
|
+
amount of money, such as its value and its currency.
|
|
24
|
+
- Provides a `Money::Currency` class which encapsulates all information about
|
|
25
|
+
a monetary unit.
|
|
26
|
+
- Represents monetary values as integers, in cents. This avoids floating point
|
|
27
|
+
rounding errors.
|
|
28
|
+
- Represents currency as `Money::Currency` instances providing a high level of
|
|
29
|
+
flexibility.
|
|
30
|
+
- Provides APIs for exchanging money from one currency to another.
|
|
31
|
+
|
|
32
|
+
### Resources
|
|
33
|
+
|
|
34
|
+
- [Website](https://rubymoney.github.io/money/)
|
|
35
|
+
- [API Documentation](https://www.rubydoc.info/gems/money/frames)
|
|
36
|
+
- [Git Repository](https://github.com/RubyMoney/money)
|
|
37
|
+
|
|
38
|
+
### Notes
|
|
39
|
+
|
|
40
|
+
- Your app must use UTF-8 to function with this library. There are a
|
|
41
|
+
number of non-ASCII currency attributes.
|
|
42
|
+
|
|
43
|
+
## Downloading
|
|
44
|
+
|
|
45
|
+
Install stable releases with the following command:
|
|
46
|
+
|
|
47
|
+
gem install money
|
|
48
|
+
|
|
49
|
+
The development version (hosted on Github) can be installed with:
|
|
50
|
+
|
|
51
|
+
git clone git://github.com/RubyMoney/money.git
|
|
52
|
+
cd money
|
|
53
|
+
rake install
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
require 'money'
|
|
59
|
+
|
|
60
|
+
# explicitly define locales
|
|
61
|
+
I18n.config.available_locales = :en
|
|
62
|
+
Money.locale_backend = :i18n
|
|
63
|
+
|
|
64
|
+
# 10.00 USD
|
|
65
|
+
money = Money.from_cents(1000, "USD")
|
|
66
|
+
money.cents #=> 1000
|
|
67
|
+
money.currency #=> Currency.new("USD")
|
|
68
|
+
|
|
69
|
+
# Comparisons
|
|
70
|
+
Money.from_cents(1000, "USD") == Money.from_cents(1000, "USD") #=> true
|
|
71
|
+
Money.from_cents(1000, "USD") == Money.from_cents(100, "USD") #=> false
|
|
72
|
+
Money.from_cents(1000, "USD") == Money.from_cents(1000, "EUR") #=> false
|
|
73
|
+
Money.from_cents(1000, "USD") != Money.from_cents(1000, "EUR") #=> true
|
|
74
|
+
|
|
75
|
+
# Arithmetic
|
|
76
|
+
Money.from_cents(1000, "USD") + Money.from_cents(500, "USD") == Money.from_cents(1500, "USD")
|
|
77
|
+
Money.from_cents(1000, "USD") - Money.from_cents(200, "USD") == Money.from_cents(800, "USD")
|
|
78
|
+
Money.from_cents(1000, "USD") / 5 == Money.from_cents(200, "USD")
|
|
79
|
+
Money.from_cents(1000, "USD") * 5 == Money.from_cents(5000, "USD")
|
|
80
|
+
|
|
81
|
+
# Unit to subunit conversions
|
|
82
|
+
Money.from_amount(5, "USD") == Money.from_cents(500, "USD") # 5 USD
|
|
83
|
+
Money.from_amount(5, "JPY") == Money.from_cents(5, "JPY") # 5 JPY
|
|
84
|
+
Money.from_amount(5, "TND") == Money.from_cents(5000, "TND") # 5 TND
|
|
85
|
+
|
|
86
|
+
# Currency conversions
|
|
87
|
+
some_code_to_setup_exchange_rates
|
|
88
|
+
Money.from_cents(1000, "USD").exchange_to("EUR") == Money.from_cents(some_value, "EUR")
|
|
89
|
+
|
|
90
|
+
# Swap currency
|
|
91
|
+
Money.from_cents(1000, "USD").with_currency("EUR") == Money.from_cents(1000, "EUR")
|
|
92
|
+
|
|
93
|
+
# Formatting (see Formatting section for more options)
|
|
94
|
+
Money.from_cents(100, "USD").format #=> "$1.00"
|
|
95
|
+
Money.from_cents(100, "GBP").format #=> "£1.00"
|
|
96
|
+
Money.from_cents(100, "EUR").format #=> "€1.00"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Currency
|
|
100
|
+
|
|
101
|
+
Currencies are consistently represented as instances of `Money::Currency`.
|
|
102
|
+
The most part of `Money` APIs allows you to supply either a `String` or a
|
|
103
|
+
`Money::Currency`.
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
Money.from_cents(1000, "USD") == Money.from_cents(1000, Money::Currency.new("USD"))
|
|
107
|
+
Money.from_cents(1000, "EUR").currency == Money::Currency.new("EUR")
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
A `Money::Currency` instance holds all the information about the currency,
|
|
111
|
+
including the currency symbol, name and much more.
|
|
112
|
+
|
|
113
|
+
```ruby
|
|
114
|
+
currency = Money.from_cents(1000, "USD").currency
|
|
115
|
+
currency.iso_code #=> "USD"
|
|
116
|
+
currency.name #=> "United States Dollar"
|
|
117
|
+
currency.cents_based? #=> true
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
To define a new `Money::Currency` use `Money::Currency.register` as shown
|
|
121
|
+
below.
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
curr = {
|
|
125
|
+
priority: 1,
|
|
126
|
+
iso_code: "USD",
|
|
127
|
+
iso_numeric: "840",
|
|
128
|
+
name: "United States Dollar",
|
|
129
|
+
symbol: "$",
|
|
130
|
+
subunit: "Cent",
|
|
131
|
+
subunit_to_unit: 100,
|
|
132
|
+
decimal_mark: ".",
|
|
133
|
+
thousands_separator: ","
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
Money::Currency.register(curr)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The pre-defined set of attributes includes:
|
|
140
|
+
|
|
141
|
+
- `:priority` a numerical value you can use to sort/group the currency list
|
|
142
|
+
- `:iso_code` the international 3-letter code as defined by the ISO 4217 standard
|
|
143
|
+
- `:iso_numeric` the international 3-digit code as defined by the ISO 4217 standard
|
|
144
|
+
- `:name` the currency name
|
|
145
|
+
- `:symbol` the currency symbol (UTF-8 encoded)
|
|
146
|
+
- `:subunit` the name of the fractional monetary unit
|
|
147
|
+
- `:subunit_to_unit` the proportion between the unit and the subunit
|
|
148
|
+
- `:decimal_mark` character between the whole and fraction amounts
|
|
149
|
+
- `:thousands_separator` character between each thousands place
|
|
150
|
+
|
|
151
|
+
All attributes except `:iso_code` are optional. Some attributes, such as
|
|
152
|
+
`:symbol`, are used by the Money class to print out a representation of the
|
|
153
|
+
object. Other attributes, such as `:name` or `:priority`, exist to provide a
|
|
154
|
+
basic API you can take advantage of to build your application.
|
|
155
|
+
|
|
156
|
+
### :priority
|
|
157
|
+
|
|
158
|
+
The priority attribute is an arbitrary numerical value you can assign to the
|
|
159
|
+
`Money::Currency` and use in sorting/grouping operation.
|
|
160
|
+
|
|
161
|
+
For instance, let's assume your Rails application needs to render a currency
|
|
162
|
+
selector like the one available
|
|
163
|
+
[here](https://finance.yahoo.com/currency-converter/). You can create a couple of
|
|
164
|
+
custom methods to return the list of major currencies and all currencies as
|
|
165
|
+
follows:
|
|
166
|
+
|
|
167
|
+
```ruby
|
|
168
|
+
# Returns an array of currency id where
|
|
169
|
+
# priority < 10
|
|
170
|
+
def major_currencies(hash)
|
|
171
|
+
hash.inject([]) do |array, (id, attributes)|
|
|
172
|
+
priority = attributes[:priority]
|
|
173
|
+
if priority && priority < 10
|
|
174
|
+
array[priority] ||= []
|
|
175
|
+
array[priority] << id
|
|
176
|
+
end
|
|
177
|
+
array
|
|
178
|
+
end.compact.flatten
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# Returns an array of all currency id
|
|
182
|
+
def all_currencies(hash)
|
|
183
|
+
hash.keys
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
major_currencies(Money::Currency.table)
|
|
187
|
+
# => [:usd, :eur, :gbp, :aud, :cad, :jpy]
|
|
188
|
+
|
|
189
|
+
all_currencies(Money::Currency.table)
|
|
190
|
+
# => [:aed, :afn, :all, ...]
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Default Currency
|
|
194
|
+
|
|
195
|
+
A default currency is not set by default. If a default currency is not set, it will raise an error when you try to initialize a `Money` object without explicitly passing a currency or parse a string that does not contain a currency. You can set a default currency for your application by using:
|
|
196
|
+
|
|
197
|
+
```ruby
|
|
198
|
+
Money.default_currency = Money::Currency.new("CAD")
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
If you use [Rails](https://github.com/RubyMoney/money/tree/main#ruby-on-rails), then `config/initializers/money.rb` is a very good place to put this.
|
|
202
|
+
|
|
203
|
+
### Currency Exponent
|
|
204
|
+
|
|
205
|
+
The exponent of a money value is the number of digits after the decimal
|
|
206
|
+
separator (which separates the major unit from the minor unit). See e.g.
|
|
207
|
+
[ISO 4217](https://www.iso.org/iso-4217-currency-codes.html) for more
|
|
208
|
+
information. You can find the exponent (as an `Integer`) by
|
|
209
|
+
|
|
210
|
+
```ruby
|
|
211
|
+
Money::Currency.new("USD").exponent # => 2
|
|
212
|
+
Money::Currency.new("JPY").exponent # => 0
|
|
213
|
+
Money::Currency.new("MGA").exponent # => 1
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Currency Lookup
|
|
217
|
+
|
|
218
|
+
To find a given currency by ISO 4217 numeric code (three digits) you can do
|
|
219
|
+
|
|
220
|
+
```ruby
|
|
221
|
+
Money::Currency.find_by_iso_numeric(978) #=> Money::Currency.new(:eur)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Currency Exchange
|
|
225
|
+
|
|
226
|
+
Exchanging money is performed through an exchange bank object. The default
|
|
227
|
+
exchange bank object requires one to manually specify the exchange rate. Here's
|
|
228
|
+
an example of how it works:
|
|
229
|
+
|
|
230
|
+
```ruby
|
|
231
|
+
Money.add_rate("USD", "CAD", 1.24515)
|
|
232
|
+
Money.add_rate("CAD", "USD", 0.803115)
|
|
233
|
+
|
|
234
|
+
Money.us_dollar(100).exchange_to("CAD") # => Money.from_cents(124, "CAD")
|
|
235
|
+
Money.ca_dollar(100).exchange_to("USD") # => Money.from_cents(80, "USD")
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
Comparison and arithmetic operations work as expected:
|
|
239
|
+
|
|
240
|
+
```ruby
|
|
241
|
+
Money.from_cents(1000, "USD") <=> Money.from_cents(900, "USD") # => 1; 9.00 USD is smaller
|
|
242
|
+
Money.from_cents(1000, "EUR") + Money.from_cents(10, "EUR") == Money.from_cents(1010, "EUR")
|
|
243
|
+
|
|
244
|
+
Money.add_rate("USD", "EUR", 0.5)
|
|
245
|
+
Money.from_cents(1000, "EUR") + Money.from_cents(1000, "USD") == Money.from_cents(1500, "EUR")
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
### Exchange rate stores
|
|
249
|
+
|
|
250
|
+
The default bank is initialized with an in-memory store for exchange rates.
|
|
251
|
+
|
|
252
|
+
```ruby
|
|
253
|
+
Money.default_bank = Money::Bank::VariableExchange.new(Money::RatesStore::Memory.new)
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
You can pass your own store implementation, i.e. for storing and retrieving rates off a database, file, cache, etc.
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
Money.default_bank = Money::Bank::VariableExchange.new(MyCustomStore.new)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Stores must implement the following interface:
|
|
263
|
+
|
|
264
|
+
```ruby
|
|
265
|
+
# Add new exchange rate.
|
|
266
|
+
# @param [String] iso_from Currency ISO code. ex. 'USD'
|
|
267
|
+
# @param [String] iso_to Currency ISO code. ex. 'CAD'
|
|
268
|
+
# @param [Numeric] rate Exchange rate. ex. 0.0016
|
|
269
|
+
#
|
|
270
|
+
# @return [Numeric] rate.
|
|
271
|
+
def add_rate(iso_from, iso_to, rate); end
|
|
272
|
+
|
|
273
|
+
# Get rate. Must be idempotent. i.e. adding the same rate must not produce duplicates.
|
|
274
|
+
# @param [String] iso_from Currency ISO code. ex. 'USD'
|
|
275
|
+
# @param [String] iso_to Currency ISO code. ex. 'CAD'
|
|
276
|
+
#
|
|
277
|
+
# @return [Numeric] rate.
|
|
278
|
+
def get_rate(iso_from, iso_to); end
|
|
279
|
+
|
|
280
|
+
# Iterate over rate tuples (iso_from, iso_to, rate)
|
|
281
|
+
#
|
|
282
|
+
# @yieldparam iso_from [String] Currency ISO string.
|
|
283
|
+
# @yieldparam iso_to [String] Currency ISO string.
|
|
284
|
+
# @yieldparam rate [Numeric] Exchange rate.
|
|
285
|
+
#
|
|
286
|
+
# @return [Enumerator]
|
|
287
|
+
#
|
|
288
|
+
# @example
|
|
289
|
+
# store.each_rate do |iso_from, iso_to, rate|
|
|
290
|
+
# puts [iso_from, iso_to, rate].join
|
|
291
|
+
# end
|
|
292
|
+
def each_rate(&block); end
|
|
293
|
+
|
|
294
|
+
# Wrap store operations in a thread-safe transaction
|
|
295
|
+
# (or IO or Database transaction, depending on your implementation)
|
|
296
|
+
#
|
|
297
|
+
# @yield [n] Block that will be wrapped in transaction.
|
|
298
|
+
#
|
|
299
|
+
# @example
|
|
300
|
+
# store.transaction do
|
|
301
|
+
# store.add_rate('USD', 'CAD', 0.9)
|
|
302
|
+
# store.add_rate('USD', 'CLP', 0.0016)
|
|
303
|
+
# end
|
|
304
|
+
def transaction(&block); end
|
|
305
|
+
|
|
306
|
+
# Serialize store and its content to make Marshal.dump work.
|
|
307
|
+
#
|
|
308
|
+
# Returns an array with store class and any arguments needed to initialize the store in the current state.
|
|
309
|
+
|
|
310
|
+
# @return [Array] [class, arg1, arg2]
|
|
311
|
+
def marshal_dump; end
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
The following example implements an `ActiveRecord` store to save exchange rates to a database.
|
|
315
|
+
|
|
316
|
+
```ruby
|
|
317
|
+
# rails g model exchange_rate from:string to:string rate:float
|
|
318
|
+
|
|
319
|
+
class ExchangeRate < ApplicationRecord
|
|
320
|
+
def self.get_rate(from_iso_code, to_iso_code)
|
|
321
|
+
rate = find_by(from: from_iso_code, to: to_iso_code)
|
|
322
|
+
rate&.rate
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
def self.add_rate(from_iso_code, to_iso_code, rate)
|
|
326
|
+
exrate = find_or_initialize_by(from: from_iso_code, to: to_iso_code)
|
|
327
|
+
exrate.rate = rate
|
|
328
|
+
exrate.save!
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def self.each_rate
|
|
332
|
+
return find_each unless block_given?
|
|
333
|
+
|
|
334
|
+
find_each do |rate|
|
|
335
|
+
yield rate.from, rate.to, rate.rate
|
|
336
|
+
end
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def self.marshal_dump
|
|
340
|
+
[self]
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
The following example implements a `Redis` store to save exchange rates to a redis database.
|
|
346
|
+
|
|
347
|
+
```ruby
|
|
348
|
+
class RedisRateStore
|
|
349
|
+
INDEX_KEY_SEPARATOR = '_TO_'.freeze
|
|
350
|
+
|
|
351
|
+
# Using second db of the redis instance
|
|
352
|
+
# because sidekiq uses the first db
|
|
353
|
+
REDIS_DATABASE = 1
|
|
354
|
+
|
|
355
|
+
# Using Hash to store rates data
|
|
356
|
+
REDIS_STORE_KEY = 'rates'
|
|
357
|
+
|
|
358
|
+
def initialize
|
|
359
|
+
conn_url = "#{Rails.application.credentials.redis_server}/#{REDIS_DATABASE}"
|
|
360
|
+
@connection = Redis.new(url: conn_url)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
def add_rate(iso_from, iso_to, rate)
|
|
364
|
+
@connection.hset(REDIS_STORE_KEY, rate_key_for(iso_from, iso_to), rate)
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def get_rate(iso_from, iso_to)
|
|
368
|
+
@connection.hget(REDIS_STORE_KEY, rate_key_for(iso_from, iso_to))
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
def each_rate
|
|
372
|
+
rates = @connection.hgetall(REDIS_STORE_KEY)
|
|
373
|
+
return to_enum(:each_rate) unless block_given?
|
|
374
|
+
|
|
375
|
+
rates.each do |key, rate|
|
|
376
|
+
iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR)
|
|
377
|
+
yield iso_from, iso_to, rate
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
def transaction
|
|
382
|
+
yield
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
private
|
|
386
|
+
|
|
387
|
+
def rate_key_for(iso_from, iso_to)
|
|
388
|
+
[iso_from, iso_to].join(INDEX_KEY_SEPARATOR).upcase
|
|
389
|
+
end
|
|
390
|
+
end
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
Now you can use it with the default bank.
|
|
394
|
+
|
|
395
|
+
```ruby
|
|
396
|
+
# For Rails 6 pass model name as a string to make it compatible with zeitwerk
|
|
397
|
+
# Money.default_bank = Money::Bank::VariableExchange.new("ExchangeRate")
|
|
398
|
+
Money.default_bank = Money::Bank::VariableExchange.new(ExchangeRate)
|
|
399
|
+
|
|
400
|
+
# Add to the underlying store
|
|
401
|
+
Money.default_bank.add_rate('USD', 'CAD', 0.9)
|
|
402
|
+
# Retrieve from the underlying store
|
|
403
|
+
Money.default_bank.get_rate('USD', 'CAD') # => 0.9
|
|
404
|
+
# Exchanging amounts just works.
|
|
405
|
+
Money.from_cents(1000, 'USD').exchange_to('CAD') #=> #<Money fractional:900 currency:CAD>
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
There is nothing stopping you from creating store objects which scrapes
|
|
409
|
+
[XE](https://www.xe.com) for the current rates or just returns `rand(2)`:
|
|
410
|
+
|
|
411
|
+
```ruby
|
|
412
|
+
Money.default_bank = Money::Bank::VariableExchange.new(StoreWhichScrapesXeDotCom.new)
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
You can also implement your own Bank to calculate exchanges differently.
|
|
416
|
+
Different banks can share Stores.
|
|
417
|
+
|
|
418
|
+
```ruby
|
|
419
|
+
Money.default_bank = MyCustomBank.new(Money::RatesStore::Memory.new)
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
If you wish to disable automatic currency conversion to prevent arithmetic when
|
|
423
|
+
currencies don't match:
|
|
424
|
+
|
|
425
|
+
```ruby
|
|
426
|
+
Money.disallow_currency_conversion!
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### Implementations
|
|
430
|
+
|
|
431
|
+
The following is a list of Money.gem compatible currency exchange rate
|
|
432
|
+
implementations.
|
|
433
|
+
|
|
434
|
+
- [eu_central_bank](https://github.com/RubyMoney/eu_central_bank)
|
|
435
|
+
- [google_currency](https://github.com/RubyMoney/google_currency)
|
|
436
|
+
- [currencylayer](https://github.com/askuratovsky/currencylayer)
|
|
437
|
+
- [nordea](https://github.com/matiaskorhonen/nordea)
|
|
438
|
+
- [nbrb_currency](https://github.com/slbug/nbrb_currency)
|
|
439
|
+
- [money-currencylayer-bank](https://github.com/phlegx/money-currencylayer-bank)
|
|
440
|
+
- [money-open-exchange-rates](https://github.com/spk/money-open-exchange-rates)
|
|
441
|
+
- [money-historical-bank](https://github.com/atwam/money-historical-bank)
|
|
442
|
+
- [russian_central_bank](https://github.com/rmustafin/russian_central_bank)
|
|
443
|
+
- [money-uphold-bank](https://github.com/subvisual/money-uphold-bank)
|
|
444
|
+
|
|
445
|
+
## Formatting
|
|
446
|
+
|
|
447
|
+
There are several formatting rules for when `Money#format` is called. For more information, check out the [formatting module source](https://github.com/RubyMoney/money/blob/main/lib/money/money/formatter.rb), or read the latest release's [rdoc version](https://www.rubydoc.info/gems/money/Money/Formatter).
|
|
448
|
+
|
|
449
|
+
If you wish to format money according to the EU's [Rules for expressing monetary units](https://style-guide.europa.eu/en/content/-/isg/topic?identifier=7.3.3-rules-for-expressing-monetary-units#id370303__id370303_PositionISO) in either English, Irish, Latvian or Maltese:
|
|
450
|
+
|
|
451
|
+
```ruby
|
|
452
|
+
m = Money.from_cents('123', :gbp) # => #<Money fractional:123 currency:GBP>
|
|
453
|
+
m.format(symbol: m.currency.to_s + ' ') # => "GBP 1.23"
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
If you would like to customize currency symbols to avoid ambiguity between currencies, you can:
|
|
457
|
+
|
|
458
|
+
```ruby
|
|
459
|
+
Money::Currency.table[:hkd][:symbol] = 'HK$'
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
## Rounding
|
|
463
|
+
|
|
464
|
+
By default, `Money` objects are rounded to the nearest cent and the additional precision is not preserved:
|
|
465
|
+
|
|
466
|
+
```ruby
|
|
467
|
+
Money.from_amount(2.34567).format #=> "$2.35"
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
To retain the additional precision, you will also need to set `infinite_precision` to `true`.
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
Money.default_infinite_precision = true
|
|
474
|
+
Money.from_amount(2.34567).format #=> "$2.34567"
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
To round to the nearest cent (or anything more precise), you can use the `round` method. However, note that the `round` method on a `Money` object does not work the same way as a normal Ruby `Float` object. Money's `round` method accepts different arguments. The first argument to the round method is the rounding mode, while the second argument is the level of precision relative to the cent.
|
|
478
|
+
|
|
479
|
+
```ruby
|
|
480
|
+
# Float
|
|
481
|
+
2.34567.round #=> 2
|
|
482
|
+
2.34567.round(2) #=> 2.35
|
|
483
|
+
|
|
484
|
+
# Money
|
|
485
|
+
Money.default_infinite_precision = true
|
|
486
|
+
Money.from_cents(2.34567).format #=> "$0.0234567"
|
|
487
|
+
Money.from_cents(2.34567).round.format #=> "$0.02"
|
|
488
|
+
Money.from_cents(2.34567).round(BigDecimal::ROUND_DOWN, 2).format #=> "$0.0234"
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
You can set the default rounding mode by passing one of the `BigDecimal` mode enumerables like so:
|
|
492
|
+
|
|
493
|
+
```ruby
|
|
494
|
+
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
See [BigDecimal::ROUND_MODE](https://ruby-doc.org/3.4.1/gems/bigdecimal/BigDecimal.html#ROUND_MODE) for more information.
|
|
498
|
+
|
|
499
|
+
To round to the nearest cash value in currencies without small denominations:
|
|
500
|
+
|
|
501
|
+
```ruby
|
|
502
|
+
Money.from_cents(11_11, "CHF").to_nearest_cash_value.format # => "CHF 11.10"
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
## Ruby on Rails
|
|
506
|
+
|
|
507
|
+
To integrate money in a Rails application use [money-rails](https://github.com/RubyMoney/money-rails).
|
|
508
|
+
|
|
509
|
+
For deprecated methods of integrating with Rails, check [the wiki](https://github.com/RubyMoney/money/wiki).
|
|
510
|
+
|
|
511
|
+
## Localization
|
|
512
|
+
|
|
513
|
+
In order to localize formatting you can use `I18n` gem:
|
|
514
|
+
|
|
515
|
+
```ruby
|
|
516
|
+
Money.locale_backend = :i18n
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
With this enabled a thousands separator and a decimal mark will get looked up in your `I18n` translation files. In a Rails application this may look like:
|
|
520
|
+
|
|
521
|
+
```yml
|
|
522
|
+
# config/locale/en.yml
|
|
523
|
+
en:
|
|
524
|
+
number:
|
|
525
|
+
currency:
|
|
526
|
+
format:
|
|
527
|
+
delimiter: ","
|
|
528
|
+
separator: "."
|
|
529
|
+
# falling back to
|
|
530
|
+
number:
|
|
531
|
+
format:
|
|
532
|
+
delimiter: ","
|
|
533
|
+
separator: "."
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
For this example `Money.from_cents(123456789, "SEK").format` will return `1,234,567.89
|
|
537
|
+
kr` which otherwise would have returned `1 234 567,89 kr`.
|
|
538
|
+
|
|
539
|
+
This will work seamlessly with [rails-i18n](https://github.com/svenfuchs/rails-i18n) gem that already has a lot of locales defined.
|
|
540
|
+
|
|
541
|
+
If you wish to disable this feature and use defaults instead:
|
|
542
|
+
|
|
543
|
+
```ruby
|
|
544
|
+
Money.locale_backend = nil
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
### Deprecation
|
|
548
|
+
|
|
549
|
+
The current default behaviour always checks the I18n locale first, falling back to "per currency"
|
|
550
|
+
localization. This is now deprecated and will be removed in favour of explicitly defined behaviour
|
|
551
|
+
in the next major release.
|
|
552
|
+
|
|
553
|
+
If you would like to use I18n localization (formatting depends on the locale):
|
|
554
|
+
|
|
555
|
+
```ruby
|
|
556
|
+
Money.locale_backend = :i18n
|
|
557
|
+
|
|
558
|
+
# example (using default localization from rails-i18n):
|
|
559
|
+
I18n.locale = :en
|
|
560
|
+
Money.from_cents(10_000_00, 'USD').format # => $10,000.00
|
|
561
|
+
Money.from_cents(10_000_00, 'EUR').format # => €10,000.00
|
|
562
|
+
|
|
563
|
+
I18n.locale = :es
|
|
564
|
+
Money.from_cents(10_000_00, 'USD').format # => $10.000,00
|
|
565
|
+
Money.from_cents(10_000_00, 'EUR').format # => €10.000,00
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
If you need to localize the position of the currency symbol, you
|
|
569
|
+
have to pass it manually. *Note: this will become the default formatting
|
|
570
|
+
behavior in the next version.*
|
|
571
|
+
|
|
572
|
+
```ruby
|
|
573
|
+
I18n.locale = :fr
|
|
574
|
+
format = I18n.t :format, scope: 'number.currency.format'
|
|
575
|
+
Money.from_cents(10_00, 'EUR').format(format: format) # => 10,00 €
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
For the legacy behaviour of "per currency" localization (formatting depends only on currency):
|
|
579
|
+
|
|
580
|
+
```ruby
|
|
581
|
+
Money.locale_backend = :currency
|
|
582
|
+
|
|
583
|
+
# example:
|
|
584
|
+
Money.from_cents(10_000_00, 'USD').format # => $10,000.00
|
|
585
|
+
Money.from_cents(10_000_00, 'EUR').format # => €10.000,00
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
In case you don't need localization and would like to use default values (can be redefined using
|
|
589
|
+
`Money.default_formatting_rules`):
|
|
590
|
+
|
|
591
|
+
```ruby
|
|
592
|
+
Money.locale_backend = nil
|
|
593
|
+
|
|
594
|
+
# example:
|
|
595
|
+
Money.from_cents(10_000_00, 'USD').format # => $10000.00
|
|
596
|
+
Money.from_cents(10_000_00, 'EUR').format # => €10000.00
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
## Collection
|
|
600
|
+
|
|
601
|
+
In case you're working with collections of `Money` instances, have a look at [money-collection](https://github.com/RubyMoney/money-collection)
|
|
602
|
+
for improved performance and accuracy.
|
|
603
|
+
|
|
604
|
+
### Troubleshooting
|
|
605
|
+
|
|
606
|
+
If you don't have some locale and don't want to get a runtime error such as:
|
|
607
|
+
|
|
608
|
+
I18n::InvalidLocale: :en is not a valid locale
|
|
609
|
+
|
|
610
|
+
Set the following:
|
|
611
|
+
```ruby
|
|
612
|
+
I18n.enforce_available_locales = false
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
## Heuristics
|
|
616
|
+
|
|
617
|
+
Prior to v6.9.0 heuristic analysis of string input was part of this gem. Since then it was extracted in to [money-heuristics gem](https://github.com/RubyMoney/money-heuristics).
|
|
618
|
+
|
|
619
|
+
## Upgrade Guides
|
|
620
|
+
|
|
621
|
+
When upgrading between major versions, please refer to the appropriate upgrade guide:
|
|
622
|
+
|
|
623
|
+
- [Upgrading to 7.0](https://github.com/RubyMoney/money/blob/main/UPGRADING-7.0.md) - Guide for migrating from 6.x to 7.0
|
|
624
|
+
- [Upgrading to 6.0](https://github.com/RubyMoney/money/blob/main/UPGRADING-6.0.md) - Guide for upgrading to version 6.0
|
|
625
|
+
|
|
626
|
+
These guides provide detailed information about breaking changes, new features, and step-by-step migration instructions.
|