money 6.14.1 → 6.16.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 766ce2bf89d38cdc9280a79a77e1e6ccf940d1453a6b3d507e1a9ea81fd1d8ca
4
- data.tar.gz: 9fb7990b6742cc6778733f3369a129e8911ac85f6c796f94f3e120af2e7c9794
3
+ metadata.gz: 3de47437ec002ed24a400508cac96b2ff6812f484c666fe3db3a82e921a805c0
4
+ data.tar.gz: f96755602e56ea81d734b7505e7cfc88caaa948d7a8d285eeb470801028c15b9
5
5
  SHA512:
6
- metadata.gz: 3c6f467ccdfac45d2603a7642cf0da25cf0a4057bfa4d03c33a20acb4805b120b8fbe4c9b9cb8bdea6a3cc89d92425c5da19aaf5eef07827e5b16f82d054db13
7
- data.tar.gz: 749fc675428631b124ec50da09f76f253c7b76cb350262f82dc53de5f334e30bd4d143c1477ab76305ad747c29fb7750ca91976f5f2562d212580cf65158dde7
6
+ metadata.gz: 2f8cd71611e96a80cb733651675cb6fac59d358cb7c9576c8c264abbfec6eb1da11b8cbe2af12cdfb070e5dd01cd74f6fdc70772da16aa4c2abc5248b13fca3c
7
+ data.tar.gz: '092ba5a188eb29d462431e94420e1c6d7a7f9a4a59c0895f66a4a1cf92cfaa37cbc1bc344a241f5f8cc9fc4e998189164bc2d0ab11480a589fe7bb0e341754ba'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.16.0
4
+
5
+ - Add `Money.from_cents` alias as a more explicit initializer, it's the same as `Money.new`
6
+
7
+ ## 6.15.0
8
+
9
+ - Add :delimiter_pattern option to the Formatter
10
+
3
11
  ## 6.14.1
4
12
 
5
13
  - Fix CHF format regression introduced in v6.14.0
data/LICENSE CHANGED
@@ -1,23 +1,23 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
3
  Copyright (c) 2005 Tobias Lutke
4
4
  Copyright (c) 2008 Phusion
5
+ Copyright (c) 2021 Shane Emmons
5
6
 
6
- Permission is hereby granted, free of charge, to any person obtaining
7
- a copy of this software and associated documentation files (the
8
- "Software"), to deal in the Software without restriction, including
9
- without limitation the rights to use, copy, modify, merge, publish,
10
- distribute, sublicense, and/or sell copies of the Software, and to
11
- permit persons to whom the Software is furnished to do so, subject to
12
- the following conditions:
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
13
 
14
- The above copyright notice and this permission notice shall be
15
- included in all copies or substantial portions of the Software.
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
16
 
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.
data/README.md CHANGED
@@ -61,38 +61,38 @@ The development version (hosted on Github) can be installed with:
61
61
  require 'money'
62
62
 
63
63
  # 10.00 USD
64
- money = Money.new(1000, "USD")
64
+ money = Money.from_cents(1000, "USD")
65
65
  money.cents #=> 1000
66
66
  money.currency #=> Currency.new("USD")
67
67
 
68
68
  # Comparisons
69
- Money.new(1000, "USD") == Money.new(1000, "USD") #=> true
70
- Money.new(1000, "USD") == Money.new(100, "USD") #=> false
71
- Money.new(1000, "USD") == Money.new(1000, "EUR") #=> false
72
- Money.new(1000, "USD") != Money.new(1000, "EUR") #=> true
69
+ Money.from_cents(1000, "USD") == Money.from_cents(1000, "USD") #=> true
70
+ Money.from_cents(1000, "USD") == Money.from_cents(100, "USD") #=> false
71
+ Money.from_cents(1000, "USD") == Money.from_cents(1000, "EUR") #=> false
72
+ Money.from_cents(1000, "USD") != Money.from_cents(1000, "EUR") #=> true
73
73
 
74
74
  # Arithmetic
75
- Money.new(1000, "USD") + Money.new(500, "USD") == Money.new(1500, "USD")
76
- Money.new(1000, "USD") - Money.new(200, "USD") == Money.new(800, "USD")
77
- Money.new(1000, "USD") / 5 == Money.new(200, "USD")
78
- Money.new(1000, "USD") * 5 == Money.new(5000, "USD")
75
+ Money.from_cents(1000, "USD") + Money.from_cents(500, "USD") == Money.from_cents(1500, "USD")
76
+ Money.from_cents(1000, "USD") - Money.from_cents(200, "USD") == Money.from_cents(800, "USD")
77
+ Money.from_cents(1000, "USD") / 5 == Money.from_cents(200, "USD")
78
+ Money.from_cents(1000, "USD") * 5 == Money.from_cents(5000, "USD")
79
79
 
80
80
  # Unit to subunit conversions
81
- Money.from_amount(5, "USD") == Money.new(500, "USD") # 5 USD
82
- Money.from_amount(5, "JPY") == Money.new(5, "JPY") # 5 JPY
83
- Money.from_amount(5, "TND") == Money.new(5000, "TND") # 5 TND
81
+ Money.from_amount(5, "USD") == Money.from_cents(500, "USD") # 5 USD
82
+ Money.from_amount(5, "JPY") == Money.from_cents(5, "JPY") # 5 JPY
83
+ Money.from_amount(5, "TND") == Money.from_cents(5000, "TND") # 5 TND
84
84
 
85
85
  # Currency conversions
86
86
  some_code_to_setup_exchange_rates
87
- Money.new(1000, "USD").exchange_to("EUR") == Money.new(some_value, "EUR")
87
+ Money.from_cents(1000, "USD").exchange_to("EUR") == Money.from_cents(some_value, "EUR")
88
88
 
89
89
  # Swap currency
90
- Money.new(1000, "USD").with_currency("EUR") == Money.new(1000, "EUR")
90
+ Money.from_cents(1000, "USD").with_currency("EUR") == Money.from_cents(1000, "EUR")
91
91
 
92
92
  # Formatting (see Formatting section for more options)
93
- Money.new(100, "USD").format #=> "$1.00"
94
- Money.new(100, "GBP").format #=> "£1.00"
95
- Money.new(100, "EUR").format #=> "€1.00"
93
+ Money.from_cents(100, "USD").format #=> "$1.00"
94
+ Money.from_cents(100, "GBP").format #=> "£1.00"
95
+ Money.from_cents(100, "EUR").format #=> "€1.00"
96
96
  ```
97
97
 
98
98
  ## Currency
@@ -102,15 +102,15 @@ The most part of `Money` APIs allows you to supply either a `String` or a
102
102
  `Money::Currency`.
103
103
 
104
104
  ``` ruby
105
- Money.new(1000, "USD") == Money.new(1000, Money::Currency.new("USD"))
106
- Money.new(1000, "EUR").currency == Money::Currency.new("EUR")
105
+ Money.from_cents(1000, "USD") == Money.from_cents(1000, Money::Currency.new("USD"))
106
+ Money.from_cents(1000, "EUR").currency == Money::Currency.new("EUR")
107
107
  ```
108
108
 
109
109
  A `Money::Currency` instance holds all the information about the currency,
110
110
  including the currency symbol, name and much more.
111
111
 
112
112
  ``` ruby
113
- currency = Money.new(1000, "USD").currency
113
+ currency = Money.from_cents(1000, "USD").currency
114
114
  currency.iso_code #=> "USD"
115
115
  currency.name #=> "United States Dollar"
116
116
  ```
@@ -230,18 +230,18 @@ an example of how it works:
230
230
  Money.add_rate("USD", "CAD", 1.24515)
231
231
  Money.add_rate("CAD", "USD", 0.803115)
232
232
 
233
- Money.us_dollar(100).exchange_to("CAD") # => Money.new(124, "CAD")
234
- Money.ca_dollar(100).exchange_to("USD") # => Money.new(80, "USD")
233
+ Money.us_dollar(100).exchange_to("CAD") # => Money.from_cents(124, "CAD")
234
+ Money.ca_dollar(100).exchange_to("USD") # => Money.from_cents(80, "USD")
235
235
  ```
236
236
 
237
237
  Comparison and arithmetic operations work as expected:
238
238
 
239
239
  ``` ruby
240
- Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
241
- Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
240
+ Money.from_cents(1000, "USD") <=> Money.from_cents(900, "USD") # => 1; 9.00 USD is smaller
241
+ Money.from_cents(1000, "EUR") + Money.from_cents(10, "EUR") == Money.from_cents(1010, "EUR")
242
242
 
243
243
  Money.add_rate("USD", "EUR", 0.5)
244
- Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
244
+ Money.from_cents(1000, "EUR") + Money.from_cents(1000, "USD") == Money.from_cents(1500, "EUR")
245
245
  ```
246
246
 
247
247
  ### Exchange rate stores
@@ -349,7 +349,7 @@ Money.default_bank.add_rate('USD', 'CAD', 0.9)
349
349
  # Retrieve from the underlying store
350
350
  Money.default_bank.get_rate('USD', 'CAD') # => 0.9
351
351
  # Exchanging amounts just works.
352
- Money.new(1000, 'USD').exchange_to('CAD') #=> #<Money fractional:900 currency:CAD>
352
+ Money.from_cents(1000, 'USD').exchange_to('CAD') #=> #<Money fractional:900 currency:CAD>
353
353
  ```
354
354
 
355
355
  There is nothing stopping you from creating store objects which scrapes
@@ -396,7 +396,7 @@ There are several formatting rules for when `Money#format` is called. For more i
396
396
  If you wish to format money according to the EU's [Rules for expressing monetary units](http://publications.europa.eu/code/en/en-370303.htm#position) in either English, Irish, Latvian or Maltese:
397
397
 
398
398
  ```ruby
399
- m = Money.new('123', :gbp) # => #<Money fractional:123 currency:GBP>
399
+ m = Money.from_cents('123', :gbp) # => #<Money fractional:123 currency:GBP>
400
400
  m.format(symbol: m.currency.to_s + ' ') # => "GBP 1.23"
401
401
  ```
402
402
 
@@ -424,9 +424,9 @@ To round to the nearest cent (or anything more precise), you can use the `round`
424
424
 
425
425
  # Money
426
426
  Money.default_infinite_precision = true
427
- Money.new(2.34567).format #=> "$0.0234567"
428
- Money.new(2.34567).round.format #=> "$0.02"
429
- Money.new(2.34567).round(BigDecimal::ROUND_HALF_UP, 2).format #=> "$0.0235"
427
+ Money.from_cents(2.34567).format #=> "$0.0234567"
428
+ Money.from_cents(2.34567).round.format #=> "$0.02"
429
+ Money.from_cents(2.34567).round(BigDecimal::ROUND_HALF_UP, 2).format #=> "$0.0235"
430
430
  ```
431
431
 
432
432
  You can set the default rounding mode by passing one of the `BigDecimal` mode enumerables like so:
@@ -466,7 +466,7 @@ en:
466
466
  separator: "."
467
467
  ```
468
468
 
469
- For this example `Money.new(123456789, "SEK").format` will return `1,234,567.89
469
+ For this example `Money.from_cents(123456789, "SEK").format` will return `1,234,567.89
470
470
  kr` which otherwise would have returned `1 234 567,89 kr`.
471
471
 
472
472
  This will work seamlessly with [rails-i18n](https://github.com/svenfuchs/rails-i18n) gem that already has a lot of locales defined.
@@ -490,12 +490,12 @@ Money.locale_backend = :i18n
490
490
 
491
491
  # example (using default localization from rails-i18n):
492
492
  I18n.locale = :en
493
- Money.new(10_000_00, 'USD').format # => $10,000.00
494
- Money.new(10_000_00, 'EUR').format # => €10,000.00
493
+ Money.from_cents(10_000_00, 'USD').format # => $10,000.00
494
+ Money.from_cents(10_000_00, 'EUR').format # => €10,000.00
495
495
 
496
496
  I18n.locale = :es
497
- Money.new(10_000_00, 'USD').format # => $10.000,00
498
- Money.new(10_000_00, 'EUR').format # => €10.000,00
497
+ Money.from_cents(10_000_00, 'USD').format # => $10.000,00
498
+ Money.from_cents(10_000_00, 'EUR').format # => €10.000,00
499
499
  ```
500
500
 
501
501
  If you need to localize the position of the currency symbol, you
@@ -505,7 +505,7 @@ behavior in the next version.*
505
505
  ```ruby
506
506
  I18n.locale = :fr
507
507
  format = I18n.t :format, scope: 'number.currency.format'
508
- Money.new(10_00, 'EUR').format(format: format) # => 10,00 €
508
+ Money.from_cents(10_00, 'EUR').format(format: format) # => 10,00 €
509
509
  ```
510
510
 
511
511
  For the legacy behaviour of "per currency" localization (formatting depends only on currency):
@@ -514,8 +514,8 @@ For the legacy behaviour of "per currency" localization (formatting depends only
514
514
  Money.locale_backend = :currency
515
515
 
516
516
  # example:
517
- Money.new(10_000_00, 'USD').format # => $10,000.00
518
- Money.new(10_000_00, 'EUR').format # => €10.000,00
517
+ Money.from_cents(10_000_00, 'USD').format # => $10,000.00
518
+ Money.from_cents(10_000_00, 'EUR').format # => €10.000,00
519
519
  ```
520
520
 
521
521
  In case you don't need localization and would like to use default values (can be redefined using
@@ -525,8 +525,8 @@ In case you don't need localization and would like to use default values (can be
525
525
  Money.locale_backend = nil
526
526
 
527
527
  # example:
528
- Money.new(10_000_00, 'USD').format # => $10000.00
529
- Money.new(10_000_00, 'EUR').format # => €10000.00
528
+ Money.from_cents(10_000_00, 'USD').format # => $10000.00
529
+ Money.from_cents(10_000_00, 'EUR').format # => €10000.00
530
530
  ```
531
531
 
532
532
  ## Collection
data/lib/money/money.rb CHANGED
@@ -312,6 +312,10 @@ class Money
312
312
  new(value, currency, options)
313
313
  end
314
314
 
315
+ class << self
316
+ alias_method :from_cents, :new
317
+ end
318
+
315
319
  # Creates a new Money object of value given in the
316
320
  # +fractional unit+ of the given +currency+.
317
321
  #
@@ -211,6 +211,12 @@ class Money
211
211
  # Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
212
212
  # Money.new(110, :usd).format(drop_trailing_zeros: true) #=> $1.1
213
213
  #
214
+ # @option rules [Boolean] :delimiter_pattern (/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/) Regular expression to set the placement
215
+ # for the thousands delimiter
216
+ #
217
+ # @example
218
+ # Money.new(89000, :btc).format(delimiter_pattern: /(\d)(?=\d)/) #=> B⃦8,9,0.00
219
+ #
214
220
  # @option rules [String] :format (nil) Provide a template for formatting. `%u` will be replaced
215
221
  # with the symbol (if present) and `%n` will be replaced with the number.
216
222
  #
@@ -330,7 +336,9 @@ class Money
330
336
 
331
337
  def format_whole_part(value)
332
338
  # Apply thousands_separator
333
- value.gsub regexp_format, "\\1#{thousands_separator}"
339
+ value.gsub(rules[:delimiter_pattern]) do |digit_to_delimit|
340
+ "#{digit_to_delimit}#{thousands_separator}"
341
+ end
334
342
  end
335
343
 
336
344
  def extract_whole_and_decimal_parts
@@ -366,15 +374,6 @@ class Money
366
374
  (Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key]
367
375
  end
368
376
 
369
- def regexp_format
370
- if rules[:south_asian_number_formatting]
371
- # from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
372
- /(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
373
- else
374
- /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
375
- end
376
- end
377
-
378
377
  def symbol_value_from(rules)
379
378
  if rules.has_key?(:symbol)
380
379
  if rules[:symbol] === true
@@ -12,6 +12,7 @@ class Money
12
12
  @rules = localize_formatting_rules(@rules)
13
13
  @rules = translate_formatting_rules(@rules) if @rules[:translate]
14
14
  @rules[:format] ||= determine_format_from_formatting_rules(@rules)
15
+ @rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)
15
16
 
16
17
  warn_about_deprecated_rules(@rules)
17
18
  end
@@ -90,6 +91,15 @@ class Money
90
91
  end
91
92
  end
92
93
 
94
+ def delimiter_pattern_rule(rules)
95
+ if rules[:south_asian_number_formatting]
96
+ # from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
97
+ /(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
98
+ else
99
+ /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
100
+ end
101
+ end
102
+
93
103
  def symbol_position_from(rules)
94
104
  if rules.has_key?(:symbol_position)
95
105
  if [:before, :after].include?(rules[:symbol_position])
data/lib/money/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = '6.14.1'
2
+ VERSION = '6.16.0'
3
3
  end
data/money.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.add_development_dependency "rake"
21
21
  s.add_development_dependency "rspec", "~> 3.4"
22
22
  s.add_development_dependency "yard", "~> 0.9.11"
23
- s.add_development_dependency "kramdown", "~> 1.1"
23
+ s.add_development_dependency "kramdown", "~> 2.3"
24
24
 
25
25
  s.files = `git ls-files -z -- config/* lib/* CHANGELOG.md LICENSE money.gemspec README.md`.split("\x0")
26
26
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.14.1
4
+ version: 6.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  - Anthony Dmitriyev
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-02-25 00:00:00.000000000 Z
12
+ date: 2021-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
@@ -93,14 +93,14 @@ dependencies:
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '1.1'
96
+ version: '2.3'
97
97
  type: :development
98
98
  prerelease: false
99
99
  version_requirements: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '1.1'
103
+ version: '2.3'
104
104
  description: A Ruby Library for dealing with money and currency conversion.
105
105
  email:
106
106
  - shane@emmons.io
@@ -144,7 +144,7 @@ metadata:
144
144
  changelog_uri: https://github.com/RubyMoney/money/blob/master/CHANGELOG.md
145
145
  source_code_uri: https://github.com/RubyMoney/money/
146
146
  bug_tracker_uri: https://github.com/RubyMoney/money/issues
147
- post_install_message:
147
+ post_install_message:
148
148
  rdoc_options: []
149
149
  require_paths:
150
150
  - lib
@@ -159,8 +159,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0'
161
161
  requirements: []
162
- rubygems_version: 3.1.2
163
- signing_key:
162
+ rubygems_version: 3.2.3
163
+ signing_key:
164
164
  specification_version: 4
165
165
  summary: A Ruby Library for dealing with money and currency conversion.
166
166
  test_files: []