money 7.0.2 → 7.1.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/CHANGELOG.md +9 -0
- data/README.md +1 -0
- data/config/currency_iso.json +4 -4
- data/lib/money/bank/base.rb +3 -7
- data/lib/money/bank/single_currency.rb +2 -3
- data/lib/money/bank/variable_exchange.rb +34 -37
- data/lib/money/currency/heuristics.rb +1 -1
- data/lib/money/currency/loader.rb +1 -1
- data/lib/money/currency.rb +53 -32
- data/lib/money/locale_backend/base.rb +1 -1
- data/lib/money/locale_backend/currency.rb +1 -1
- data/lib/money/locale_backend/i18n.rb +6 -4
- data/lib/money/money/allocation.rb +10 -9
- data/lib/money/money/arithmetic.rb +34 -25
- data/lib/money/money/constructors.rb +5 -6
- data/lib/money/money/formatter.rb +25 -20
- data/lib/money/money/formatting_rules.rb +4 -2
- data/lib/money/money/locale_backend.rb +4 -4
- data/lib/money/money.rb +25 -30
- data/lib/money/rates_store/memory.rb +5 -8
- data/lib/money/version.rb +1 -1
- data/money.gemspec +7 -7
- data/sig/lib/money/bank/base.rbs +117 -0
- data/sig/lib/money/bank/single_currency.rbs +15 -0
- data/sig/lib/money/bank/variable_exchange.rbs +198 -0
- data/sig/lib/money/currency/heuristics.rbs +8 -0
- data/sig/lib/money/currency.rbs +322 -0
- data/sig/lib/money/locale_backend/base.rbs +6 -0
- data/sig/lib/money/locale_backend/errors.rbs +9 -0
- data/sig/lib/money/locale_backend/i18n.rbs +11 -0
- data/sig/lib/money/money/allocation.rbs +24 -0
- data/sig/lib/money/money/arithmetic.rbs +225 -0
- data/sig/lib/money/money/constructors.rbs +73 -0
- data/sig/lib/money/money/formatter.rbs +214 -0
- data/sig/lib/money/money/formatting_rules.rbs +32 -0
- data/sig/lib/money/money/locale_backend.rbs +7 -0
- data/sig/lib/money/money.rbs +522 -0
- data/sig/lib/money/rates_store/memory.rbs +90 -0
- data/sig/manifest.yaml +10 -0
- metadata +20 -6
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
# "Money is any object or record that is generally accepted as payment for
|
|
2
|
+
# goods and services and repayment of debts in a given socio-economic context
|
|
3
|
+
# or country." -Wikipedia
|
|
4
|
+
#
|
|
5
|
+
# An instance of Money represents an amount of a specific currency.
|
|
6
|
+
#
|
|
7
|
+
# Money is a value object and should be treated as immutable.
|
|
8
|
+
#
|
|
9
|
+
# @see https://en.wikipedia.org/wiki/Money
|
|
10
|
+
class Money
|
|
11
|
+
include Comparable
|
|
12
|
+
|
|
13
|
+
include Money::Arithmetic
|
|
14
|
+
|
|
15
|
+
extend Constructors
|
|
16
|
+
|
|
17
|
+
# Raised when smallest denomination of a currency is not defined
|
|
18
|
+
class UndefinedSmallestDenomination < StandardError
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
@default_currency: Money::Currency
|
|
22
|
+
@fractional: BigDecimal
|
|
23
|
+
|
|
24
|
+
# Convenience method for fractional part of the amount. Synonym of #fractional
|
|
25
|
+
#
|
|
26
|
+
# @return [Integer] when Money.default_infinite_precision is false
|
|
27
|
+
# @return [BigDecimal] when Money.default_infinite_precision is true
|
|
28
|
+
#
|
|
29
|
+
# @see Money.default_infinite_precision
|
|
30
|
+
def cents: () -> (Integer | BigDecimal)
|
|
31
|
+
|
|
32
|
+
# The value of the monetary amount represented in the fractional or subunit
|
|
33
|
+
# of the currency.
|
|
34
|
+
#
|
|
35
|
+
# For example, in the US dollar currency the fractional unit is cents, and
|
|
36
|
+
# there are 100 cents in one US dollar. So given the Money representation of
|
|
37
|
+
# one US dollar, the fractional interpretation is 100.
|
|
38
|
+
#
|
|
39
|
+
# Another example is that of the Kuwaiti dinar. In this case the fractional
|
|
40
|
+
# unit is the fils and there 1000 fils to one Kuwaiti dinar. So given the
|
|
41
|
+
# Money representation of one Kuwaiti dinar, the fractional interpretation is
|
|
42
|
+
# 1000.
|
|
43
|
+
#
|
|
44
|
+
# @return [Integer] when Money.default_infinite_precision is false
|
|
45
|
+
# @return [BigDecimal] when Money.default_infinite_precision is true
|
|
46
|
+
#
|
|
47
|
+
# @see Money.default_infinite_precision
|
|
48
|
+
def fractional: () -> (Integer | BigDecimal)
|
|
49
|
+
|
|
50
|
+
# Round a given amount of money to the nearest possible amount in cash value. For
|
|
51
|
+
# example, in Swiss franc (CHF), the smallest possible amount of cash value is
|
|
52
|
+
# CHF 0.05. Therefore, this method rounds CHF 0.07 to CHF 0.05, and CHF 0.08 to
|
|
53
|
+
# CHF 0.10.
|
|
54
|
+
#
|
|
55
|
+
# @return [Integer] when Money.default_infinite_precision is false
|
|
56
|
+
# @return [BigDecimal] when Money.default_infinite_precision is true
|
|
57
|
+
#
|
|
58
|
+
# @see Money.default_infinite_precision
|
|
59
|
+
def round_to_nearest_cash_value: () -> (Integer | BigDecimal)
|
|
60
|
+
|
|
61
|
+
# Round a given amount of money to the nearest possible money in cash value.
|
|
62
|
+
# For example, in Swiss franc (CHF), the smallest possible amount of cash
|
|
63
|
+
# value is CHF 0.05. Therefore, this method rounds CHF 0.07 to CHF 0.05, and
|
|
64
|
+
# CHF 0.08 to CHF 0.10.
|
|
65
|
+
#
|
|
66
|
+
# @return [Money]
|
|
67
|
+
def to_nearest_cash_value: () -> Money
|
|
68
|
+
|
|
69
|
+
attr_reader currency: Currency
|
|
70
|
+
|
|
71
|
+
attr_reader bank: untyped
|
|
72
|
+
|
|
73
|
+
# @!attribute [rw] default_bank
|
|
74
|
+
# Used to set a default bank for currency exchange.
|
|
75
|
+
#
|
|
76
|
+
# Each Money object is associated with a bank
|
|
77
|
+
# object, which is responsible for currency exchange. This property
|
|
78
|
+
# allows you to specify the default bank object. The default value for
|
|
79
|
+
# this property is an instance of +Bank::VariableExchange.+ It allows
|
|
80
|
+
# one to specify custom exchange rates.
|
|
81
|
+
#
|
|
82
|
+
# @return [Money::Bank::Base]
|
|
83
|
+
#
|
|
84
|
+
# @!attribute default_formatting_rules
|
|
85
|
+
# Used to define a default hash of rules for every time
|
|
86
|
+
# +Money#format+ is called. Rules provided on method call will be
|
|
87
|
+
# merged with the default ones. To overwrite a rule, just provide the
|
|
88
|
+
# intended value while calling +format+.
|
|
89
|
+
#
|
|
90
|
+
# @see Money::Formatter#initialize Money::Formatter for more details
|
|
91
|
+
#
|
|
92
|
+
# @example
|
|
93
|
+
# Money.default_formatting_rules = { display_free: true }
|
|
94
|
+
# Money.new(0, "USD").format # => "free"
|
|
95
|
+
# Money.new(0, "USD").format(display_free: false) # => "$0.00"
|
|
96
|
+
#
|
|
97
|
+
# @return [Hash]
|
|
98
|
+
#
|
|
99
|
+
# @!attribute [rw] default_infinite_precision
|
|
100
|
+
# @return [Boolean] Use this to enable infinite precision cents as the
|
|
101
|
+
# global default
|
|
102
|
+
#
|
|
103
|
+
# @!attribute [rw] conversion_precision
|
|
104
|
+
# Used to specify precision for converting Rational to BigDecimal
|
|
105
|
+
#
|
|
106
|
+
# @return [Integer]
|
|
107
|
+
attr_accessor self.default_formatting_rules: untyped
|
|
108
|
+
|
|
109
|
+
# @!attribute [rw] default_bank
|
|
110
|
+
# Used to set a default bank for currency exchange.
|
|
111
|
+
#
|
|
112
|
+
# Each Money object is associated with a bank
|
|
113
|
+
# object, which is responsible for currency exchange. This property
|
|
114
|
+
# allows you to specify the default bank object. The default value for
|
|
115
|
+
# this property is an instance of +Bank::VariableExchange.+ It allows
|
|
116
|
+
# one to specify custom exchange rates.
|
|
117
|
+
#
|
|
118
|
+
# @return [Money::Bank::Base]
|
|
119
|
+
#
|
|
120
|
+
# @!attribute default_formatting_rules
|
|
121
|
+
# Used to define a default hash of rules for every time
|
|
122
|
+
# +Money#format+ is called. Rules provided on method call will be
|
|
123
|
+
# merged with the default ones. To overwrite a rule, just provide the
|
|
124
|
+
# intended value while calling +format+.
|
|
125
|
+
#
|
|
126
|
+
# @see Money::Formatter#initialize Money::Formatter for more details
|
|
127
|
+
#
|
|
128
|
+
# @example
|
|
129
|
+
# Money.default_formatting_rules = { display_free: true }
|
|
130
|
+
# Money.new(0, "USD").format # => "free"
|
|
131
|
+
# Money.new(0, "USD").format(display_free: false) # => "$0.00"
|
|
132
|
+
#
|
|
133
|
+
# @return [Hash]
|
|
134
|
+
#
|
|
135
|
+
# @!attribute [rw] default_infinite_precision
|
|
136
|
+
# @return [Boolean] Use this to enable infinite precision cents as the
|
|
137
|
+
# global default
|
|
138
|
+
#
|
|
139
|
+
# @!attribute [rw] conversion_precision
|
|
140
|
+
# Used to specify precision for converting Rational to BigDecimal
|
|
141
|
+
#
|
|
142
|
+
# @return [Integer]
|
|
143
|
+
attr_accessor self.default_infinite_precision: untyped
|
|
144
|
+
|
|
145
|
+
# @!attribute [rw] default_bank
|
|
146
|
+
# Used to set a default bank for currency exchange.
|
|
147
|
+
#
|
|
148
|
+
# Each Money object is associated with a bank
|
|
149
|
+
# object, which is responsible for currency exchange. This property
|
|
150
|
+
# allows you to specify the default bank object. The default value for
|
|
151
|
+
# this property is an instance of +Bank::VariableExchange.+ It allows
|
|
152
|
+
# one to specify custom exchange rates.
|
|
153
|
+
#
|
|
154
|
+
# @return [Money::Bank::Base]
|
|
155
|
+
#
|
|
156
|
+
# @!attribute default_formatting_rules
|
|
157
|
+
# Used to define a default hash of rules for every time
|
|
158
|
+
# +Money#format+ is called. Rules provided on method call will be
|
|
159
|
+
# merged with the default ones. To overwrite a rule, just provide the
|
|
160
|
+
# intended value while calling +format+.
|
|
161
|
+
#
|
|
162
|
+
# @see Money::Formatter#initialize Money::Formatter for more details
|
|
163
|
+
#
|
|
164
|
+
# @example
|
|
165
|
+
# Money.default_formatting_rules = { display_free: true }
|
|
166
|
+
# Money.new(0, "USD").format # => "free"
|
|
167
|
+
# Money.new(0, "USD").format(display_free: false) # => "$0.00"
|
|
168
|
+
#
|
|
169
|
+
# @return [Hash]
|
|
170
|
+
#
|
|
171
|
+
# @!attribute [rw] default_infinite_precision
|
|
172
|
+
# @return [Boolean] Use this to enable infinite precision cents as the
|
|
173
|
+
# global default
|
|
174
|
+
#
|
|
175
|
+
# @!attribute [rw] conversion_precision
|
|
176
|
+
# Used to specify precision for converting Rational to BigDecimal
|
|
177
|
+
#
|
|
178
|
+
# @return [Integer]
|
|
179
|
+
attr_accessor self.conversion_precision: untyped
|
|
180
|
+
|
|
181
|
+
# @!attribute [rw] strict_eql_compare
|
|
182
|
+
# Use this to specify how +Money#eql?+ behaves. Opt-in to the new
|
|
183
|
+
# behavior by setting this to +true+ and disable warnings when comparing
|
|
184
|
+
# zero amounts with different currencies.
|
|
185
|
+
#
|
|
186
|
+
# @example
|
|
187
|
+
# Money.strict_eql_compare = false # (default)
|
|
188
|
+
# Money.new(0, "USD").eql?(Money.new(0, "EUR")) # => true
|
|
189
|
+
# # => [DEPRECATION] warning
|
|
190
|
+
#
|
|
191
|
+
# Money.strict_eql_compare = true
|
|
192
|
+
# Money.new(0, "USD").eql?(Money.new(0, "EUR")) # => false
|
|
193
|
+
#
|
|
194
|
+
# @return [Boolean]
|
|
195
|
+
#
|
|
196
|
+
# @see Money#eql
|
|
197
|
+
attr_accessor self.strict_eql_compare: untyped
|
|
198
|
+
|
|
199
|
+
attr_reader self.locale_backend: untyped
|
|
200
|
+
|
|
201
|
+
attr_writer self.default_bank: untyped
|
|
202
|
+
|
|
203
|
+
# @!attribute default_currency
|
|
204
|
+
# @return [Money::Currency] The default currency, which is used when
|
|
205
|
+
# +Money.new+ is called without an explicit currency argument. The
|
|
206
|
+
# default value is Currency.new("USD"). The value must be a valid
|
|
207
|
+
# +Money::Currency+ instance.
|
|
208
|
+
def self.default_currency: () -> Money::Currency
|
|
209
|
+
|
|
210
|
+
def self.default_currency=: (Money::Currency currency) -> Money::Currency
|
|
211
|
+
|
|
212
|
+
def self.default_bank: () -> untyped
|
|
213
|
+
|
|
214
|
+
def self.locale_backend=: (untyped value) -> untyped
|
|
215
|
+
|
|
216
|
+
# @attr_writer rounding_mode Use this to specify the rounding mode
|
|
217
|
+
def self.rounding_mode=: (untyped new_rounding_mode) -> untyped
|
|
218
|
+
|
|
219
|
+
def self.setup_defaults: () -> untyped
|
|
220
|
+
|
|
221
|
+
def self.inherited: (untyped base) -> untyped
|
|
222
|
+
|
|
223
|
+
# Use this to return the rounding mode.
|
|
224
|
+
#
|
|
225
|
+
# @return [BigDecimal::ROUND_MODE] rounding mode
|
|
226
|
+
def self.rounding_mode: () -> untyped
|
|
227
|
+
|
|
228
|
+
# Temporarily changes the rounding mode in a given block.
|
|
229
|
+
#
|
|
230
|
+
# @param [BigDecimal::ROUND_MODE] mode
|
|
231
|
+
#
|
|
232
|
+
# @yield The block within which rounding mode will be changed. Its return
|
|
233
|
+
# value will also be the return value of the whole method.
|
|
234
|
+
#
|
|
235
|
+
# @return [Object] block results
|
|
236
|
+
#
|
|
237
|
+
# @example
|
|
238
|
+
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_DOWN) do
|
|
239
|
+
# Money.new(1200) * BigDecimal('0.029')
|
|
240
|
+
# end
|
|
241
|
+
def self.with_rounding_mode: (untyped mode) { () -> untyped } -> untyped
|
|
242
|
+
|
|
243
|
+
# Temporarily changes the default bank in the current thread only
|
|
244
|
+
#
|
|
245
|
+
# @param [Money::Bank::Base] bank The bank to use within the block
|
|
246
|
+
# @yield The block within which the bank will be changed
|
|
247
|
+
# @return [Object] block results
|
|
248
|
+
#
|
|
249
|
+
# @example
|
|
250
|
+
# Money.with_bank(european_bank) do
|
|
251
|
+
# Money.new(100, "USD").exchange_to("EUR")
|
|
252
|
+
# end
|
|
253
|
+
def self.with_bank: (Money::Bank::Base bank) { () -> untyped } -> untyped
|
|
254
|
+
|
|
255
|
+
# Adds a new exchange rate to the default bank and return the rate.
|
|
256
|
+
#
|
|
257
|
+
# @param [Currency, String, Symbol] from_currency Currency to exchange from.
|
|
258
|
+
# @param [Currency, String, Symbol] to_currency Currency to exchange to.
|
|
259
|
+
# @param [Numeric] rate Rate to exchange with.
|
|
260
|
+
#
|
|
261
|
+
# @return [Numeric]
|
|
262
|
+
#
|
|
263
|
+
# @example
|
|
264
|
+
# Money.add_rate("USD", "CAD", 1.25) #=> 1.25
|
|
265
|
+
def self.add_rate: (
|
|
266
|
+
(Money::Currency | string | Symbol) from_currency,
|
|
267
|
+
(Money::Currency | string | Symbol) to_currency,
|
|
268
|
+
(Money::Currency | string | Symbol) rate)
|
|
269
|
+
-> Numeric
|
|
270
|
+
|
|
271
|
+
# Sets the default bank to be a SingleCurrency bank that raises on
|
|
272
|
+
# currency exchange. Useful when apps operate in a single currency at a time.
|
|
273
|
+
def self.disallow_currency_conversion!: () -> untyped
|
|
274
|
+
|
|
275
|
+
# Creates a new Money object of value given in the +unit+ of the given
|
|
276
|
+
# +currency+.
|
|
277
|
+
#
|
|
278
|
+
# @param [Numeric] amount The numerical value of the money.
|
|
279
|
+
# @param [Currency, String, Symbol] currency The currency format.
|
|
280
|
+
# @param [Hash] options Optional settings for the new Money instance
|
|
281
|
+
# @option [Money::Bank::*] :bank The exchange bank to use.
|
|
282
|
+
#
|
|
283
|
+
# @example
|
|
284
|
+
# Money.from_amount(23.45, "USD") # => #<Money fractional:2345 currency:USD>
|
|
285
|
+
# Money.from_amount(23.45, "JPY") # => #<Money fractional:23 currency:JPY>
|
|
286
|
+
#
|
|
287
|
+
# @return [Money]
|
|
288
|
+
#
|
|
289
|
+
# @see #initialize
|
|
290
|
+
def self.from_amount: (Numeric amount, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
|
|
291
|
+
|
|
292
|
+
# DEPRECATED.
|
|
293
|
+
#
|
|
294
|
+
# @see Money.from_amount
|
|
295
|
+
def self.from_dollars: (Numeric amount, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
|
|
296
|
+
|
|
297
|
+
alias self.from_cents self.new
|
|
298
|
+
|
|
299
|
+
# Creates a new Money object of value given in the
|
|
300
|
+
# +fractional unit+ of the given +currency+.
|
|
301
|
+
#
|
|
302
|
+
# Alternatively you can use the convenience
|
|
303
|
+
# methods like {Money.ca_dollar} and {Money.us_dollar}.
|
|
304
|
+
#
|
|
305
|
+
# @param [Object] obj Either the fractional value of the money,
|
|
306
|
+
# a Money object, or a currency. (If passed a currency as the first
|
|
307
|
+
# argument, a Money will be created in that currency with fractional value
|
|
308
|
+
# = 0.
|
|
309
|
+
# @param [Currency, String, Symbol] currency The currency format.
|
|
310
|
+
# @param [Hash] options Optional settings for the new Money instance
|
|
311
|
+
# @option [Money::Bank::*] :bank The exchange bank to use.
|
|
312
|
+
#
|
|
313
|
+
# @return [Money]
|
|
314
|
+
#
|
|
315
|
+
# @example
|
|
316
|
+
# Money.new(100) #=> #<Money @fractional=100 @currency="USD">
|
|
317
|
+
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
|
|
318
|
+
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
|
|
319
|
+
#
|
|
320
|
+
def initialize: (Object obj, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
|
|
321
|
+
|
|
322
|
+
# DEPRECATED.
|
|
323
|
+
#
|
|
324
|
+
# @see #amount
|
|
325
|
+
def dollars: () -> BigDecimal
|
|
326
|
+
|
|
327
|
+
# Returns the numerical value of the money.
|
|
328
|
+
#
|
|
329
|
+
# @return [BigDecimal]
|
|
330
|
+
#
|
|
331
|
+
# @example
|
|
332
|
+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
|
|
333
|
+
#
|
|
334
|
+
# @see #to_d
|
|
335
|
+
# @see #fractional
|
|
336
|
+
def amount: () -> BigDecimal
|
|
337
|
+
|
|
338
|
+
# Returns a Integer hash value based on the +fractional+ and +currency+ attributes
|
|
339
|
+
# in order to use functions like & (intersection), group_by, etc.
|
|
340
|
+
#
|
|
341
|
+
# @return [Integer]
|
|
342
|
+
#
|
|
343
|
+
# @example
|
|
344
|
+
# Money.new(100).hash #=> 908351
|
|
345
|
+
def hash: () -> int
|
|
346
|
+
|
|
347
|
+
# Uses +Currency#symbol+. If +nil+ is returned, defaults to "¤".
|
|
348
|
+
#
|
|
349
|
+
# @return [String]
|
|
350
|
+
#
|
|
351
|
+
# @example
|
|
352
|
+
# Money.new(100, "USD").symbol #=> "$"
|
|
353
|
+
def symbol: () -> string
|
|
354
|
+
|
|
355
|
+
# Common inspect function
|
|
356
|
+
#
|
|
357
|
+
# @return [String]
|
|
358
|
+
def inspect: () -> ::String
|
|
359
|
+
|
|
360
|
+
# Returns the amount of money as a string.
|
|
361
|
+
#
|
|
362
|
+
# @return [String]
|
|
363
|
+
#
|
|
364
|
+
# @example
|
|
365
|
+
# Money.ca_dollar(100).to_s #=> "1.00"
|
|
366
|
+
def to_s: () -> string
|
|
367
|
+
|
|
368
|
+
# Return the amount of money as a BigDecimal.
|
|
369
|
+
#
|
|
370
|
+
# @return [BigDecimal]
|
|
371
|
+
#
|
|
372
|
+
# @example
|
|
373
|
+
# Money.us_dollar(1_00).to_d #=> BigDecimal("1.00")
|
|
374
|
+
def to_d: () -> BigDecimal
|
|
375
|
+
|
|
376
|
+
# Return the amount of money as a Integer.
|
|
377
|
+
#
|
|
378
|
+
# @return [Integer]
|
|
379
|
+
#
|
|
380
|
+
# @example
|
|
381
|
+
# Money.us_dollar(1_00).to_i #=> 1
|
|
382
|
+
def to_i: () -> int
|
|
383
|
+
|
|
384
|
+
# Return the amount of money as a float. Floating points cannot guarantee
|
|
385
|
+
# precision. Therefore, this function should only be used when you no longer
|
|
386
|
+
# need to represent currency or working with another system that requires
|
|
387
|
+
# floats.
|
|
388
|
+
#
|
|
389
|
+
# @return [Float]
|
|
390
|
+
#
|
|
391
|
+
# @example
|
|
392
|
+
# Money.us_dollar(100).to_f #=> 1.0
|
|
393
|
+
def to_f: () -> Float
|
|
394
|
+
|
|
395
|
+
# Returns a new Money instance in a given currency leaving the amount intact
|
|
396
|
+
# and not performing currency conversion.
|
|
397
|
+
#
|
|
398
|
+
# @param [Currency, String, Symbol] new_currency Currency of the new object.
|
|
399
|
+
#
|
|
400
|
+
# @return [self]
|
|
401
|
+
def with_currency: ((Money::Currency | string | Symbol) new_currency) -> Money
|
|
402
|
+
|
|
403
|
+
# Conversion to +self+.
|
|
404
|
+
#
|
|
405
|
+
# @return [self]
|
|
406
|
+
def to_money: (?untyped? given_currency) -> Money
|
|
407
|
+
|
|
408
|
+
# Receive the amount of this money object in another Currency.
|
|
409
|
+
#
|
|
410
|
+
# @param [Currency, String, Symbol] other_currency Currency to exchange to.
|
|
411
|
+
#
|
|
412
|
+
# @yield [n] Optional block to use when rounding after exchanging one currency
|
|
413
|
+
# for another.
|
|
414
|
+
# @yieldparam [Float] n The resulting float after exchanging one currency for
|
|
415
|
+
# another.
|
|
416
|
+
# @yieldreturn [Integer]
|
|
417
|
+
#
|
|
418
|
+
# @return [Money]
|
|
419
|
+
#
|
|
420
|
+
# @example
|
|
421
|
+
# Money.new(2000, "USD").exchange_to("EUR")
|
|
422
|
+
# Money.new(2000, "USD").exchange_to("EUR") {|x| x.round}
|
|
423
|
+
# Money.new(2000, "USD").exchange_to(Currency.new("EUR"))
|
|
424
|
+
def exchange_to: ((Money::Currency | string | Symbol) other_currency) { () -> int } -> Money
|
|
425
|
+
|
|
426
|
+
# Receive a money object with the same amount as the current Money object
|
|
427
|
+
# in United States dollar.
|
|
428
|
+
#
|
|
429
|
+
# @return [Money]
|
|
430
|
+
#
|
|
431
|
+
# @example
|
|
432
|
+
# n = Money.new(100, "CAD").as_us_dollar
|
|
433
|
+
# n.currency #=> #<Money::Currency id: usd>
|
|
434
|
+
def as_us_dollar: () -> Money
|
|
435
|
+
|
|
436
|
+
# Receive a money object with the same amount as the current Money object
|
|
437
|
+
# in Canadian dollar.
|
|
438
|
+
#
|
|
439
|
+
# @return [Money]
|
|
440
|
+
#
|
|
441
|
+
# @example
|
|
442
|
+
# n = Money.new(100, "USD").as_ca_dollar
|
|
443
|
+
# n.currency #=> #<Money::Currency id: cad>
|
|
444
|
+
def as_ca_dollar: () -> Money
|
|
445
|
+
|
|
446
|
+
# Receive a money object with the same amount as the current Money object
|
|
447
|
+
# in euro.
|
|
448
|
+
#
|
|
449
|
+
# @return [Money]
|
|
450
|
+
#
|
|
451
|
+
# @example
|
|
452
|
+
# n = Money.new(100, "USD").as_euro
|
|
453
|
+
# n.currency #=> #<Money::Currency id: eur>
|
|
454
|
+
def as_euro: () -> Money
|
|
455
|
+
|
|
456
|
+
# Splits a given amount in parts without losing pennies. The left-over pennies will be
|
|
457
|
+
# distributed round-robin amongst the parties. This means that parts listed first will likely
|
|
458
|
+
# receive more pennies than ones listed later.
|
|
459
|
+
#
|
|
460
|
+
# Pass [2, 1, 1] as input to give twice as much to part1 as part2 or
|
|
461
|
+
# part3 which results in 50% of the cash to party1, 25% to part2, and 25% to part3. Passing a
|
|
462
|
+
# number instead of an array will split the amount evenly (without losing pennies when rounding).
|
|
463
|
+
#
|
|
464
|
+
# @param [Array<Numeric>, Numeric] parts how amount should be distributed to parts
|
|
465
|
+
#
|
|
466
|
+
# @return [Array<Money>]
|
|
467
|
+
#
|
|
468
|
+
# @example
|
|
469
|
+
# Money.new(5, "USD").allocate([3, 7]) #=> [Money.new(2), Money.new(3)]
|
|
470
|
+
# Money.new(100, "USD").allocate([1, 1, 1]) #=> [Money.new(34), Money.new(33), Money.new(33)]
|
|
471
|
+
# Money.new(100, "USD").allocate(2) #=> [Money.new(50), Money.new(50)]
|
|
472
|
+
# Money.new(100, "USD").allocate(3) #=> [Money.new(34), Money.new(33), Money.new(33)]
|
|
473
|
+
#
|
|
474
|
+
def allocate: ((Array[Numeric] | Numeric) parts) -> Array[Money]
|
|
475
|
+
|
|
476
|
+
alias split allocate
|
|
477
|
+
|
|
478
|
+
# Round the monetary amount to smallest unit of coinage.
|
|
479
|
+
#
|
|
480
|
+
# @note
|
|
481
|
+
# This method is only useful when operating with Money.default_infinite_precision turned
|
|
482
|
+
# on. Without Money.default_infinite_precision values are rounded to the smallest unit of
|
|
483
|
+
# coinage automatically.
|
|
484
|
+
#
|
|
485
|
+
# @return [Money]
|
|
486
|
+
#
|
|
487
|
+
# @example
|
|
488
|
+
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
|
|
489
|
+
#
|
|
490
|
+
# @see Money.default_infinite_precision
|
|
491
|
+
def round: (?untyped rounding_mode, ?::Integer rounding_precision) -> Money
|
|
492
|
+
|
|
493
|
+
# Creates a formatted price string according to several rules.
|
|
494
|
+
#
|
|
495
|
+
# @param [Hash] rules See {Money::Formatter Money::Formatter} for the list of formatting options
|
|
496
|
+
#
|
|
497
|
+
# @return [String]
|
|
498
|
+
#
|
|
499
|
+
def format: (*Hash[untyped, untyped] rules) -> string
|
|
500
|
+
|
|
501
|
+
# Returns a thousands separator according to the locale
|
|
502
|
+
#
|
|
503
|
+
# @return [String]
|
|
504
|
+
#
|
|
505
|
+
def thousands_separator: () -> string
|
|
506
|
+
|
|
507
|
+
# Returns a decimal mark according to the locale
|
|
508
|
+
#
|
|
509
|
+
# @return [String]
|
|
510
|
+
#
|
|
511
|
+
def decimal_mark: () -> string
|
|
512
|
+
|
|
513
|
+
def dup_with: (?::Hash[untyped, untyped] options) -> untyped
|
|
514
|
+
|
|
515
|
+
private
|
|
516
|
+
|
|
517
|
+
def as_d: (untyped num) -> untyped
|
|
518
|
+
|
|
519
|
+
def return_value: (untyped value) -> untyped
|
|
520
|
+
|
|
521
|
+
def locale_backend: () -> untyped
|
|
522
|
+
end
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
module RatesStore
|
|
3
|
+
# Class for thread-safe storage of exchange rate pairs.
|
|
4
|
+
# Used by instances of +Money::Bank::VariableExchange+.
|
|
5
|
+
#
|
|
6
|
+
# @example
|
|
7
|
+
# store = Money::RatesStore::Memory.new
|
|
8
|
+
# store.add_rate 'USD', 'CAD', 0.98
|
|
9
|
+
# store.get_rate 'USD', 'CAD' # => 0.98
|
|
10
|
+
# # iterates rates
|
|
11
|
+
# store.each_rate {|iso_from, iso_to, rate| puts "#{from} -> #{to}: #{rate}" }
|
|
12
|
+
class Memory
|
|
13
|
+
INDEX_KEY_SEPARATOR: string
|
|
14
|
+
|
|
15
|
+
# Initializes a new +Money::RatesStore::Memory+ object.
|
|
16
|
+
#
|
|
17
|
+
# @param [Hash] opts Optional store options.
|
|
18
|
+
# @option opts [Boolean] :without_mutex disables the usage of a mutex
|
|
19
|
+
# @param [Hash] rates Optional initial exchange rate data.
|
|
20
|
+
def initialize: (?::Hash[untyped, untyped] opts, ?::Hash[untyped, untyped] rates) -> void
|
|
21
|
+
|
|
22
|
+
# Registers a conversion rate and returns it. Uses +Mutex+ to synchronize data access.
|
|
23
|
+
#
|
|
24
|
+
# @param [String] currency_iso_from Currency to exchange from.
|
|
25
|
+
# @param [String] currency_iso_to Currency to exchange to.
|
|
26
|
+
# @param [Numeric] rate Rate to use when exchanging currencies.
|
|
27
|
+
#
|
|
28
|
+
# @return [Numeric]
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
31
|
+
# store = Money::RatesStore::Memory.new
|
|
32
|
+
# store.add_rate("USD", "CAD", 1.24515)
|
|
33
|
+
# store.add_rate("CAD", "USD", 0.803115)
|
|
34
|
+
def add_rate: (string currency_iso_from, string currency_iso_to, Numeric rate) -> Numeric
|
|
35
|
+
|
|
36
|
+
# Retrieve the rate for the given currencies. Uses +Mutex+ to synchronize data access.
|
|
37
|
+
# Delegates to +Money::RatesStore::Memory+
|
|
38
|
+
#
|
|
39
|
+
# @param [String] currency_iso_from Currency to exchange from.
|
|
40
|
+
# @param [String] currency_iso_to Currency to exchange to.
|
|
41
|
+
#
|
|
42
|
+
# @return [Numeric]
|
|
43
|
+
#
|
|
44
|
+
# @example
|
|
45
|
+
# store = Money::RatesStore::Memory.new
|
|
46
|
+
# store.add_rate("USD", "CAD", 1.24515)
|
|
47
|
+
#
|
|
48
|
+
# store.get_rate("USD", "CAD") #=> 1.24515
|
|
49
|
+
def get_rate: (string currency_iso_from, string currency_iso_to) -> Numeric
|
|
50
|
+
|
|
51
|
+
def marshal_dump: () -> untyped
|
|
52
|
+
|
|
53
|
+
# Wraps block execution in a thread-safe transaction
|
|
54
|
+
def transaction: () { () -> untyped } -> untyped
|
|
55
|
+
|
|
56
|
+
# Iterate over rate tuples (iso_from, iso_to, rate)
|
|
57
|
+
#
|
|
58
|
+
# @yieldparam iso_from [String] Currency ISO string.
|
|
59
|
+
# @yieldparam iso_to [String] Currency ISO string.
|
|
60
|
+
# @yieldparam rate [Numeric] Exchange rate.
|
|
61
|
+
#
|
|
62
|
+
# @return [Enumerator]
|
|
63
|
+
#
|
|
64
|
+
# @example
|
|
65
|
+
# store.each_rate do |iso_from, iso_to, rate|
|
|
66
|
+
# puts [iso_from, iso_to, rate].join
|
|
67
|
+
# end
|
|
68
|
+
def each_rate: () ?{ (string iso_from, string iso_to, Numeric rate) -> untyped } -> untyped
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
attr_reader rates: untyped
|
|
73
|
+
|
|
74
|
+
attr_reader options: untyped
|
|
75
|
+
|
|
76
|
+
attr_reader guard: untyped
|
|
77
|
+
|
|
78
|
+
# Return the rate hashkey for the given currencies.
|
|
79
|
+
#
|
|
80
|
+
# @param [String] currency_iso_from The currency to exchange from.
|
|
81
|
+
# @param [String] currency_iso_to The currency to exchange to.
|
|
82
|
+
#
|
|
83
|
+
# @return [String]
|
|
84
|
+
#
|
|
85
|
+
# @example
|
|
86
|
+
# rate_key_for("USD", "CAD") #=> "USD_TO_CAD"
|
|
87
|
+
def rate_key_for: (string currency_iso_from, string currency_iso_to) -> string
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
data/sig/manifest.yaml
ADDED
metadata
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: money
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 7.0
|
|
4
|
+
version: 7.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shane Emmons
|
|
8
8
|
- Anthony Dmitriyev
|
|
9
|
-
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
|
-
date:
|
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
13
12
|
dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
|
15
14
|
name: bigdecimal
|
|
@@ -74,6 +73,23 @@ files:
|
|
|
74
73
|
- lib/money/rates_store/memory.rb
|
|
75
74
|
- lib/money/version.rb
|
|
76
75
|
- money.gemspec
|
|
76
|
+
- sig/lib/money/bank/base.rbs
|
|
77
|
+
- sig/lib/money/bank/single_currency.rbs
|
|
78
|
+
- sig/lib/money/bank/variable_exchange.rbs
|
|
79
|
+
- sig/lib/money/currency.rbs
|
|
80
|
+
- sig/lib/money/currency/heuristics.rbs
|
|
81
|
+
- sig/lib/money/locale_backend/base.rbs
|
|
82
|
+
- sig/lib/money/locale_backend/errors.rbs
|
|
83
|
+
- sig/lib/money/locale_backend/i18n.rbs
|
|
84
|
+
- sig/lib/money/money.rbs
|
|
85
|
+
- sig/lib/money/money/allocation.rbs
|
|
86
|
+
- sig/lib/money/money/arithmetic.rbs
|
|
87
|
+
- sig/lib/money/money/constructors.rbs
|
|
88
|
+
- sig/lib/money/money/formatter.rbs
|
|
89
|
+
- sig/lib/money/money/formatting_rules.rbs
|
|
90
|
+
- sig/lib/money/money/locale_backend.rbs
|
|
91
|
+
- sig/lib/money/rates_store/memory.rbs
|
|
92
|
+
- sig/manifest.yaml
|
|
77
93
|
homepage: https://rubymoney.github.io/money
|
|
78
94
|
licenses:
|
|
79
95
|
- MIT
|
|
@@ -82,7 +98,6 @@ metadata:
|
|
|
82
98
|
source_code_uri: https://github.com/RubyMoney/money/
|
|
83
99
|
bug_tracker_uri: https://github.com/RubyMoney/money/issues
|
|
84
100
|
rubygems_mfa_required: 'true'
|
|
85
|
-
post_install_message:
|
|
86
101
|
rdoc_options: []
|
|
87
102
|
require_paths:
|
|
88
103
|
- lib
|
|
@@ -97,8 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
97
112
|
- !ruby/object:Gem::Version
|
|
98
113
|
version: '0'
|
|
99
114
|
requirements: []
|
|
100
|
-
rubygems_version:
|
|
101
|
-
signing_key:
|
|
115
|
+
rubygems_version: 4.0.10
|
|
102
116
|
specification_version: 4
|
|
103
117
|
summary: A Ruby Library for dealing with money and currency conversion.
|
|
104
118
|
test_files: []
|