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,24 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
class Allocation
|
|
3
|
+
# Allocates a specified amount into parts based on their proportions or distributes
|
|
4
|
+
# it evenly when the number of parts is specified numerically.
|
|
5
|
+
#
|
|
6
|
+
# The total of the allocated amounts will always equal the original amount.
|
|
7
|
+
#
|
|
8
|
+
# The parts can be specified as:
|
|
9
|
+
# Numeric — performs the split between a given number of parties evenly
|
|
10
|
+
# Array<Numeric> — allocates the amounts proportionally to the given array
|
|
11
|
+
#
|
|
12
|
+
# @param amount [Numeric] The total amount to be allocated.
|
|
13
|
+
# @param parts [Numeric, Array<Numeric>] Number of parts to split into or an array (proportions for allocation).
|
|
14
|
+
# @param decimal_cutoff [bool | Integer] Controls per-split precision:
|
|
15
|
+
# - When true, splits are truncated to whole amounts.
|
|
16
|
+
# - When an Integer N, splits are rounded to N decimal places.
|
|
17
|
+
# - When false or nil, no rounding/truncation is applied.
|
|
18
|
+
# Defaults to true (whole amounts).
|
|
19
|
+
#
|
|
20
|
+
# @return [Array[Numeric]] An array containing the allocated amounts.
|
|
21
|
+
# @raise [ArgumentError] If parts is empty or not provided.
|
|
22
|
+
def self.generate: (Numeric amount, (Numeric | Array[Numeric]) parts, ?(bool | Integer) decimal_cutoff) -> Array[Numeric]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
module Arithmetic
|
|
3
|
+
# Wrapper for coerced numeric values to distinguish
|
|
4
|
+
# when numeric was on the 1st place in operation.
|
|
5
|
+
CoercedNumeric: untyped
|
|
6
|
+
|
|
7
|
+
# Returns a money object with changed polarity.
|
|
8
|
+
#
|
|
9
|
+
# @return [Money]
|
|
10
|
+
#
|
|
11
|
+
# @example
|
|
12
|
+
# - Money.new(100) #=> #<Money @fractional=-100>
|
|
13
|
+
def -@: () -> Money
|
|
14
|
+
|
|
15
|
+
# Checks whether two Money objects have the same currency and the same
|
|
16
|
+
# amount. Checks against objects that are not Money or a subclass will
|
|
17
|
+
# always return false.
|
|
18
|
+
#
|
|
19
|
+
# @param [Money] other_money Value to compare with.
|
|
20
|
+
#
|
|
21
|
+
# @return [Boolean]
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# Money.new(1_00).eql?(Money.new(1_00)) #=> true
|
|
25
|
+
# Money.new(1_00).eql?(Money.new(1_01)) #=> false
|
|
26
|
+
# Money.new(1_00, "USD").eql?(Money.new(1_00, "GBP")) #=> false
|
|
27
|
+
# Money.new(0, "USD").eql?(Money.new(0, "EUR")) #=> false
|
|
28
|
+
# Money.new(1_00).eql?("1.00") #=> false
|
|
29
|
+
#
|
|
30
|
+
# @see Money.strict_eql_compare
|
|
31
|
+
def eql?: (Money other_money) -> bool
|
|
32
|
+
|
|
33
|
+
# Compares two Money objects. If money objects have a different currency it
|
|
34
|
+
# will attempt to convert the currency.
|
|
35
|
+
#
|
|
36
|
+
# @param [Money] other Value to compare with.
|
|
37
|
+
#
|
|
38
|
+
# @return [Integer]
|
|
39
|
+
#
|
|
40
|
+
# @raise [TypeError] when other object is not Money
|
|
41
|
+
#
|
|
42
|
+
def <=>: (Money other) -> Integer
|
|
43
|
+
|
|
44
|
+
# Uses Comparable's implementation but raises ArgumentError if non-zero
|
|
45
|
+
# numeric value is given.
|
|
46
|
+
def ==: ((Money | Numeric) other) -> bool
|
|
47
|
+
|
|
48
|
+
# Test if the amount is positive. Returns +true+ if the money amount is
|
|
49
|
+
# greater than 0, +false+ otherwise.
|
|
50
|
+
#
|
|
51
|
+
# @return [Boolean]
|
|
52
|
+
#
|
|
53
|
+
# @example
|
|
54
|
+
# Money.new(1).positive? #=> true
|
|
55
|
+
# Money.new(0).positive? #=> false
|
|
56
|
+
# Money.new(-1).positive? #=> false
|
|
57
|
+
def positive?: () -> bool
|
|
58
|
+
|
|
59
|
+
# Test if the amount is negative. Returns +true+ if the money amount is
|
|
60
|
+
# less than 0, +false+ otherwise.
|
|
61
|
+
#
|
|
62
|
+
# @return [Boolean]
|
|
63
|
+
#
|
|
64
|
+
# @example
|
|
65
|
+
# Money.new(-1).negative? #=> true
|
|
66
|
+
# Money.new(0).negative? #=> false
|
|
67
|
+
# Money.new(1).negative? #=> false
|
|
68
|
+
def negative?: () -> bool
|
|
69
|
+
|
|
70
|
+
# Returns a new Money object containing the sum of the two operands' monetary
|
|
71
|
+
# values. If +other_money+ has a different currency then its monetary value
|
|
72
|
+
# is automatically exchanged to this object's currency using +exchange_to+.
|
|
73
|
+
#
|
|
74
|
+
# @param [Money] other Other +Money+ object to add.
|
|
75
|
+
# @return [Money]
|
|
76
|
+
#
|
|
77
|
+
# @raise [TypeError] If +other+ is a non-zero Numeric.
|
|
78
|
+
#
|
|
79
|
+
# @example
|
|
80
|
+
# Money.new(100) + Money.new(100) #=> #<Money @fractional=200>
|
|
81
|
+
def +: ((Money | Numeric) other) -> Money
|
|
82
|
+
|
|
83
|
+
# Returns a new Money object containing the difference between the two
|
|
84
|
+
# operands' monetary values. If +other_money+ has a different currency then
|
|
85
|
+
# its monetary value is automatically exchanged to this object's currency
|
|
86
|
+
# using +exchange_to+.
|
|
87
|
+
#
|
|
88
|
+
# @param [Money] other Other +Money+ object to subtract.
|
|
89
|
+
# @return [Money]
|
|
90
|
+
#
|
|
91
|
+
# @raise [TypeError] If +other+ is a non-zero Numeric.
|
|
92
|
+
#
|
|
93
|
+
# @example
|
|
94
|
+
# Money.new(100) - Money.new(99) #=> #<Money @fractional=1>
|
|
95
|
+
def -: ((Money | Numeric) other) -> Money
|
|
96
|
+
|
|
97
|
+
# Multiplies the monetary value with the given number and returns a new
|
|
98
|
+
# +Money+ object with this monetary value and the same currency.
|
|
99
|
+
#
|
|
100
|
+
# Note that you can't multiply a Money object by an other +Money+ object.
|
|
101
|
+
#
|
|
102
|
+
# @param [Numeric] value Number to multiply by.
|
|
103
|
+
#
|
|
104
|
+
# @return [Money] The resulting money.
|
|
105
|
+
#
|
|
106
|
+
# @raise [TypeError] If +value+ is NOT a number.
|
|
107
|
+
#
|
|
108
|
+
# @example
|
|
109
|
+
# Money.new(100) * 2 #=> #<Money @fractional=200>
|
|
110
|
+
#
|
|
111
|
+
def *: (Numeric value) -> Money
|
|
112
|
+
|
|
113
|
+
# Divides the monetary value with the given number and returns a new +Money+
|
|
114
|
+
# object with this monetary value and the same currency.
|
|
115
|
+
# Can also divide by another +Money+ object to get a ratio.
|
|
116
|
+
#
|
|
117
|
+
# +Money/Numeric+ returns +Money+. +Money/Money+ returns +Float+.
|
|
118
|
+
#
|
|
119
|
+
# @param [Money, Numeric] value Number to divide by.
|
|
120
|
+
#
|
|
121
|
+
# @return [Money] The resulting money if you divide Money by a number.
|
|
122
|
+
# @return [Float] The resulting number if you divide Money by a Money.
|
|
123
|
+
#
|
|
124
|
+
# @example
|
|
125
|
+
# Money.new(100) / 10 #=> #<Money @fractional=10>
|
|
126
|
+
# Money.new(100) / Money.new(10) #=> 10.0
|
|
127
|
+
#
|
|
128
|
+
def /: ((Money | Numeric) value) -> (Money | Float)
|
|
129
|
+
|
|
130
|
+
# Synonym for +#/+.
|
|
131
|
+
#
|
|
132
|
+
# @param [Money, Numeric] value Number to divide by.
|
|
133
|
+
#
|
|
134
|
+
# @return [Money] The resulting money if you divide Money by a number.
|
|
135
|
+
# @return [Float] The resulting number if you divide Money by a Money.
|
|
136
|
+
#
|
|
137
|
+
# @see #/
|
|
138
|
+
#
|
|
139
|
+
def div: ((Money | Numeric) value) -> (Money | Float)
|
|
140
|
+
|
|
141
|
+
# Divide money by money or fixnum and return array containing quotient and
|
|
142
|
+
# modulus.
|
|
143
|
+
#
|
|
144
|
+
# @param [Money, Integer] val Number to divmod by.
|
|
145
|
+
#
|
|
146
|
+
# @return [Array<Money,Money>,Array<Integer,Money>]
|
|
147
|
+
#
|
|
148
|
+
# @example
|
|
149
|
+
# Money.new(100).divmod(9) #=> [#<Money @fractional=11>, #<Money @fractional=1>]
|
|
150
|
+
# Money.new(100).divmod(Money.new(9)) #=> [11, #<Money @fractional=1>]
|
|
151
|
+
def divmod: ((Money | Integer) val) -> Array[Money | Integer]
|
|
152
|
+
|
|
153
|
+
private
|
|
154
|
+
|
|
155
|
+
def divmod_money: (untyped val) -> ::Array[untyped]
|
|
156
|
+
|
|
157
|
+
def divmod_other: (untyped val) -> ::Array[untyped]
|
|
158
|
+
|
|
159
|
+
public
|
|
160
|
+
|
|
161
|
+
# Equivalent to +self.divmod(val)[1]+
|
|
162
|
+
#
|
|
163
|
+
# @param [Money, Integer] val Number take modulo with.
|
|
164
|
+
#
|
|
165
|
+
# @return [Money]
|
|
166
|
+
#
|
|
167
|
+
# @example
|
|
168
|
+
# Money.new(100).modulo(9) #=> #<Money @fractional=1>
|
|
169
|
+
# Money.new(100).modulo(Money.new(9)) #=> #<Money @fractional=1>
|
|
170
|
+
def modulo: ((Money | Integer) val) -> Money
|
|
171
|
+
|
|
172
|
+
# Synonym for +#modulo+.
|
|
173
|
+
#
|
|
174
|
+
# @param [Money, Integer] val Number take modulo with.
|
|
175
|
+
#
|
|
176
|
+
# @return [Money]
|
|
177
|
+
#
|
|
178
|
+
# @see #modulo
|
|
179
|
+
def %: ((Money | Integer) val) -> Money
|
|
180
|
+
|
|
181
|
+
# If different signs +self.modulo(val) - val+ otherwise +self.modulo(val)+
|
|
182
|
+
#
|
|
183
|
+
# @param [Money, Integer] val Number to rake remainder with.
|
|
184
|
+
#
|
|
185
|
+
# @return [Money]
|
|
186
|
+
#
|
|
187
|
+
# @example
|
|
188
|
+
# Money.new(100).remainder(9) #=> #<Money @fractional=1>
|
|
189
|
+
def remainder: ((Money | Integer) val) -> Money
|
|
190
|
+
|
|
191
|
+
# Return absolute value of self as a new Money object.
|
|
192
|
+
#
|
|
193
|
+
# @return [Money]
|
|
194
|
+
#
|
|
195
|
+
# @example
|
|
196
|
+
# Money.new(-100).abs #=> #<Money @fractional=100>
|
|
197
|
+
def abs: () -> Money
|
|
198
|
+
|
|
199
|
+
# Test if the money amount is zero.
|
|
200
|
+
#
|
|
201
|
+
# @return [Boolean]
|
|
202
|
+
#
|
|
203
|
+
# @example
|
|
204
|
+
# Money.new(100).zero? #=> false
|
|
205
|
+
# Money.new(0).zero? #=> true
|
|
206
|
+
def zero?: () -> bool
|
|
207
|
+
|
|
208
|
+
# Test if the money amount is non-zero. Returns this money object if it is
|
|
209
|
+
# non-zero, or nil otherwise, like +Numeric#nonzero?+.
|
|
210
|
+
#
|
|
211
|
+
# @return [Money, nil]
|
|
212
|
+
#
|
|
213
|
+
# @example
|
|
214
|
+
# Money.new(100).nonzero? #=> #<Money @fractional=100>
|
|
215
|
+
# Money.new(0).nonzero? #=> nil
|
|
216
|
+
def nonzero?: () -> Arithmetic?
|
|
217
|
+
|
|
218
|
+
# Used to make Money instance handle the operations when arguments order is reversed
|
|
219
|
+
# @return [Array]
|
|
220
|
+
#
|
|
221
|
+
# @example
|
|
222
|
+
# 2 * Money.new(10) #=> #<Money @fractional=20>
|
|
223
|
+
def coerce: (untyped other) -> ::Array[self | untyped]
|
|
224
|
+
end
|
|
225
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
module Constructors
|
|
3
|
+
# Create a new money object with value 0.
|
|
4
|
+
#
|
|
5
|
+
# @param [Currency, String, Symbol] currency The currency to use.
|
|
6
|
+
#
|
|
7
|
+
# @return [Money]
|
|
8
|
+
#
|
|
9
|
+
# @example
|
|
10
|
+
# Money.empty #=> #<Money @fractional=0>
|
|
11
|
+
def empty: (?(Money::Currency | string | Symbol) currency) -> Money
|
|
12
|
+
|
|
13
|
+
alias zero empty
|
|
14
|
+
|
|
15
|
+
# Creates a new Money object of the given value, using the Canadian
|
|
16
|
+
# dollar currency.
|
|
17
|
+
#
|
|
18
|
+
# @param [Integer] cents The cents value.
|
|
19
|
+
#
|
|
20
|
+
# @return [Money]
|
|
21
|
+
#
|
|
22
|
+
# @example
|
|
23
|
+
# n = Money.ca_dollar(100)
|
|
24
|
+
# n.cents #=> 100
|
|
25
|
+
# n.currency #=> #<Money::Currency id: cad>
|
|
26
|
+
def ca_dollar: (int cents) -> Money
|
|
27
|
+
|
|
28
|
+
alias cad ca_dollar
|
|
29
|
+
|
|
30
|
+
# Creates a new Money object of the given value, using the American dollar
|
|
31
|
+
# currency.
|
|
32
|
+
#
|
|
33
|
+
# @param [Integer] cents The cents value.
|
|
34
|
+
#
|
|
35
|
+
# @return [Money]
|
|
36
|
+
#
|
|
37
|
+
# @example
|
|
38
|
+
# n = Money.us_dollar(100)
|
|
39
|
+
# n.cents #=> 100
|
|
40
|
+
# n.currency #=> #<Money::Currency id: usd>
|
|
41
|
+
def us_dollar: (int cents) -> Money
|
|
42
|
+
|
|
43
|
+
alias usd us_dollar
|
|
44
|
+
|
|
45
|
+
# Creates a new Money object of the given value, using the Euro currency.
|
|
46
|
+
#
|
|
47
|
+
# @param [Integer] cents The cents value.
|
|
48
|
+
#
|
|
49
|
+
# @return [Money]
|
|
50
|
+
#
|
|
51
|
+
# @example
|
|
52
|
+
# n = Money.euro(100)
|
|
53
|
+
# n.cents #=> 100
|
|
54
|
+
# n.currency #=> #<Money::Currency id: eur>
|
|
55
|
+
def euro: (int cents) -> Money
|
|
56
|
+
|
|
57
|
+
alias eur euro
|
|
58
|
+
|
|
59
|
+
# Creates a new Money object of the given value, in British pounds.
|
|
60
|
+
#
|
|
61
|
+
# @param [Integer] pence The pence value.
|
|
62
|
+
#
|
|
63
|
+
# @return [Money]
|
|
64
|
+
#
|
|
65
|
+
# @example
|
|
66
|
+
# n = Money.pound_sterling(100)
|
|
67
|
+
# n.fractional #=> 100
|
|
68
|
+
# n.currency #=> #<Money::Currency id: gbp>
|
|
69
|
+
def pound_sterling: (int pence) -> Money
|
|
70
|
+
|
|
71
|
+
alias gbp pound_sterling
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
class Formatter
|
|
3
|
+
DEFAULTS: Hash[Symbol, string]
|
|
4
|
+
|
|
5
|
+
# Creates a formatted price string according to several rules.
|
|
6
|
+
#
|
|
7
|
+
# @param [Hash] rules The options used to format the string.
|
|
8
|
+
#
|
|
9
|
+
# @return [String]
|
|
10
|
+
#
|
|
11
|
+
# @option rules [Boolean, String] :display_free (false) Whether a zero
|
|
12
|
+
# amount of money should be formatted of "free" or as the supplied string.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# Money.us_dollar(0).format(display_free: true) #=> "free"
|
|
16
|
+
# Money.us_dollar(0).format(display_free: "gratis") #=> "gratis"
|
|
17
|
+
# Money.us_dollar(0).format #=> "$0.00"
|
|
18
|
+
#
|
|
19
|
+
# @option rules [Boolean] :with_currency (false) Whether the currency name
|
|
20
|
+
# should be appended to the result string.
|
|
21
|
+
#
|
|
22
|
+
# @example
|
|
23
|
+
# Money.ca_dollar(100).format #=> "$1.00"
|
|
24
|
+
# Money.ca_dollar(100).format(with_currency: true) #=> "$1.00 CAD"
|
|
25
|
+
# Money.us_dollar(85).format(with_currency: true) #=> "$0.85 USD"
|
|
26
|
+
#
|
|
27
|
+
# @option rules [Boolean] :rounded_infinite_precision (false) Whether the
|
|
28
|
+
# amount of money should be rounded when using {infinite_precision}
|
|
29
|
+
#
|
|
30
|
+
# @example
|
|
31
|
+
# Money.us_dollar(100.1).format #=> "$1.001"
|
|
32
|
+
# Money.us_dollar(100.1).format(rounded_infinite_precision: true) #=> "$1"
|
|
33
|
+
# Money.us_dollar(100.9).format(rounded_infinite_precision: true) #=> "$1.01"
|
|
34
|
+
#
|
|
35
|
+
# @option rules [Boolean] :no_cents (false) Whether cents should be omitted.
|
|
36
|
+
#
|
|
37
|
+
# @example
|
|
38
|
+
# Money.ca_dollar(100).format(no_cents: true) #=> "$1"
|
|
39
|
+
# Money.ca_dollar(599).format(no_cents: true) #=> "$5"
|
|
40
|
+
#
|
|
41
|
+
# @option rules [Boolean] :no_cents_if_whole (false) Whether cents should be
|
|
42
|
+
# omitted if the cent value is zero
|
|
43
|
+
#
|
|
44
|
+
# @example
|
|
45
|
+
# Money.ca_dollar(10000).format(no_cents_if_whole: true) #=> "$100"
|
|
46
|
+
# Money.ca_dollar(10034).format(no_cents_if_whole: true) #=> "$100.34"
|
|
47
|
+
#
|
|
48
|
+
# @option rules [Boolean, String, nil] :symbol (true) Whether a money symbol
|
|
49
|
+
# should be prepended to the result string. The default is true. This method
|
|
50
|
+
# attempts to pick a symbol that's suitable for the given currency.
|
|
51
|
+
#
|
|
52
|
+
# @example
|
|
53
|
+
# Money.new(100, "USD") #=> "$1.00"
|
|
54
|
+
# Money.new(100, "GBP") #=> "£1.00"
|
|
55
|
+
# Money.new(100, "EUR") #=> "€1.00"
|
|
56
|
+
#
|
|
57
|
+
# # Same thing.
|
|
58
|
+
# Money.new(100, "USD").format(symbol: true) #=> "$1.00"
|
|
59
|
+
# Money.new(100, "GBP").format(symbol: true) #=> "£1.00"
|
|
60
|
+
# Money.new(100, "EUR").format(symbol: true) #=> "€1.00"
|
|
61
|
+
#
|
|
62
|
+
# # You can specify a false expression or an empty string to disable
|
|
63
|
+
# # prepending a money symbol.§
|
|
64
|
+
# Money.new(100, "USD").format(symbol: false) #=> "1.00"
|
|
65
|
+
# Money.new(100, "GBP").format(symbol: nil) #=> "1.00"
|
|
66
|
+
# Money.new(100, "EUR").format(symbol: "") #=> "1.00"
|
|
67
|
+
#
|
|
68
|
+
# # If the symbol for the given currency isn't known, then it will default
|
|
69
|
+
# # to "¤" as symbol.
|
|
70
|
+
# Money.new(100, "AWG").format(symbol: true) #=> "¤1.00"
|
|
71
|
+
#
|
|
72
|
+
# # You can specify a string as value to enforce using a particular symbol.
|
|
73
|
+
# Money.new(100, "AWG").format(symbol: "ƒ") #=> "ƒ1.00"
|
|
74
|
+
#
|
|
75
|
+
# # You can specify a indian currency format
|
|
76
|
+
# Money.new(10000000, "INR").format(south_asian_number_formatting: true) #=> "1,00,000.00"
|
|
77
|
+
# Money.new(10000000).format(south_asian_number_formatting: true) #=> "$1,00,000.00"
|
|
78
|
+
#
|
|
79
|
+
# @option rules [Boolean, String, nil] :decimal_mark (true) Whether the
|
|
80
|
+
# currency should be separated by the specified character or '.'
|
|
81
|
+
#
|
|
82
|
+
# @example
|
|
83
|
+
# # If a string is specified, it's value is used.
|
|
84
|
+
# Money.new(100, "USD").format(decimal_mark: ",") #=> "$1,00"
|
|
85
|
+
#
|
|
86
|
+
# # If the decimal_mark for a given currency isn't known, then it will default
|
|
87
|
+
# # to "." as decimal_mark.
|
|
88
|
+
# Money.new(100, "FOO").format #=> "$1.00"
|
|
89
|
+
#
|
|
90
|
+
# @option rules [Boolean, String, nil] :thousands_separator (true) Whether
|
|
91
|
+
# the currency should be delimited by the specified character or ','
|
|
92
|
+
#
|
|
93
|
+
# @example
|
|
94
|
+
# # If a falsey value is specified, no thousands_separator is used.
|
|
95
|
+
# Money.new(100000, "USD").format(thousands_separator: false) #=> "1000.00"
|
|
96
|
+
# Money.new(100000, "USD").format(thousands_separator: nil) #=> "1000.00"
|
|
97
|
+
# Money.new(100000, "USD").format(thousands_separator: "") #=> "1000.00"
|
|
98
|
+
#
|
|
99
|
+
# # If true is specified, the locale or default thousands_separator is used.
|
|
100
|
+
# Money.new(100000, "USD").format(thousands_separator: true) #=> "1,000.00"
|
|
101
|
+
#
|
|
102
|
+
# # If a string is specified, it's value is used.
|
|
103
|
+
# Money.new(100000, "USD").format(thousands_separator: ".") #=> "$1.000.00"
|
|
104
|
+
#
|
|
105
|
+
# # If the thousands_separator for a given currency isn't known, then it will
|
|
106
|
+
# # default to "," as thousands_separator.
|
|
107
|
+
# Money.new(100000, "FOO").format #=> "$1,000.00"
|
|
108
|
+
#
|
|
109
|
+
# @option rules [Boolean] :html_wrap (false) Whether all currency parts should be HTML-formatted.
|
|
110
|
+
#
|
|
111
|
+
# @example
|
|
112
|
+
# Money.ca_dollar(570).format(html_wrap: true, with_currency: true)
|
|
113
|
+
# #=> "<span class=\"money-currency-symbol\">$</span><span class=\"money-whole\">5</span><span class=\"money-decimal-mark\">.</span><span class=\"money-decimal\">70</span> <span class=\"money-currency\">CAD</span>"
|
|
114
|
+
#
|
|
115
|
+
# @option rules [Boolean] :sign_positive (false) Whether positive numbers should be
|
|
116
|
+
# signed, too.
|
|
117
|
+
#
|
|
118
|
+
# @example
|
|
119
|
+
# # You can specify to display the sign with positive numbers
|
|
120
|
+
# Money.new(100, "GBP").format(sign_positive: true, sign_before_symbol: true) #=> "+£1.00"
|
|
121
|
+
# Money.new(100, "GBP").format(sign_positive: true, sign_before_symbol: false) #=> "£+1.00"
|
|
122
|
+
# Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: true) #=> "£1.00"
|
|
123
|
+
# Money.new(100, "GBP").format(sign_positive: false, sign_before_symbol: false) #=> "£1.00"
|
|
124
|
+
# Money.new(100, "GBP").format #=> "£+1.00"
|
|
125
|
+
#
|
|
126
|
+
# @option rules [Boolean] :disambiguate (false) Prevents the result from being ambiguous
|
|
127
|
+
# due to equal symbols for different currencies. Uses the `disambiguate_symbol`.
|
|
128
|
+
#
|
|
129
|
+
# @example
|
|
130
|
+
# Money.new(10000, "USD").format(disambiguate: false) #=> "$100.00"
|
|
131
|
+
# Money.new(10000, "CAD").format(disambiguate: false) #=> "$100.00"
|
|
132
|
+
# Money.new(10000, "USD").format(disambiguate: true) #=> "US$100.00"
|
|
133
|
+
# Money.new(10000, "CAD").format(disambiguate: true) #=> "C$100.00"
|
|
134
|
+
#
|
|
135
|
+
# @option rules [Boolean] :translate (true) `true` Checks for custom
|
|
136
|
+
# symbol definitions using I18n.
|
|
137
|
+
#
|
|
138
|
+
# @example
|
|
139
|
+
# # With the following entry in the translation files:
|
|
140
|
+
# # en:
|
|
141
|
+
# # number:
|
|
142
|
+
# # currency:
|
|
143
|
+
# # symbol:
|
|
144
|
+
# # CAD: "CAD$"
|
|
145
|
+
# Money.new(10000, "CAD").format(translate: true) #=> "CAD$100.00"
|
|
146
|
+
#
|
|
147
|
+
# @option rules [Boolean] :drop_trailing_zeros (false) Ignore trailing zeros after
|
|
148
|
+
# the decimal mark
|
|
149
|
+
#
|
|
150
|
+
# @example
|
|
151
|
+
# Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
|
|
152
|
+
# Money.new(110, :usd).format(drop_trailing_zeros: true) #=> $1.1
|
|
153
|
+
#
|
|
154
|
+
# @option rules [Boolean] :delimiter_pattern (/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/) Regular expression to set the placement
|
|
155
|
+
# for the thousands delimiter
|
|
156
|
+
#
|
|
157
|
+
# @example
|
|
158
|
+
# Money.new(89000, :btc).format(delimiter_pattern: /(\d)(?=\d)/) #=> B⃦8,9,0.00
|
|
159
|
+
#
|
|
160
|
+
# @option rules [String] :format (nil) Provide a template for formatting. `%u` will be replaced
|
|
161
|
+
# with the symbol (if present) and `%n` will be replaced with the number.
|
|
162
|
+
#
|
|
163
|
+
# @example
|
|
164
|
+
# Money.new(10000, "USD").format(format: '%u %n') #=> "$ 100.00"
|
|
165
|
+
# Money.new(10000, "USD").format(format: '<span>%u%n</span>') #=> "<span>$100.00</span>"
|
|
166
|
+
#
|
|
167
|
+
# Note that the default rules can be defined through {Money.default_formatting_rules} hash.
|
|
168
|
+
#
|
|
169
|
+
# @see Money.default_formatting_rules Money.default_formatting_rules for more information.
|
|
170
|
+
def initialize: (untyped money, *untyped rules) -> void
|
|
171
|
+
|
|
172
|
+
def to_s: () -> string
|
|
173
|
+
|
|
174
|
+
def thousands_separator: () -> string
|
|
175
|
+
|
|
176
|
+
def decimal_mark: () -> string
|
|
177
|
+
|
|
178
|
+
alias delimiter thousands_separator
|
|
179
|
+
|
|
180
|
+
alias separator decimal_mark
|
|
181
|
+
|
|
182
|
+
private
|
|
183
|
+
|
|
184
|
+
attr_reader money: Money
|
|
185
|
+
|
|
186
|
+
attr_reader currency: Currency
|
|
187
|
+
|
|
188
|
+
attr_reader rules: FormattingRules
|
|
189
|
+
|
|
190
|
+
def format_number: () -> string
|
|
191
|
+
|
|
192
|
+
def append_sign: (untyped formatted_number) -> string
|
|
193
|
+
|
|
194
|
+
def append_currency_symbol: (untyped formatted_number) -> string
|
|
195
|
+
|
|
196
|
+
def show_free_text?: () -> untyped
|
|
197
|
+
|
|
198
|
+
def html_wrap: (string string, string class_name) -> ::String
|
|
199
|
+
|
|
200
|
+
def free_text: () -> (untyped | "free")
|
|
201
|
+
|
|
202
|
+
def format_whole_part: (untyped value) -> untyped
|
|
203
|
+
|
|
204
|
+
def extract_whole_and_decimal_parts: () -> Array[string]
|
|
205
|
+
|
|
206
|
+
def format_decimal_part: (untyped value) -> (nil | string)
|
|
207
|
+
|
|
208
|
+
def lookup: (Symbol key) -> untyped
|
|
209
|
+
|
|
210
|
+
def lookup_default: (Symbol key) -> untyped
|
|
211
|
+
|
|
212
|
+
def symbol_value_from: (Hash[Symbol, untyped] rules) -> (untyped | untyped | "" | untyped)
|
|
213
|
+
end
|
|
214
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class Money
|
|
2
|
+
class FormattingRules
|
|
3
|
+
@rules: Hash[Symbol, untyped]
|
|
4
|
+
|
|
5
|
+
def initialize: (untyped currency, *untyped raw_rules) -> void
|
|
6
|
+
|
|
7
|
+
def []: (untyped key) -> untyped
|
|
8
|
+
|
|
9
|
+
def has_key?: (untyped key) -> untyped
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
attr_reader currency: untyped
|
|
14
|
+
|
|
15
|
+
# Cleans up formatting rules.
|
|
16
|
+
#
|
|
17
|
+
# @param [Hash] rules
|
|
18
|
+
#
|
|
19
|
+
# @return [Hash]
|
|
20
|
+
def normalize_formatting_rules: (Hash[Symbol, untyped] rules) -> Hash[Symbol, untyped]
|
|
21
|
+
|
|
22
|
+
def default_formatting_rules: () -> Hash[Symbol, untyped]
|
|
23
|
+
|
|
24
|
+
def translate_formatting_rules: (Hash[Symbol, untyped] rules) -> Hash[Symbol, untyped]
|
|
25
|
+
|
|
26
|
+
def determine_format: () -> String
|
|
27
|
+
|
|
28
|
+
def default_format: () -> String
|
|
29
|
+
|
|
30
|
+
def delimiter_pattern_rule: (Hash[Symbol, untyped] rules) -> ::Regexp
|
|
31
|
+
end
|
|
32
|
+
end
|