money_helper 1.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZWIyMmI3OTU3NmUxMjQ2OTBhMDMwMWE3OGI0YTZmYWNlMWIxM2I5MA==
5
- data.tar.gz: !binary |-
6
- MGFjMWQ5MTRlMzVkYWRjMTA0ZGI1NDQxNjk3NzUzNDRjZWEzM2ZmOA==
2
+ SHA256:
3
+ metadata.gz: 8fc38d5c4921667c0f06286f5058378d60658b3f991dd7a343f5ab597384560c
4
+ data.tar.gz: c88db16a193d5674d9f902466aace23c8b7b1a0f71b89733589a05be7059049b
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- OWIzN2QxZDUwOTMyNWFiMTZmZTdhNzIwZDMwZDBkMmJmMWMzMjQ5YjNiMGE4
10
- YzgxYWRlN2YyMGYyODJkNDI4NmI4ZjliZmM4MTM0ZTMzYTc1MWRjOWFlOTE4
11
- YmJiY2NiMmU4YzFhM2JlNjM0M2EyYzE2ZGI3MmIzY2YwOGQ3MDA=
12
- data.tar.gz: !binary |-
13
- ZTVkOWYyZDMzOWM1NmY5NDc1MTRlYmFkNDIwNjk3N2VkNDUwNDI5YWZkZGVi
14
- NTAzM2ZhYmQzZWJmNDA3YTllYTg5ZTg1MDcwYjQ0ZmY2MjJlMmI2MGEyMTFj
15
- M2NlMjllZDczMDgxZWE2NDhiMzMwYTc0MGZmMzYxMzlhYTUzMDU=
6
+ metadata.gz: 1c258cca4d8ac444695faedd9245d1f805bcb5534bcf6f864dffdcfe330bf8f54f6c10caf800e32c1c2621bc523f6277acde3821a16c6a11edf31b777cdbdc6b
7
+ data.tar.gz: 8133b959081243c17b6d160ec72a802ed79648989c60ebaca8546a8509cc145b1148f89de4cc1e4ee89cc51459cc0e54fcf58f29a7d5fd412b4f1a93a85744fa
@@ -0,0 +1,45 @@
1
+ version: 2.1
2
+
3
+ references:
4
+ set_env: &set_env
5
+ run:
6
+ name: Set up environment
7
+ command: |
8
+ echo "export BUNDLE_GEMFILE=$(pwd)/gemfiles/activesupport_$ACTIVESUPPORT_VERSION.gemfile" >> $BASH_ENV
9
+ bundle: &bundle
10
+ run:
11
+ name: Install dependencies
12
+ command: bundle install
13
+ test: &test
14
+ run:
15
+ name: Run test suite
16
+ command: bundle exec rake
17
+
18
+ jobs:
19
+ test:
20
+ docker:
21
+ - image: "cimg/ruby:<< parameters.ruby_version >>"
22
+ parameters:
23
+ ruby_version:
24
+ type: string
25
+ activesupport_version:
26
+ type: string
27
+ environment:
28
+ ACTIVESUPPORT_VERSION: << parameters.activesupport_version >>
29
+ steps:
30
+ - checkout
31
+ - <<: *set_env
32
+ - <<: *bundle
33
+ - <<: *test
34
+
35
+ workflows:
36
+ default:
37
+ jobs:
38
+ - test:
39
+ matrix:
40
+ parameters:
41
+ ruby_version: ["2.6", "2.7", "3.0"]
42
+ activesupport_version: ["52", "60", "61", "edge"]
43
+ exclude:
44
+ - ruby_version: "2.6"
45
+ activesupport_version: "edge"
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ gemfiles/*.lock
3
+ gemfiles/.bundle
4
+ gemfiles/vendor
5
+ pkg
6
+ .DS_Store
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - vendor/**/*
6
+ - gemfiles/vendor/**/*
7
+ NewCops: enable
8
+ SuggestExtensions: false
9
+
10
+ Layout/LineLength:
11
+ Max: 512
12
+
13
+ Metrics:
14
+ Enabled: false
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Naming/MethodName:
20
+ Enabled: false
21
+
22
+ Style/AsciiComments:
23
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,14 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2021-09-21 17:26:48 UTC using RuboCop version 1.21.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'money_helper.gemspec'
data/Appraisals ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ appraise 'activesupport_52' do
4
+ gem 'activesupport', '~> 5.2.6'
5
+ end
6
+
7
+ appraise 'activesupport_60' do
8
+ gem 'activesupport', '~> 6.0.4'
9
+ end
10
+
11
+ appraise 'activesupport_61' do
12
+ gem 'activesupport', '~> 6.1.4'
13
+ end
14
+
15
+ appraise 'activesupport_edge' do
16
+ git 'git://github.com/rails/rails.git' do
17
+ gem 'activesupport', require: 'active_support'
18
+ end
19
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,50 @@
1
+ ### 3.0.0 (Next)
2
+
3
+ * [#27](https://github.com/artsy/money_helper/pull/27): Update `money_to_text` to pass `format` options to `Money#format` for additional features such as `no_cents` - [@agrberg](https://github.com/agrberg).
4
+ * Your contribution here.
5
+
6
+ ### 2.0.0 (2021-09-20)
7
+
8
+ #### Breaking Changes
9
+
10
+ * [#20](https://github.com/artsy/money_helper/pull/20): Update `money_to_text` and `money_range_to_text` to expect amounts in minor unit and simplify formatting; drop support for Ruby 2.3 and 2.4 - [@starsirius](https://github.com/starsirius)
11
+
12
+ ### 1.0.2 (2019-04-19)
13
+
14
+ * [#16](https://github.com/artsy/money_helper/pull/16): Remove deprecation warnings - [@sweir27](https://github.com/sweir27).
15
+ * [#15](https://github.com/artsy/money_helper/pull/15): Added Rubocop - [@dblock](https://github.com/dblock).
16
+
17
+ ### 1.0.1 (2017-04-03)
18
+
19
+ * Relaxed activesupport version dependency - [@dblock](https://github.com/dblock).
20
+ * The `money_to_text` method now takes options - [@mzikherman](https://github.com/mzikherman).
21
+ * Added `symbol_with_optional_iso_code` - [@mzikherman](https://github.com/mzikherman).
22
+
23
+ ### 1.0.0 (2015-03-07)
24
+
25
+ * Added compatibility with Money 6.5.1+, including new currency symbols - [@syakhmi](https://github.com/syakhmi).
26
+
27
+ ### 0.0.5 (2014-04-12)
28
+
29
+ * Added compatibility with Money 6.1+ - [@syakhmi](https://github.com/syakhmi).
30
+ * Dropped support for pre 6.0 versions of Money - [@syakhmi](https://github.com/syakhmi).
31
+
32
+ ### 0.0.4 (2014-01-08)
33
+
34
+ * Added compatibility with Money 6.x - [@dblock](https://github.com/dblock).
35
+ * Avoid failing on deprecated currencies (such as ITL) - [@joeyAghion](https://github.com/joeyAghion).
36
+
37
+ ### 0.0.3 (2013-07-17)
38
+
39
+ * Added support for currencies that don't have a symbol, eg. Uzbekistan Som (UZS) - [@dblock](https://github.com/dblock).
40
+
41
+ ### 0.0.2 (2013-07-16)
42
+
43
+ * Added unit tests - [@syakhmi](https://github.com/syakhmi).
44
+ * Added support for Cambodian riel - [@syakhmi](https://github.com/syakhmi).
45
+ * Added dependencies to gemspec - [@syakhmi](https://github.com/syakhmi).
46
+ * Now defaults to USD when currency not specified - [@syakhmi](https://github.com/syakhmi).
47
+
48
+ ### 0.0.1 (2013-07-12)
49
+
50
+ * Initial public release - [@syakhmi](https://github.com/syakhmi).
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ # pinned so that the lock files stay in sync
8
+ gem 'rubocop', '1.21.0'
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013-2018 Artsy, Sahil Yakhmi, Joey Aghion, and contributors.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ MoneyHelper [![CircleCI][ci_badge]][circleci]
2
+ ============
3
+
4
+ A simple module to assist in formatting unambiguous prices and price ranges in international currencies in a Roman Script environment.
5
+
6
+ Usage
7
+ -----
8
+
9
+ Examples:
10
+
11
+ ``` ruby
12
+ money_to_text(10000, currency: 'AUD')
13
+ #=> 'AUD $100.00'
14
+
15
+ money_to_text(10000, currency: 'AUD', with_currency: false, with_symbol: false)
16
+ #=> '100.00'
17
+
18
+ money_range_to_text(10000, 40000, currency: 'AUD')
19
+ #=> 'AUD $100.00 - 400.00'
20
+
21
+ money_range_to_text(10000, 40000, currency: 'AUD', delimiter: ' ... ')
22
+ #=> 'AUD $100.00 ... 400.00'
23
+ ```
24
+
25
+ Defaults
26
+ --------
27
+
28
+ As of [#16][#16] this library explicitly specifies the `Money.locale_backend = :currency` default.
29
+
30
+ Contributing
31
+ ------------
32
+
33
+ Fork the project. Make your feature addition or bug fix with tests. Send a pull request.
34
+
35
+ Copyright and License
36
+ ---------------------
37
+
38
+ MIT License, see [LICENSE][license] for details.
39
+
40
+ (c) 2013-2018 [Artsy][artsy], [Sahil Yakhmi][sahil_yakhmi], [Joey Aghion][joey_aghion] and [contributors][contributors].
41
+
42
+ [ci_badge]: https://circleci.com/gh/artsy/money_helper/tree/main.svg?style=svg
43
+ [circleci]: https://circleci.com/gh/artsy/money_helper/tree/main
44
+ [#16]: https://github.com/artsy/money_helper/pull/16
45
+ [license]: LICENSE.md
46
+ [artsy]: https://github.com/artsy
47
+ [sahil_yakhmi]: https://github.com/syakhmi
48
+ [joey_aghion]: https://github.com/joeyAghion
49
+ [contributors]: CHANGELOG.md
data/RELEASING.md ADDED
@@ -0,0 +1,65 @@
1
+ # Releasing money_helper
2
+
3
+ There are no hard rules about when to release money_helper. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
4
+
5
+ ### Release
6
+
7
+ Run tests, check that all tests succeed locally.
8
+
9
+ ```
10
+ bundle install
11
+ rake
12
+ ```
13
+
14
+ Check that the last build succeeded on [CI][ci] for all supported platforms.
15
+
16
+ Change "Next" in [CHANGELOG.md][changelog] to the current date.
17
+
18
+ ```
19
+ ### 0.2.2 (2015/7/10)
20
+ ```
21
+
22
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
23
+
24
+ Commit your changes.
25
+
26
+ ```
27
+ git add CHANGELOG.md
28
+ git commit -m "Preparing for release, 0.2.2."
29
+ git push origin master
30
+ ```
31
+
32
+ Release.
33
+
34
+ ```
35
+ $ rake release
36
+
37
+ money_helper 0.2.2 built to pkg/money_helper-0.2.2.gem.
38
+ Tagged v0.2.2.
39
+ Pushed git commits and tags.
40
+ Pushed money_helper 0.2.2 to rubygems.org.
41
+ ```
42
+
43
+ ### Prepare for the Next Version
44
+
45
+ Add the next release to [CHANGELOG.md][changelog].
46
+
47
+ ```
48
+ ### 0.2.3 (Next)
49
+
50
+ * Your contribution here.
51
+ ```
52
+
53
+ Increment the third version number in [lib/version.rb][version].
54
+
55
+ Commit your changes.
56
+
57
+ ```
58
+ git add CHANGELOG.md lib/version.rb
59
+ git commit -m "Preparing for next development iteration, 0.2.3."
60
+ git push origin master
61
+ ```
62
+
63
+ [ci]: https://circleci.com/gh/artsy/money_helper/tree/master
64
+ [changelog]: CHANGELOG.md
65
+ [version]: lib/version.rb
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ require 'bundler/gem_tasks'
6
+
7
+ Bundler.setup :default, :development
8
+
9
+ require 'rspec/core'
10
+ require 'rspec/core/rake_task'
11
+
12
+ RSpec::Core::RakeTask.new(:spec) do |spec|
13
+ spec.pattern = FileList['spec/**/*_spec.rb']
14
+ end
15
+
16
+ require 'rubocop/rake_task'
17
+ RuboCop::RakeTask.new
18
+
19
+ task default: %i[rubocop spec]
data/UPGRADING.md ADDED
@@ -0,0 +1,44 @@
1
+ Upgrading MoneyHelper
2
+ =====================
3
+
4
+ ### Upgrading to >= 3.0.0
5
+
6
+ #### `money_to_text` format options
7
+
8
+ Prior to 3.0.0 `money_to_text` took a named argument `with_symbol`. The default value was `true` and assing `false` would omit the currency symbol.
9
+
10
+ This data was later passed to the `Money#format` method with a default `format` string of `'%u%n'`. Users of `MoneyHelper` could not modify the format string or utilize additional format options, such as `no_cents: false`.
11
+
12
+ In 3.0.0 this was changed so `money_to_text` can take an optional `format` argument which is eventually passed to `Money#format` with only the slight modification of adding in `format: '%u%n'` if this key is not present.
13
+
14
+ ### Upgrading to >= 2.0.0
15
+
16
+ #### `money_to_text` amount is now given in minor units
17
+
18
+ Prior to 2.0.0 `money_to_text` took an amount in a currency's major units and converted this value internally before passing to `Money#new` which expects values in their minor unit (i.e. For USD the major unit is Dollars and the minor unit is Cents). This version removes the autoscaling so `money_to_text` can take amounts in their minor units.
19
+
20
+ Additionally, `currency`, `number_only`, and `options` were removed as postional arguments and replaced with named arguments `currency`, `with_currency`, and `with_symbol` with `'USD'`, `true` and `true` as their default arguments respectively.
21
+
22
+ One minor difference is that `currency` can be omited completely where as in < 2.0.0 `''` or `nil` was required to be passed for the default of `'USD'` to be used.
23
+
24
+ Finally the automatic omission of certain currency codes (USD, GBP, EUR, and MYR) is no longer supported. To replicate this behavior you must explicitly specify `with_currency: false`.
25
+
26
+ The following illustrates the primary difference in usage.
27
+
28
+ ```ruby
29
+ # Before 2.0.0
30
+
31
+ MoneyHelper.money_to_text(123, 'USD')
32
+ # => $123
33
+
34
+ # After 2.0.0
35
+
36
+ MoneyHelper.money_to_text(123) # `currency: 'USD'` is the default
37
+ # => USD $1.23
38
+
39
+ # to replicate previous behavior almost exactly
40
+ MoneyHelper.money_to_text(12300, with_currency: false)
41
+ # => $123.00
42
+
43
+ # Note the inclusion of the cents. The ability to remove this was added in 3.0.0 as another breaking change
44
+ ```
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'http://rubygems.org'
6
+
7
+ gem 'activesupport', '~> 5.2.6'
8
+ gem 'rubocop', '1.21.0'
9
+
10
+ gemspec path: '../'
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'http://rubygems.org'
6
+
7
+ gem 'activesupport', '~> 6.0.4'
8
+ gem 'rubocop', '1.21.0'
9
+
10
+ gemspec path: '../'
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'http://rubygems.org'
6
+
7
+ gem 'activesupport', '~> 6.1.4'
8
+ gem 'rubocop', '1.21.0'
9
+
10
+ gemspec path: '../'
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source 'http://rubygems.org'
6
+
7
+ git 'git://github.com/rails/rails.git' do
8
+ gem 'activesupport', require: 'active_support'
9
+ end
10
+
11
+ gem 'rubocop', '1.21.0'
12
+
13
+ gemspec path: '../'
data/lib/money_helper.rb CHANGED
@@ -1,15 +1,18 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'version'
2
4
 
3
5
  require 'active_support/core_ext/object/blank'
4
6
  require 'money'
5
7
 
6
8
  module MoneyHelper
9
+ Money.locale_backend = :currency
7
10
  I18n.enforce_available_locales = false
8
11
 
9
- SYMBOL_ONLY = %w{USD GBP EUR MYR} #don't use ISO code
10
- OK_SYMBOLS = %w{
12
+ SYMBOL_ONLY = %w[USD GBP EUR MYR].freeze # don't use ISO code
13
+ OK_SYMBOLS = %w[
11
14
  $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր. C$ ₦ ₲ ₱ T ฿ T$ m ₴ ₫ ៛ ₺ E ₽
12
- } #ok to include in string
15
+ ].freeze # ok to include in string
13
16
 
14
17
  ##
15
18
  # Formats a single amount in the given currency into a price string. Defaults to USD if currency not
@@ -17,27 +20,38 @@ module MoneyHelper
17
20
  #
18
21
  # = Example
19
22
  #
20
- # $10,000; HKD $10,000 for (10000, "USD") and (10000, "HKD"), respectively
23
+ # MoneyHelper.money_to_text(30_175_93, currency: 'USD') #=> 'USD $30,175.93'
24
+ # MoneyHelper.money_to_text(30_175_93, currency: 'EUR') #=> 'EUR €30.175,93'
25
+ # MoneyHelper.money_to_text(30_175_93) #=> 'USD $30,175.93'
21
26
  #
22
27
  # = Arguments
23
28
  #
24
- # amount: (Float)
25
- # currency: (String)
26
- # number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
27
- # specific to currency)
28
- def self.money_to_text(amount, currency, number_only = false)
29
- return nil unless amount.present?
30
- currency = "USD" if currency.blank?
31
- valid_currency = code_valid?(currency) ? currency : "USD"
29
+ # amount_minor: (Integer) amount in minor unit
30
+ # currency: (String) optional ISO currency code, defaulting to USD
31
+ # with_currency: (Boolean) optional flag to include ISO currency code, defaulting to true
32
+ #  format: (Hash) optional formatting options to pass to `Money#format` e.g.:
33
+ # no_cents: (Boolean) optional flag to exclude cents, defaulting to false
34
+ # symbol: (Boolean) optional flag to include currency symbol, defaulting to true
35
+ def self.money_to_text(amount_minor, currency: 'USD', with_currency: true, format: {})
36
+ return '' if amount_minor.blank?
37
+
38
+ format_options = { format: '%u%n' }.merge(format)
39
+
40
+ formatted_amount = Money.new(amount_minor, currency).format(format_options)
41
+ formatted_currency = with_currency ? currency.upcase : ''
42
+
43
+ "#{formatted_currency} #{formatted_amount}".strip
44
+ end
45
+
46
+ def self.symbol_with_optional_iso_code(currency = 'USD')
32
47
  symbol = symbol_for_code(currency)
33
- include_symbol = !number_only && symbol.present? && OK_SYMBOLS.include?(symbol)
34
- subunit_factor = Money::Currency.new(valid_currency).subunit_to_unit
35
- (number_only || SYMBOL_ONLY.include?(currency) ? "" : currency + " ") +
36
- Money.new(amount*subunit_factor.ceil, valid_currency).format({
37
- no_cents: true,
38
- symbol_position: :before,
39
- symbol: include_symbol
40
- }).delete(' ')
48
+ if SYMBOL_ONLY.include?(currency)
49
+ symbol
50
+ elsif symbol && OK_SYMBOLS.include?(symbol)
51
+ "#{iso_for_currency(currency)} #{symbol}"
52
+ else
53
+ iso_for_currency(currency).to_s
54
+ end
41
55
  end
42
56
 
43
57
  ##
@@ -45,42 +59,50 @@ module MoneyHelper
45
59
  #
46
60
  # = Example
47
61
  #
48
- # $10,000 - 20,000 for (10000, 20000, "USD")
49
- # HKD $10,000 - 20,000 for (10000, 20000, "HKD")
50
- # $10,000 ... 20,000 for (10000, 20000, "USD", " ... ")
51
- # HKD $10,000 ... 20,000 for (10000, 20000, "HKD", " ... ")
62
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD') #=> 'USD $30,175.93 - 40,983.27'
63
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27) #=> 'USD $30,175.93 - 40,983.27'
64
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'EUR') #=> 'EUR €30.175,93 - 40.983,27'
65
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, delimiter: ' ... ') #=> 'USD $30,175.93 ... 40,983.27'
52
66
  #
53
67
  # = Arguments
54
68
  #
55
- # low: (Float)
56
- # high: (Float)
57
- # currency: (String)
69
+ # low: (Integer) amount in minor unit
70
+ # high: (Integer) amount in minor unit
71
+ # currency: (String) optional ISO currency code, defaulting to USD
58
72
  # delimiter: (String) optional
59
- def self.money_range_to_text(low, high, currency, delimiter = ' - ')
73
+ def self.money_range_to_text(low, high, currency: 'USD', delimiter: ' - ')
60
74
  if low.blank? && high.blank?
61
- nil
75
+ ''
62
76
  elsif low.blank?
63
- "Under " + money_to_text(high, currency)
77
+ "Under #{money_to_text(high, currency: currency)}"
64
78
  elsif high.blank?
65
- money_to_text(low, currency) + " and up"
79
+ "#{money_to_text(low, currency: currency)} and up"
66
80
  elsif low == high
67
- money_to_text(low, currency)
81
+ money_to_text(low, currency: currency)
68
82
  else
69
- [ money_to_text(low, currency), money_to_text(high, currency, true) ].compact.join(delimiter)
83
+ formatted_low = money_to_text(low, currency: currency)
84
+ formatted_high = money_to_text(high, currency: currency, with_currency: false, format: { symbol: false })
85
+ [formatted_low, formatted_high].compact.join(delimiter)
70
86
  end
71
87
  end
72
88
 
73
- private
74
-
75
89
  def self.code_valid?(code)
76
90
  Money::Currency.stringified_keys.include?(code.downcase)
77
91
  end
78
92
 
93
+ def self.iso_for_currency(code)
94
+ return unless code && code_valid?(code)
95
+
96
+ Money::Currency.new(code).iso_code.tap do |iso_code|
97
+ iso_code.strip! if iso_code.present?
98
+ end
99
+ end
100
+
79
101
  def self.symbol_for_code(code)
80
102
  return unless code && code_valid?(code)
103
+
81
104
  Money::Currency.new(code).symbol.tap do |symbol|
82
105
  symbol.strip! if symbol.present?
83
106
  end
84
107
  end
85
-
86
108
  end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MoneyHelper
4
+ VERSION = '3.0.0'
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+ require 'version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'money_helper'
8
+ s.version = MoneyHelper::VERSION
9
+ s.summary = 'MoneyHelper international price formatting utility.'
10
+ s.description = 'A simple module to assist in formatting unambiguous prices and price ranges in international currencies in a Roman Script context.'
11
+ s.authors = ['Sahil Yakhmi', 'Joey Aghion', 'Matt Zikherman', 'Sarah Weir']
12
+ s.email = 'matt@artsymail.com'
13
+ s.platform = Gem::Platform::RUBY
14
+ s.required_rubygems_version = '>= 1.3.6'
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- spec/*`.split("\n")
17
+ s.require_paths = ['lib']
18
+ s.homepage = 'https://github.com/artsy/money_helper'
19
+ s.add_dependency('activesupport')
20
+ s.add_dependency('money', '6.16.0')
21
+ s.add_development_dependency('appraisal')
22
+ s.add_development_dependency('rspec', '~> 3')
23
+ s.licenses = ['MIT']
24
+ end
@@ -1,178 +1,244 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'money_helper'
3
+ require 'spec_helper'
4
4
 
5
5
  describe MoneyHelper do
6
- describe "money_to_text" do
7
- it "includes only symbol for well-recognizable currencies" do
8
- expect(MoneyHelper.money_to_text(30000, "EUR")).to eql("€30.000")
9
- expect(MoneyHelper.money_to_text(30000, "GBP")).to eql(30,000")
10
- expect(MoneyHelper.money_to_text(30000, "MYR")).to eql("RM30,000")
11
- expect(MoneyHelper.money_to_text(30000, "TRY")).to eql("TRY 30.000")
12
- expect(MoneyHelper.money_to_text(30000, "USD")).to eql("$30,000")
13
- end
14
- it "includes ISO code and symbol for ambiguous currencies" do
15
- expect(MoneyHelper.money_to_text(30000, "AUD")).to eql("AUD $30,000")
16
- expect(MoneyHelper.money_to_text(30000, "ARS")).to eql("ARS $30.000")
17
- expect(MoneyHelper.money_to_text(30000, "BBD")).to eql("BBD $30,000")
18
- expect(MoneyHelper.money_to_text(30000, "BMD")).to eql("BMD $30,000")
19
- expect(MoneyHelper.money_to_text(30000, "BND")).to eql("BND $30,000")
20
- expect(MoneyHelper.money_to_text(30000, "BSD")).to eql("BSD $30,000")
21
- expect(MoneyHelper.money_to_text(30000, "BZD")).to eql("BZD $30,000")
22
- expect(MoneyHelper.money_to_text(30000, "CAD")).to eql("CAD $30,000")
23
- expect(MoneyHelper.money_to_text(30000, "CLP")).to eql("CLP $30.000")
24
- expect(MoneyHelper.money_to_text(30000, "COP")).to eql("COP $30.000")
25
- expect(MoneyHelper.money_to_text(30000, "CUC")).to eql("CUC $30,000")
26
- expect(MoneyHelper.money_to_text(30000, "CUP")).to eql("CUP $30,000")
27
- expect(MoneyHelper.money_to_text(30000, "DOP")).to eql("DOP $30,000")
28
- expect(MoneyHelper.money_to_text(30000, "FJD")).to eql("FJD $30,000")
29
- expect(MoneyHelper.money_to_text(30000, "GYD")).to eql("GYD $30,000")
30
- expect(MoneyHelper.money_to_text(30000, "HKD")).to eql("HKD $30,000")
31
- expect(MoneyHelper.money_to_text(30000, "JMD")).to eql("JMD $30,000")
32
- expect(MoneyHelper.money_to_text(30000, "KYD")).to eql("KYD $30,000")
33
- expect(MoneyHelper.money_to_text(30000, "LRD")).to eql("LRD $30,000")
34
- expect(MoneyHelper.money_to_text(30000, "MXN")).to eql("MXN $30,000")
35
- expect(MoneyHelper.money_to_text(30000, "NAD")).to eql("NAD $30,000")
36
- expect(MoneyHelper.money_to_text(30000, "NZD")).to eql("NZD $30,000")
37
- expect(MoneyHelper.money_to_text(30000, "SBD")).to eql("SBD $30,000")
38
- expect(MoneyHelper.money_to_text(30000, "SGD")).to eql("SGD $30,000")
39
- expect(MoneyHelper.money_to_text(30000, "SRD")).to eql("SRD $30,000")
40
- expect(MoneyHelper.money_to_text(30000, "TWD")).to eql("TWD $30,000")
41
- expect(MoneyHelper.money_to_text(30000, "TTD")).to eql("TTD $30,000")
42
- expect(MoneyHelper.money_to_text(30000, "UYU")).to eql("UYU $30.000")
43
- expect(MoneyHelper.money_to_text(30000, "XCD")).to eql("XCD $30,000")
44
- expect(MoneyHelper.money_to_text(30000, "ZWL")).to eql("ZWL $30,000")
45
-
46
- expect(MoneyHelper.money_to_text(30000, "FKP")).to eql("FKP £30,000")
47
- expect(MoneyHelper.money_to_text(30000, "GIP")).to eql("GIP £30,000")
48
- expect(MoneyHelper.money_to_text(30000, "SDG")).to eql("SDG £30,000")
49
- expect(MoneyHelper.money_to_text(30000, "SHP")).to eql("SHP £30,000")
50
-
51
- expect(MoneyHelper.money_to_text(30000, "CNY")).to eql("CNY ¥30,000")
52
- expect(MoneyHelper.money_to_text(30000, "JPY")).to eql("JPY ¥30,000")
53
-
54
- expect(MoneyHelper.money_to_text(30000, "ALL")).to eql("ALL L30,000")
55
- expect(MoneyHelper.money_to_text(30000, "HNL")).to eql("HNL L30,000")
56
- expect(MoneyHelper.money_to_text(30000, "LSL")).to eql("LSL L30,000")
57
- expect(MoneyHelper.money_to_text(30000, "MDL")).to eql("MDL L30,000")
58
- expect(MoneyHelper.money_to_text(30000, "RON")).to eql("RON 30.000")
59
- expect(MoneyHelper.money_to_text(30000, "SZL")).to eql("SZL E30,000")
60
-
61
- expect(MoneyHelper.money_to_text(30000, "ANG")).to eql("ANG ƒ30.000")
62
- expect(MoneyHelper.money_to_text(30000, "AWG")).to eql("AWG ƒ30,000")
63
-
64
- expect(MoneyHelper.money_to_text(30000, "BWP")).to eql("BWP P30,000")
65
- expect(MoneyHelper.money_to_text(30000, "MOP")).to eql("MOP P30,000")
66
-
67
- expect(MoneyHelper.money_to_text(30000, "CRC")).to eql("CRC ₡30.000")
68
- expect(MoneyHelper.money_to_text(30000, "SVC")).to eql("SVC 30,000")
69
-
70
- expect(MoneyHelper.money_to_text(30000, "MUR")).to eql("MUR ₨30,000")
71
- expect(MoneyHelper.money_to_text(30000, "NPR")).to eql("NPR ₨30,000")
72
- expect(MoneyHelper.money_to_text(30000, "PKR")).to eql("PKR ₨30,000")
73
- expect(MoneyHelper.money_to_text(30000, "SCR")).to eql("SCR 30,000")
74
- end
75
- it "includes ISO code and symbol for difficult to recognize symbols" do
76
- expect(MoneyHelper.money_to_text(30000, "AMD")).to eql("AMD դր.30,000")
77
- expect(MoneyHelper.money_to_text(30000, "BDT")).to eql("BDT 30,000")
78
- expect(MoneyHelper.money_to_text(30000, "BRL")).to eql("BRL R$30.000")
79
- expect(MoneyHelper.money_to_text(30000, "GMD")).to eql("GMD D30,000")
80
- expect(MoneyHelper.money_to_text(30000, "GEL")).to eql("GEL ლ30,000")
81
- expect(MoneyHelper.money_to_text(30000, "GHS")).to eql("GHS ₵30,000")
82
- expect(MoneyHelper.money_to_text(30000, "GTQ")).to eql("GTQ Q30,000")
83
- expect(MoneyHelper.money_to_text(30000, "HTG")).to eql("HTG G30,000")
84
- expect(MoneyHelper.money_to_text(30000, "IDR")).to eql("IDR Rp30.000")
85
- expect(MoneyHelper.money_to_text(30000, "ILS")).to eql("ILS 30,000")
86
- expect(MoneyHelper.money_to_text(30000, "INR")).to eql("INR 30,000")
87
- expect(MoneyHelper.money_to_text(30000, "KHR")).to eql("KHR 30,000")
88
- expect(MoneyHelper.money_to_text(30000, "KPW")).to eql("KPW 30,000")
89
- expect(MoneyHelper.money_to_text(30000, "LAK")).to eql("LAK 30,000")
90
- expect(MoneyHelper.money_to_text(30000, "MNT")).to eql("MNT ₮30,000")
91
- expect(MoneyHelper.money_to_text(30000, "NIO")).to eql("NIO C$30,000")
92
- expect(MoneyHelper.money_to_text(30000, "NGN")).to eql("NGN ₦30,000")
93
- expect(MoneyHelper.money_to_text(30000, "PGK")).to eql("PGK K30,000")
94
- expect(MoneyHelper.money_to_text(30000, "PHP")).to eql("PHP 30,000")
95
- expect(MoneyHelper.money_to_text(30000, "PYG")).to eql("PYG ₲30,000")
96
- expect(MoneyHelper.money_to_text(30000, "RUB")).to eql("RUB 30.000")
97
- expect(MoneyHelper.money_to_text(30000, "THB")).to eql("THB ฿30,000")
98
- expect(MoneyHelper.money_to_text(30000, "TOP")).to eql("TOP T$30,000")
99
- expect(MoneyHelper.money_to_text(30000, "TMT")).to eql("TMT T30,000")
100
- expect(MoneyHelper.money_to_text(30000, "UAH")).to eql("UAH ₴30,000")
101
- expect(MoneyHelper.money_to_text(30000, "VND")).to eql("VND ₫30.000")
102
- expect(MoneyHelper.money_to_text(30000, "WST")).to eql("WST T30,000")
103
- expect(MoneyHelper.money_to_text(30000, "ZAR")).to eql("ZAR R30,000")
104
- end
105
- it "includes only ISO code for currencies that don't have a symbol" do
106
- expect(MoneyHelper.money_to_text(30000, "UZS")).to eql("UZS 30,000")
107
- end
108
- it "includes only ISO code for RTL symbols" do
109
- expect(MoneyHelper.money_to_text(30000, "AFN")).to eql("AFN 30,000")
110
- expect(MoneyHelper.money_to_text(30000, "DZD")).to eql("DZD 30,000")
111
- expect(MoneyHelper.money_to_text(30000, "BHD")).to eql("BHD 30,000")
112
- expect(MoneyHelper.money_to_text(30000, "EGP")).to eql("EGP 30,000")
113
- expect(MoneyHelper.money_to_text(30000, "IRR")).to eql("IRR 30,000")
114
- expect(MoneyHelper.money_to_text(30000, "IQD")).to eql("IQD 30,000")
115
- expect(MoneyHelper.money_to_text(30000, "JOD")).to eql("JOD 30,000")
116
- expect(MoneyHelper.money_to_text(30000, "KWD")).to eql("KWD 30,000")
117
- expect(MoneyHelper.money_to_text(30000, "LBP")).to eql("LBP 30,000")
118
- expect(MoneyHelper.money_to_text(30000, "LYD")).to eql("LYD 30,000")
119
- expect(MoneyHelper.money_to_text(30000, "MAD")).to eql("MAD 30,000")
120
- expect(MoneyHelper.money_to_text(30000, "OMR")).to eql("OMR 30,000")
121
- expect(MoneyHelper.money_to_text(30000, "JOD")).to eql("JOD 30,000")
122
- expect(MoneyHelper.money_to_text(30000, "QAR")).to eql("QAR 30,000")
123
- expect(MoneyHelper.money_to_text(30000, "SAR")).to eql("SAR 30,000")
124
- expect(MoneyHelper.money_to_text(30000, "SYP")).to eql("SYP 30,000")
125
- expect(MoneyHelper.money_to_text(30000, "TND")).to eql("TND 30,000")
126
- expect(MoneyHelper.money_to_text(30000, "AED")).to eql("AED 30,000")
127
- expect(MoneyHelper.money_to_text(30000, "YER")).to eql("YER 30,000")
128
- end
129
- it "defaults to USD when an empty string or nil is passed as the currency" do
130
- expect(MoneyHelper.money_to_text(30000, "")).to eql("$30,000")
131
- expect(MoneyHelper.money_to_text(30000, nil)).to eql("$30,000")
132
- end
133
- it "returns only the formatted numeral when number_only = true" do
134
- expect(MoneyHelper.money_to_text(30000, "EUR", true)).to eql("30.000")
135
- expect(MoneyHelper.money_to_text(30000, "AUD", true)).to eql("30,000")
136
- expect(MoneyHelper.money_to_text(30000, "AMD", true)).to eql("30,000")
137
- expect(MoneyHelper.money_to_text(30000, "AFN", true)).to eql("30,000")
138
- end
139
- it "returns nil if amount passed in is whitespace, empty string, or nil" do
140
- expect(MoneyHelper.money_to_text(" ", "USD")).to be_nil
141
- expect(MoneyHelper.money_to_text("", "USD")).to be_nil
142
- expect(MoneyHelper.money_to_text(nil, "USD")).to be_nil
143
- end
144
- it "falls back to ISO code when currency can't be found" do
145
- expect(MoneyHelper.money_to_text(10_000, "ITL")).to eql("ITL 10,000")
146
- expect(MoneyHelper.money_to_text(10_000, "ITL", true)).to eql("10,000")
6
+ describe 'money_to_text' do
7
+ it 'includes ISO code and symbol' do
8
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD')).to eql('AUD $30,175.93')
9
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ARS')).to eql('ARS $30.175,93')
10
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BBD')).to eql('BBD $30,175.93')
11
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BMD')).to eql('BMD $30,175.93')
12
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BND')).to eql('BND $30,175.93')
13
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BSD')).to eql('BSD $30,175.93')
14
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BZD')).to eql('BZD $30,175.93')
15
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CAD')).to eql('CAD $30,175.93')
16
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CLP')).to eql('CLP $3.017.593')
17
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'COP')).to eql('COP $30.175,93')
18
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CUC')).to eql('CUC $30,175.93')
19
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CUP')).to eql('CUP $30,175.93')
20
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'DOP')).to eql('DOP $30,175.93')
21
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'FJD')).to eql('FJD $30,175.93')
22
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GYD')).to eql('GYD $30,175.93')
23
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HKD')).to eql('HKD $30,175.93')
24
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JMD')).to eql('JMD $30,175.93')
25
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KYD')).to eql('KYD $30,175.93')
26
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LRD')).to eql('LRD $30,175.93')
27
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MXN')).to eql('MXN $30,175.93')
28
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NAD')).to eql('NAD $30,175.93')
29
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NZD')).to eql('NZD $30,175.93')
30
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SBD')).to eql('SBD $30,175.93')
31
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SGD')).to eql('SGD $30,175.93')
32
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SRD')).to eql('SRD $30,175.93')
33
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TWD')).to eql('TWD $30,175.93')
34
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TTD')).to eql('TTD $30,175.93')
35
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'UYU')).to eql('UYU $U30.175,93')
36
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
37
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'XCD')).to eql('XCD $30,175.93')
38
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ZWL')).to eql('ZWL $30,175.93')
39
+
40
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR')).to eql('EUR 30.175,93')
41
+
42
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'FKP')).to eql('FKP £30,175.93')
43
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GBP')).to eql('GBP £30,175.93')
44
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GIP')).to eql('GIP £30,175.93')
45
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SDG')).to eql('SDG £30,175.93')
46
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SHP')).to eql('SHP £30,175.93')
47
+
48
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CNY')).to eql('CNY ¥30,175.93')
49
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JPY')).to eql('JPY ¥3,017,593')
50
+
51
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ALL')).to eql('ALL L30,175.93')
52
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HNL')).to eql('HNL L30,175.93')
53
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LSL')).to eql('LSL L30,175.93')
54
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MDL')).to eql('MDL L30,175.93')
55
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MYR')).to eql('MYR RM30,175.93')
56
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'RON')).to eql('RON Lei30.175,93')
57
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SZL')).to eql('SZL E30,175.93')
58
+
59
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ANG')).to eql('ANG ƒ30.175,93')
60
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AWG')).to eql('AWG ƒ30,175.93')
61
+
62
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BWP')).to eql('BWP P30,175.93')
63
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MOP')).to eql('MOP P30,175.93')
64
+
65
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CRC')).to eql('CRC ₡30.175,93')
66
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SVC')).to eql('SVC ₡30,175.93')
67
+
68
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MUR')).to eql('MUR 30,175.93')
69
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NPR')).to eql('NPR Rs.30,175.93')
70
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PKR')).to eql('PKR ₨30,175.93')
71
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SCR')).to eql('SCR ₨30,175.93')
72
+
73
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD')).to eql('AMD դր.30,175.93')
74
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BDT')).to eql('BDT ৳30,175.93')
75
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BRL')).to eql('BRL R$30.175,93')
76
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GEL')).to eql('GEL 30,175.93')
77
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GHS')).to eql('GHS 30,175.93')
78
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GMD')).to eql('GMD D30,175.93')
79
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GTQ')).to eql('GTQ Q30,175.93')
80
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HTG')).to eql('HTG G30,175.93')
81
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IDR')).to eql('IDR Rp30.175,93')
82
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ILS')).to eql('ILS ₪30,175.93')
83
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'INR')).to eql('INR ₹30,175.93')
84
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KHR')).to eql('KHR ៛30,175.93')
85
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KPW')).to eql('KPW 30,175.93')
86
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LAK')).to eql('LAK 30,175.93')
87
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MNT')).to eql('MNT 30,175.93')
88
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NGN')).to eql('NGN 30,175.93')
89
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NIO')).to eql('NIO C$30,175.93')
90
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PGK')).to eql('PGK K30,175.93')
91
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PHP')).to eql('PHP 30,175.93')
92
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PYG')).to eql('PYG ₲3,017,593')
93
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'RUB')).to eql('RUB ₽30.175,93')
94
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'THB')).to eql('THB ฿30,175.93')
95
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TMT')).to eql('TMT T30,175.93')
96
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TOP')).to eql('TOP T$30,175.93')
97
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TRY')).to eql('TRY 30.175,93')
98
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'UAH')).to eql('UAH 30,175.93')
99
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'VND')).to eql('VND ₫3.017.593')
100
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'WST')).to eql('WST T30,175.93')
101
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ZAR')).to eql('ZAR R30,175.93')
102
+ end
103
+
104
+ it "respects currency's thousands separator and decimal mark" do
105
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
106
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR')).to eql('EUR 30.175,93')
107
+ end
108
+
109
+ it "converts from currency's minor unit to major unit" do
110
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
111
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JPY')).to eql('JPY ¥3,017,593')
112
+ end
113
+
114
+ it 'displays LTR for RTL symbols' do
115
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN')).to eql('AFN ؋30,175.93')
116
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'DZD')).to eql('DZD د.ج30,175.93')
117
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BHD')).to eql('BHD د.ب3,017.593')
118
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EGP')).to eql('EGP ج.م30,175.93')
119
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IRR')).to eql('IRR 30,175.93')
120
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IQD')).to eql('IQD ع.د3,017.593')
121
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JOD')).to eql('JOD د.ا3,017.593')
122
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LYD')).to eql('LYD ل.د3,017.593')
123
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MAD')).to eql('MAD د.م.30,175.93')
124
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'QAR')).to eql('QAR ر.ق30,175.93')
125
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SYP')).to eql('SYP £S30,175.93')
126
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TND')).to eql('TND د.ت3,017.593')
127
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AED')).to eql('AED د.إ30,175.93')
128
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'YER')).to eql('YER ﷼30,175.93')
129
+ end
130
+
131
+ it 'defaults to USD' do
132
+ expect(MoneyHelper.money_to_text(30_175_93)).to eql('USD $30,175.93')
133
+ end
134
+
135
+ it 'omits ISO code when with_currency is false' do
136
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_currency: false)).to eql('€30.175,93')
137
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', with_currency: false)).to eql('$30,175.93')
138
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', with_currency: false)).to eql('դր.30,175.93')
139
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', with_currency: false)).to eql('؋30,175.93')
140
+ end
141
+
142
+ it 'omits symbol when `format: symbol:` is false' do
143
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', format: { symbol: false })).to eql('EUR 30.175,93')
144
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', format: { symbol: false })).to eql('AUD 30,175.93')
145
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', format: { symbol: false })).to eql('AMD 30,175.93')
146
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', format: { symbol: false })).to eql('AFN 30,175.93')
147
+ end
148
+
149
+ it 'omits ISO code and symbol when both `with_currency` and `format: symbol:` are false' do
150
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_currency: false, format: { symbol: false })).to eql('30.175,93')
151
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', with_currency: false, format: { symbol: false })).to eql('30,175.93')
152
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', with_currency: false, format: { symbol: false })).to eql('30,175.93')
153
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', with_currency: false, format: { symbol: false })).to eql('30,175.93')
154
+ end
155
+
156
+ it 'omits cents when `format: no_cents:` is true' do
157
+ expect(MoneyHelper.money_to_text(30_175_93, with_currency: false, format: { no_cents: true, symbol: false })).to eql('30,175')
158
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_currency: false, format: { no_cents: true, symbol: false })).to eql('30.175')
159
+ end
160
+
161
+ it 'returns an empty string if amount passed in is whitespace, empty string, or nil' do
162
+ expect(MoneyHelper.money_to_text(' ')).to eql('')
163
+ expect(MoneyHelper.money_to_text('')).to eql('')
164
+ expect(MoneyHelper.money_to_text(nil)).to eql('')
165
+ end
166
+
167
+ it "raises an exception when currency can't be found" do
168
+ expect do
169
+ MoneyHelper.money_to_text(30_175_93, currency: 'AAA')
170
+ end.to raise_error(Money::Currency::UnknownCurrency, "Unknown currency 'aaa'")
171
+ end
172
+ end
173
+
174
+ describe 'symbol_with_optional_iso_code' do
175
+ it 'just includes the symbol for USD GBP EUR and MYR' do
176
+ expect(MoneyHelper.symbol_with_optional_iso_code('EUR')).to eql('€')
177
+ expect(MoneyHelper.symbol_with_optional_iso_code('GBP')).to eql('£')
178
+ expect(MoneyHelper.symbol_with_optional_iso_code('MYR')).to eql('RM')
179
+ expect(MoneyHelper.symbol_with_optional_iso_code('USD')).to eql('$')
180
+ end
181
+
182
+ it 'includes the iso code as well for other currencies' do
183
+ expect(MoneyHelper.symbol_with_optional_iso_code('AUD')).to eql('AUD $')
184
+ expect(MoneyHelper.symbol_with_optional_iso_code('UZS')).to eql('UZS')
185
+ expect(MoneyHelper.symbol_with_optional_iso_code('JPY')).to eql('JPY ¥')
147
186
  end
148
187
  end
149
- describe "money_range_to_text" do
150
- it "includes no indicator for currency for the upper amount in range" do
151
- expect(MoneyHelper.money_range_to_text(30000, 40000, "USD")).to eql("$30,000 - 40,000")
152
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AUD")).to eql("AUD $30,000 - 40,000")
153
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AMD")).to eql("AMD դր.30,000 - 40,000")
154
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AFN")).to eql("AFN 30,000 - 40,000")
155
- end
156
- it "uses the special range amount delimeter when supplied" do
157
- expect(MoneyHelper.money_range_to_text(30000, 40000, "USD", "-")).to eql("$30,000-40,000")
158
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AUD", "-")).to eql("AUD $30,000-40,000")
159
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AMD", "-")).to eql("AMD դր.30,000-40,000")
160
- expect(MoneyHelper.money_range_to_text(30000, 40000, "AFN", "-")).to eql("AFN 30,000-40,000")
188
+
189
+ describe 'money_range_to_text' do
190
+ it 'includes no indicator for currency for the upper amount in range' do
191
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD')).to eql('USD $30,175.93 - 40,983.27')
192
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AUD')).to eql('AUD $30,175.93 - 40,983.27')
193
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AMD')).to eql('AMD դր.30,175.93 - 40,983.27')
194
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AFN')).to eql('AFN ؋30,175.93 - 40,983.27')
195
+ end
196
+
197
+ it 'uses the special range amount delimeter when supplied' do
198
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD', delimiter: '-')).to eql('USD $30,175.93-40,983.27')
199
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AUD', delimiter: '-')).to eql('AUD $30,175.93-40,983.27')
200
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AMD', delimiter: '-')).to eql('AMD դր.30,175.93-40,983.27')
201
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AFN', delimiter: '-')).to eql('AFN ؋30,175.93-40,983.27')
161
202
  end
203
+
162
204
  it "prefixes the text 'Under ' when low amount not given" do
163
- expect(MoneyHelper.money_range_to_text(nil, 40000, "USD")).to eql("Under $40,000")
205
+ expect(MoneyHelper.money_range_to_text(nil, 40_983_27, currency: 'USD')).to eql('Under USD $40,983.27')
164
206
  end
207
+
165
208
  it "appends the text ' and up' when high amount not given" do
166
- expect(MoneyHelper.money_range_to_text(30000, nil, "USD")).to eql("$30,000 and up")
209
+ expect(MoneyHelper.money_range_to_text(30_175_93, nil, currency: 'USD')).to eql('USD $30,175.93 and up')
210
+ end
211
+
212
+ it 'treats as a single price when low amount and high amount are identical' do
213
+ expect(MoneyHelper.money_range_to_text(30_175_93, 30_175_93, currency: 'USD')).to eql('USD $30,175.93')
214
+ end
215
+
216
+ it 'returns empty string when both amounts are nil' do
217
+ expect(MoneyHelper.money_range_to_text(nil, nil, currency: 'USD')).to eql('')
167
218
  end
168
- it "treats as a single price when low amount and high amount are identical" do
169
- expect(MoneyHelper.money_range_to_text(30000, 30000, "USD")).to eql("$30,000")
219
+
220
+ it "raises an exception when currency can't be found" do
221
+ expect do
222
+ MoneyHelper.money_range_to_text(10_000, 20_000, currency: 'ITL')
223
+ end.to raise_error(Money::Currency::UnknownCurrency, "Unknown currency 'itl'")
224
+ end
225
+ end
226
+
227
+ describe 'symbol_with_optional_iso_code' do
228
+ it 'returns the symbol only if currency is in SYMBOL_ONLY list' do
229
+ expect(MoneyHelper.symbol_with_optional_iso_code('EUR')).to eql('€')
230
+ expect(MoneyHelper.symbol_with_optional_iso_code('USD')).to eql('$')
170
231
  end
171
- it "returns nil when both amounts are nil" do
172
- expect(MoneyHelper.money_range_to_text(nil, nil, "USD")).to be_nil
232
+
233
+ it 'returns iso code and symbol if symbol is in OK_SYMBOLS' do
234
+ expect(MoneyHelper.symbol_with_optional_iso_code('INR')).to eql('INR ₹')
235
+ expect(MoneyHelper.symbol_with_optional_iso_code('KHR')).to eql('KHR ៛')
236
+ expect(MoneyHelper.symbol_with_optional_iso_code('KPW')).to eql('KPW ₩')
173
237
  end
174
- it "falls back to ISO code when currency can't be found" do
175
- expect(MoneyHelper.money_range_to_text(10_000, 20_000, "ITL")).to eql("ITL 10,000 - 20,000")
238
+
239
+ it 'returns only the iso code if symbol is not in OK_SYMBOLS' do
240
+ expect(MoneyHelper.symbol_with_optional_iso_code('CHF')).to eql('CHF')
241
+ expect(MoneyHelper.symbol_with_optional_iso_code('YER')).to eql('YER')
176
242
  end
177
243
  end
178
244
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
4
+
5
+ require 'rubygems'
6
+ require 'rspec'
7
+
8
+ require 'money_helper'
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe MoneyHelper do
6
+ it 'has a version' do
7
+ expect(MoneyHelper::VERSION).to_not be nil
8
+ end
9
+ end
metadata CHANGED
@@ -1,90 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sahil Yakhmi
8
8
  - Joey Aghion
9
- autorequire:
9
+ - Matt Zikherman
10
+ - Sarah Weir
11
+ autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
12
- date: 2015-03-07 00:00:00.000000000 Z
14
+ date: 2022-02-16 00:00:00.000000000 Z
13
15
  dependencies:
14
16
  - !ruby/object:Gem::Dependency
15
- name: money
17
+ name: activesupport
16
18
  requirement: !ruby/object:Gem::Requirement
17
19
  requirements:
18
- - - ~>
20
+ - - ">="
19
21
  - !ruby/object:Gem::Version
20
- version: '6.5'
22
+ version: '0'
21
23
  type: :runtime
22
24
  prerelease: false
23
25
  version_requirements: !ruby/object:Gem::Requirement
24
26
  requirements:
25
- - - ~>
27
+ - - ">="
26
28
  - !ruby/object:Gem::Version
27
- version: '6.5'
29
+ version: '0'
28
30
  - !ruby/object:Gem::Dependency
29
- name: activesupport
31
+ name: money
30
32
  requirement: !ruby/object:Gem::Requirement
31
33
  requirements:
32
- - - ~>
34
+ - - '='
33
35
  - !ruby/object:Gem::Version
34
- version: '4'
36
+ version: 6.16.0
35
37
  type: :runtime
36
38
  prerelease: false
37
39
  version_requirements: !ruby/object:Gem::Requirement
38
40
  requirements:
39
- - - ~>
41
+ - - '='
42
+ - !ruby/object:Gem::Version
43
+ version: 6.16.0
44
+ - !ruby/object:Gem::Dependency
45
+ name: appraisal
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
40
56
  - !ruby/object:Gem::Version
41
- version: '4'
57
+ version: '0'
42
58
  - !ruby/object:Gem::Dependency
43
59
  name: rspec
44
60
  requirement: !ruby/object:Gem::Requirement
45
61
  requirements:
46
- - - ~>
62
+ - - "~>"
47
63
  - !ruby/object:Gem::Version
48
64
  version: '3'
49
65
  type: :development
50
66
  prerelease: false
51
67
  version_requirements: !ruby/object:Gem::Requirement
52
68
  requirements:
53
- - - ~>
69
+ - - "~>"
54
70
  - !ruby/object:Gem::Version
55
71
  version: '3'
56
72
  description: A simple module to assist in formatting unambiguous prices and price
57
73
  ranges in international currencies in a Roman Script context.
58
- email: sahil@artsymail.net
74
+ email: matt@artsymail.com
59
75
  executables: []
60
76
  extensions: []
61
77
  extra_rdoc_files: []
62
78
  files:
79
+ - ".circleci/config.yml"
80
+ - ".gitignore"
81
+ - ".rubocop.yml"
82
+ - ".rubocop_todo.yml"
83
+ - Appraisals
84
+ - CHANGELOG.md
85
+ - Gemfile
86
+ - LICENSE.md
87
+ - README.md
88
+ - RELEASING.md
89
+ - Rakefile
90
+ - UPGRADING.md
91
+ - gemfiles/activesupport_52.gemfile
92
+ - gemfiles/activesupport_60.gemfile
93
+ - gemfiles/activesupport_61.gemfile
94
+ - gemfiles/activesupport_edge.gemfile
63
95
  - lib/money_helper.rb
96
+ - lib/version.rb
97
+ - money_helper.gemspec
64
98
  - spec/money_helper_spec.rb
65
- homepage: https://github.com/syakhmi/money_helper
99
+ - spec/spec_helper.rb
100
+ - spec/version_spec.rb
101
+ homepage: https://github.com/artsy/money_helper
66
102
  licenses:
67
103
  - MIT
68
104
  metadata: {}
69
- post_install_message:
105
+ post_install_message:
70
106
  rdoc_options: []
71
107
  require_paths:
72
108
  - lib
73
109
  required_ruby_version: !ruby/object:Gem::Requirement
74
110
  requirements:
75
- - - ! '>='
111
+ - - ">="
76
112
  - !ruby/object:Gem::Version
77
113
  version: '0'
78
114
  required_rubygems_version: !ruby/object:Gem::Requirement
79
115
  requirements:
80
- - - ! '>='
116
+ - - ">="
81
117
  - !ruby/object:Gem::Version
82
- version: '0'
118
+ version: 1.3.6
83
119
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.4.6
86
- signing_key:
120
+ rubygems_version: 3.2.27
121
+ signing_key:
87
122
  specification_version: 4
88
- summary: MoneyHelper international price formatting utility
123
+ summary: MoneyHelper international price formatting utility.
89
124
  test_files:
90
125
  - spec/money_helper_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/version_spec.rb