money_helper 0.0.5 → 2.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
- MWZjMTgyNzQwNTY2ZjQzOTEzNzQxYTQyMmIxYjk1Yjc5ZjU2OWQ5Yg==
5
- data.tar.gz: !binary |-
6
- ZWRhNjgyOTJiN2ViZjdkMTExMDkwZGJmYTU2ZTI4NDc1NjY0NzczMQ==
2
+ SHA256:
3
+ metadata.gz: df602a2214fc58f6ccf9041f9cce6cc87ba22609f6d8314b6a497b5802de48ff
4
+ data.tar.gz: 46e3888d004f525559bf11b657e7cb504eb9c12f5dcaed61a56b18f1469a1e64
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- OThjNWYzNjBmNjA5MGJjNjEzNWE3ZGJkOTBiMTdiZWVlZmQ1ODkyZGI0OTFk
10
- Yzg5YTdkZjA2YjY1NGMwODNmYTIzMDIyOGRmNDdmMDMxNTBiZWNiNWNjYmVi
11
- N2MyNjNlZThlYTEzOGQxOTg3MDAxNWI0N2Q5NjQwNWU1MGZhOTQ=
12
- data.tar.gz: !binary |-
13
- MjUyMTE5NzY0MjA1NWY3MDVhNmYwNDk5NDg4YzRkZTBjMTgxOGE2MmIyOGUx
14
- NzNhMWZmNmJkOTEzZTc2MTViOTBlNmMzMmYxMjJmYTQ0NTc1NDRkZTYwODc1
15
- M2M5MWU1ZmExYTllMmQ2YzlhNDU1MGIwNDIyZWQxNDkzZWRmM2M=
6
+ metadata.gz: 1b5f70766be6b1091e40266cb5c3cbcb34d0457e37511e60537935bc4408dada70693d0141e3c95840894f3a3d34658a99cd1354c413b58c97484047448dcecc
7
+ data.tar.gz: 7eb7051305203fed98bed96d14c2651c989fc498c58859802a85bdd26e7dda11792cef78ad9c0290530b3cd7bd69d4c94c2664cfc0bb4f487584c68e86016e25
@@ -0,0 +1,47 @@
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.5", "2.6", "2.7"]
42
+ activesupport_version: ["40", "41", "42", "50", "51", "52", "edge"]
43
+ exclude:
44
+ - ruby_version: "2.5"
45
+ activesupport_version: "edge"
46
+ - ruby_version: "2.6"
47
+ 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,21 @@
1
+ AllCops:
2
+ Exclude:
3
+ - vendor/**/*
4
+ - gemfiles/vendor/**/*
5
+
6
+ Metrics:
7
+ Enabled: false
8
+
9
+ Metrics/LineLength:
10
+ Max: 512
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Naming/MethodName:
16
+ Enabled: false
17
+
18
+ Style/AsciiComments:
19
+ Enabled: false
20
+
21
+ inherit_from: .rubocop_todo.yml
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,12 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2019-04-19 14:01:54 -0400 using RuboCop version 0.67.2.
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: 3
10
+ Lint/IneffectiveAccessModifier:
11
+ Exclude:
12
+ - 'lib/money_helper.rb'
data/Appraisals ADDED
@@ -0,0 +1,29 @@
1
+ appraise 'activesupport_40' do
2
+ gem 'activesupport', '~> 4.0.0'
3
+ end
4
+
5
+ appraise 'activesupport_41' do
6
+ gem 'activesupport', '~> 4.1.0'
7
+ end
8
+
9
+ appraise 'activesupport_42' do
10
+ gem 'activesupport', '~> 4.2.0'
11
+ end
12
+
13
+ appraise 'activesupport_50' do
14
+ gem 'activesupport', '~> 5.0.0'
15
+ end
16
+
17
+ appraise 'activesupport_51' do
18
+ gem 'activesupport', '~> 5.1.0'
19
+ end
20
+
21
+ appraise 'activesupport_52' do
22
+ gem 'activesupport', '~> 5.2.1'
23
+ end
24
+
25
+ appraise 'activesupport_edge' do
26
+ git 'git://github.com/rails/rails.git' do
27
+ gem 'activesupport', require: 'active_support'
28
+ end
29
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,49 @@
1
+ ### 2.0.1 (Next)
2
+
3
+ * Your contribution here.
4
+
5
+ ### 2.0.0 (2021-09-20)
6
+
7
+ #### Breaking Changes
8
+
9
+ * [#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)
10
+
11
+ ### 1.0.2 (2019-04-19)
12
+
13
+ * [#16](https://github.com/artsy/money_helper/pull/16): Remove deprecation warnings - [@sweir27](https://github.com/sweir27).
14
+ * [#15](https://github.com/artsy/money_helper/pull/15): Added Rubocop - [@dblock](https://github.com/dblock).
15
+
16
+ ### 1.0.1 (2017-04-03)
17
+
18
+ * Relaxed activesupport version dependency - [@dblock](https://github.com/dblock).
19
+ * The `money_to_text` method now takes options - [@mzikherman](https://github.com/mzikherman).
20
+ * Added `symbol_with_optional_iso_code` - [@mzikherman](https://github.com/mzikherman).
21
+
22
+ ### 1.0.0 (2015-03-07)
23
+
24
+ * Added compatibility with Money 6.5.1+, including new currency symbols - [@syakhmi](https://github.com/syakhmi).
25
+
26
+ ### 0.0.5 (2014-04-12)
27
+
28
+ * Added compatibility with Money 6.1+ - [@syakhmi](https://github.com/syakhmi).
29
+ * Dropped support for pre 6.0 versions of Money - [@syakhmi](https://github.com/syakhmi).
30
+
31
+ ### 0.0.4 (2014-01-08)
32
+
33
+ * Added compatibility with Money 6.x - [@dblock](https://github.com/dblock).
34
+ * Avoid failing on deprecated currencies (such as ITL) - [@joeyAghion](https://github.com/joeyAghion).
35
+
36
+ ### 0.0.3 (2013-07-17)
37
+
38
+ * Added support for currencies that don't have a symbol, eg. Uzbekistan Som (UZS) - [@dblock](https://github.com/dblock).
39
+
40
+ ### 0.0.2 (2013-07-16)
41
+
42
+ * Added unit tests - [@syakhmi](https://github.com/syakhmi).
43
+ * Added support for Cambodian riel - [@syakhmi](https://github.com/syakhmi).
44
+ * Added dependencies to gemspec - [@syakhmi](https://github.com/syakhmi).
45
+ * Now defaults to USD when currency not specified - [@syakhmi](https://github.com/syakhmi).
46
+
47
+ ### 0.0.1 (2013-07-12)
48
+
49
+ * Initial public release - [@syakhmi](https://github.com/syakhmi).
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rubocop', '0.67.2'
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/master.svg?style=shield
43
+ [circleci]: https://circleci.com/gh/artsy/money_helper/tree/master
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,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'bundler/gem_tasks'
4
+
5
+ Bundler.setup :default, :development
6
+
7
+ require 'rspec/core'
8
+ require 'rspec/core/rake_task'
9
+
10
+ RSpec::Core::RakeTask.new(:spec) do |spec|
11
+ spec.pattern = FileList['spec/**/*_spec.rb']
12
+ end
13
+
14
+ require 'rubocop/rake_task'
15
+ RuboCop::RakeTask.new
16
+
17
+ task default: %i[rubocop spec]
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 4.0.0'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 4.1.0'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 4.2.0'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 5.0.0'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 5.1.0'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ gem 'activesupport', '~> 5.2.1'
6
+ gem 'rubocop', '0.59.2'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,11 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source 'http://rubygems.org'
4
+
5
+ git 'git://github.com/rails/rails.git' do
6
+ gem 'activesupport', require: 'active_support'
7
+ end
8
+
9
+ gem 'rubocop', '0.59.2'
10
+
11
+ gemspec path: '../'
data/lib/money_helper.rb CHANGED
@@ -1,14 +1,16 @@
1
- # encoding: UTF-8
1
+ require_relative 'version'
2
2
 
3
3
  require 'active_support/core_ext/object/blank'
4
4
  require 'money'
5
5
 
6
6
  module MoneyHelper
7
+ Money.locale_backend = :currency
8
+ I18n.enforce_available_locales = false
7
9
 
8
- SYMBOL_ONLY = %w{USD GBP EUR MYR TRY} #don't use ISO code
9
- OK_SYMBOLS = %w{
10
- $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր. C$ ₦ TL ₲ ₱ T ฿ T$ m ₴ ₫ ៛
11
- } #ok to include in string
10
+ SYMBOL_ONLY = %w[USD GBP EUR MYR].freeze # don't use ISO code
11
+ OK_SYMBOLS = %w[
12
+ $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր. C$ ₦ ₲ ₱ T ฿ T$ m ₴ ₫ ៛ ₺ E ₽
13
+ ].freeze # ok to include in string
12
14
 
13
15
  ##
14
16
  # Formats a single amount in the given currency into a price string. Defaults to USD if currency not
@@ -16,27 +18,39 @@ module MoneyHelper
16
18
  #
17
19
  # = Example
18
20
  #
19
- # $10,000; HKD $10,000 for (10000, "USD") and (10000, "HKD"), respectively
21
+ # MoneyHelper.money_to_text(30_175_93, currency: 'USD') #=> 'USD $30,175.93'
22
+ # MoneyHelper.money_to_text(30_175_93, currency: 'EUR') #=> 'EUR €30.175,93'
23
+ # MoneyHelper.money_to_text(30_175_93) #=> 'USD $30,175.93'
20
24
  #
21
25
  # = Arguments
22
26
  #
23
- # amount: (Float)
24
- # currency: (String)
25
- # number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
26
- # specific to currency)
27
- def self.money_to_text(amount, currency, number_only = false)
28
- return nil unless amount.present?
29
- currency = "USD" if currency.blank?
30
- valid_currency = code_valid?(currency) ? currency : "USD"
27
+ # amount_minor: (Integer) amount in minor unit
28
+ # currency: (String) optional ISO currency code, defaulting to USD
29
+ # with_currency: (Boolean) optional flag to include ISO currency code, defaulting to true
30
+ # with_symbol: (Boolean) optional flag to include currency symbol, defaulting to true
31
+ def self.money_to_text(amount_minor, currency: 'USD', with_currency: true, with_symbol: true)
32
+ return '' if amount_minor.blank?
33
+
34
+ money_options = {
35
+ format: '%u%n',
36
+ symbol: with_symbol
37
+ }
38
+
39
+ formatted_amount = Money.new(amount_minor, currency).format(money_options)
40
+ formatted_currency = with_currency ? currency.upcase : ''
41
+
42
+ "#{formatted_currency} #{formatted_amount}".strip
43
+ end
44
+
45
+ def self.symbol_with_optional_iso_code(currency = 'USD')
31
46
  symbol = symbol_for_code(currency)
32
- include_symbol = !number_only && symbol.present? && OK_SYMBOLS.include?(symbol)
33
- subunit_factor = Money::Currency.new(valid_currency).subunit_to_unit
34
- (number_only || SYMBOL_ONLY.include?(currency) ? "" : currency + " ") +
35
- Money.new(amount*subunit_factor.ceil, valid_currency).format({
36
- no_cents: true,
37
- symbol_position: :before,
38
- symbol: include_symbol
39
- }).delete(' ')
47
+ if SYMBOL_ONLY.include?(currency)
48
+ symbol
49
+ elsif symbol && OK_SYMBOLS.include?(symbol)
50
+ "#{iso_for_currency(currency)} #{symbol}"
51
+ else
52
+ iso_for_currency(currency).to_s
53
+ end
40
54
  end
41
55
 
42
56
  ##
@@ -44,42 +58,50 @@ module MoneyHelper
44
58
  #
45
59
  # = Example
46
60
  #
47
- # $10,000 - 20,000 for (10000, 20000, "USD")
48
- # HKD $10,000 - 20,000 for (10000, 20000, "HKD")
49
- # $10,000 ... 20,000 for (10000, 20000, "USD", " ... ")
50
- # HKD $10,000 ... 20,000 for (10000, 20000, "HKD", " ... ")
61
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD') #=> 'USD $30,175.93 - 40,983.27'
62
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27) #=> 'USD $30,175.93 - 40,983.27'
63
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'EUR') #=> 'EUR €30.175,93 - 40.983,27'
64
+ # MoneyHelper.money_range_to_text(30_175_93, 40_983_27, delimiter: ' ... ') #=> 'USD $30,175.93 ... 40,983.27'
51
65
  #
52
66
  # = Arguments
53
67
  #
54
- # low: (Float)
55
- # high: (Float)
56
- # currency: (String)
68
+ # low: (Integer) amount in minor unit
69
+ # high: (Integer) amount in minor unit
70
+ # currency: (String) optional ISO currency code, defaulting to USD
57
71
  # delimiter: (String) optional
58
- def self.money_range_to_text(low, high, currency, delimiter = ' - ')
72
+ def self.money_range_to_text(low, high, currency: 'USD', delimiter: ' - ')
59
73
  if low.blank? && high.blank?
60
- nil
74
+ ''
61
75
  elsif low.blank?
62
- "Under " + money_to_text(high, currency)
76
+ 'Under ' + money_to_text(high, currency: currency)
63
77
  elsif high.blank?
64
- money_to_text(low, currency) + " and up"
78
+ money_to_text(low, currency: currency) + ' and up'
65
79
  elsif low == high
66
- money_to_text(low, currency)
80
+ money_to_text(low, currency: currency)
67
81
  else
68
- [ money_to_text(low, currency), money_to_text(high, currency, true) ].compact.join(delimiter)
82
+ formatted_low = money_to_text(low, currency: currency)
83
+ formatted_high = money_to_text(high, currency: currency, with_currency: false, with_symbol: false)
84
+ [formatted_low, formatted_high].compact.join(delimiter)
69
85
  end
70
86
  end
71
87
 
72
- private
73
-
74
88
  def self.code_valid?(code)
75
89
  Money::Currency.stringified_keys.include?(code.downcase)
76
90
  end
77
91
 
92
+ def self.iso_for_currency(code)
93
+ return unless code && code_valid?(code)
94
+
95
+ Money::Currency.new(code).iso_code.tap do |iso_code|
96
+ iso_code.strip! if iso_code.present?
97
+ end
98
+ end
99
+
78
100
  def self.symbol_for_code(code)
79
101
  return unless code && code_valid?(code)
102
+
80
103
  Money::Currency.new(code).symbol.tap do |symbol|
81
104
  symbol.strip! if symbol.present?
82
105
  end
83
106
  end
84
-
85
107
  end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module MoneyHelper
2
+ VERSION = '2.0.0'.freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
2
+ require 'version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'money_helper'
6
+ s.version = MoneyHelper::VERSION
7
+ s.summary = 'MoneyHelper international price formatting utility.'
8
+ s.description = 'A simple module to assist in formatting unambiguous prices and price ranges in international currencies in a Roman Script context.'
9
+ s.authors = ['Sahil Yakhmi', 'Joey Aghion', 'Matt Zikherman', 'Sarah Weir']
10
+ s.email = 'matt@artsymail.com'
11
+ s.platform = Gem::Platform::RUBY
12
+ s.required_rubygems_version = '>= 1.3.6'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- spec/*`.split("\n")
15
+ s.require_paths = ['lib']
16
+ s.homepage = 'https://github.com/artsy/money_helper'
17
+ s.add_dependency('activesupport')
18
+ s.add_dependency('money', '6.16.0')
19
+ s.add_development_dependency('appraisal')
20
+ s.add_development_dependency('rspec', '~> 3')
21
+ s.licenses = ['MIT']
22
+ end
@@ -1,178 +1,237 @@
1
- # encoding: UTF-8
2
-
3
- require 'money_helper'
1
+ require 'spec_helper'
4
2
 
5
3
  describe MoneyHelper do
6
- describe "money_to_text" do
7
- it "includes only symbol for well-recognizable currencies" do
8
- MoneyHelper.money_to_text(30000, "EUR").should eql("€30.000")
9
- MoneyHelper.money_to_text(30000, "GBP").should eql(30,000")
10
- MoneyHelper.money_to_text(30000, "MYR").should eql("RM30,000")
11
- MoneyHelper.money_to_text(30000, "TRY").should eql("TL30.000")
12
- MoneyHelper.money_to_text(30000, "USD").should eql("$30,000")
13
- end
14
- it "includes ISO code and symbol for ambiguous currencies" do
15
- MoneyHelper.money_to_text(30000, "AUD").should eql("AUD $30,000")
16
- MoneyHelper.money_to_text(30000, "ARS").should eql("ARS $30.000")
17
- MoneyHelper.money_to_text(30000, "BBD").should eql("BBD $30,000")
18
- MoneyHelper.money_to_text(30000, "BMD").should eql("BMD $30,000")
19
- MoneyHelper.money_to_text(30000, "BND").should eql("BND $30,000")
20
- MoneyHelper.money_to_text(30000, "BSD").should eql("BSD $30,000")
21
- MoneyHelper.money_to_text(30000, "BZD").should eql("BZD $30,000")
22
- MoneyHelper.money_to_text(30000, "CAD").should eql("CAD $30,000")
23
- MoneyHelper.money_to_text(30000, "CLP").should eql("CLP $30.000")
24
- MoneyHelper.money_to_text(30000, "COP").should eql("COP $30.000")
25
- MoneyHelper.money_to_text(30000, "CUC").should eql("CUC $30,000")
26
- MoneyHelper.money_to_text(30000, "CUP").should eql("CUP $30,000")
27
- MoneyHelper.money_to_text(30000, "DOP").should eql("DOP $30,000")
28
- MoneyHelper.money_to_text(30000, "FJD").should eql("FJD $30,000")
29
- MoneyHelper.money_to_text(30000, "GYD").should eql("GYD $30,000")
30
- MoneyHelper.money_to_text(30000, "HKD").should eql("HKD $30,000")
31
- MoneyHelper.money_to_text(30000, "JMD").should eql("JMD $30,000")
32
- MoneyHelper.money_to_text(30000, "KYD").should eql("KYD $30,000")
33
- MoneyHelper.money_to_text(30000, "LRD").should eql("LRD $30,000")
34
- MoneyHelper.money_to_text(30000, "MXN").should eql("MXN $30,000")
35
- MoneyHelper.money_to_text(30000, "NAD").should eql("NAD $30,000")
36
- MoneyHelper.money_to_text(30000, "NZD").should eql("NZD $30,000")
37
- MoneyHelper.money_to_text(30000, "SBD").should eql("SBD $30,000")
38
- MoneyHelper.money_to_text(30000, "SGD").should eql("SGD $30,000")
39
- MoneyHelper.money_to_text(30000, "SRD").should eql("SRD $30,000")
40
- MoneyHelper.money_to_text(30000, "TWD").should eql("TWD $30,000")
41
- MoneyHelper.money_to_text(30000, "TTD").should eql("TTD $30,000")
42
- MoneyHelper.money_to_text(30000, "UYU").should eql("UYU $30.000")
43
- MoneyHelper.money_to_text(30000, "XCD").should eql("XCD $30,000")
44
- MoneyHelper.money_to_text(30000, "ZWL").should eql("ZWL $30,000")
45
-
46
- MoneyHelper.money_to_text(30000, "FKP").should eql("FKP £30,000")
47
- MoneyHelper.money_to_text(30000, "GIP").should eql("GIP £30,000")
48
- MoneyHelper.money_to_text(30000, "SDG").should eql("SDG £30,000")
49
- MoneyHelper.money_to_text(30000, "SHP").should eql("SHP £30,000")
50
-
51
- MoneyHelper.money_to_text(30000, "CNY").should eql("CNY ¥30,000")
52
- MoneyHelper.money_to_text(30000, "JPY").should eql("JPY ¥30,000")
53
-
54
- MoneyHelper.money_to_text(30000, "ALL").should eql("ALL L30,000")
55
- MoneyHelper.money_to_text(30000, "HNL").should eql("HNL L30,000")
56
- MoneyHelper.money_to_text(30000, "LSL").should eql("LSL L30,000")
57
- MoneyHelper.money_to_text(30000, "MDL").should eql("MDL L30,000")
58
- MoneyHelper.money_to_text(30000, "RON").should eql("RON 30.000")
59
- MoneyHelper.money_to_text(30000, "SZL").should eql("SZL L30,000")
60
-
61
- MoneyHelper.money_to_text(30000, "ANG").should eql("ANG ƒ30.000")
62
- MoneyHelper.money_to_text(30000, "AWG").should eql("AWG ƒ30,000")
63
-
64
- MoneyHelper.money_to_text(30000, "BWP").should eql("BWP P30,000")
65
- MoneyHelper.money_to_text(30000, "MOP").should eql("MOP P30,000")
66
-
67
- MoneyHelper.money_to_text(30000, "CRC").should eql("CRC ₡30.000")
68
- MoneyHelper.money_to_text(30000, "SVC").should eql("SVC 30,000")
69
-
70
- MoneyHelper.money_to_text(30000, "MUR").should eql("MUR ₨30,000")
71
- MoneyHelper.money_to_text(30000, "NPR").should eql("NPR ₨30,000")
72
- MoneyHelper.money_to_text(30000, "PKR").should eql("PKR ₨30,000")
73
- MoneyHelper.money_to_text(30000, "SCR").should eql("SCR 30,000")
74
- end
75
- it "includes ISO code and symbol for difficult to recognize symbols" do
76
- MoneyHelper.money_to_text(30000, "AMD").should eql("AMD դր.30,000")
77
- MoneyHelper.money_to_text(30000, "BDT").should eql("BDT 30,000")
78
- MoneyHelper.money_to_text(30000, "BRL").should eql("BRL R$30.000")
79
- MoneyHelper.money_to_text(30000, "GMD").should eql("GMD D30,000")
80
- MoneyHelper.money_to_text(30000, "GEL").should eql("GEL ლ30,000")
81
- MoneyHelper.money_to_text(30000, "GHS").should eql("GHS ₵30,000")
82
- MoneyHelper.money_to_text(30000, "GTQ").should eql("GTQ Q30,000")
83
- MoneyHelper.money_to_text(30000, "HTG").should eql("HTG G30,000")
84
- MoneyHelper.money_to_text(30000, "IDR").should eql("IDR Rp30.000")
85
- MoneyHelper.money_to_text(30000, "ILS").should eql("ILS 30,000")
86
- MoneyHelper.money_to_text(30000, "INR").should eql("INR 30,000")
87
- MoneyHelper.money_to_text(30000, "KHR").should eql("KHR 30,000")
88
- MoneyHelper.money_to_text(30000, "KPW").should eql("KPW 30,000")
89
- MoneyHelper.money_to_text(30000, "LAK").should eql("LAK 30,000")
90
- MoneyHelper.money_to_text(30000, "MNT").should eql("MNT ₮30,000")
91
- MoneyHelper.money_to_text(30000, "NIO").should eql("NIO C$30,000")
92
- MoneyHelper.money_to_text(30000, "NGN").should eql("NGN ₦30,000")
93
- MoneyHelper.money_to_text(30000, "PGK").should eql("PGK K30,000")
94
- MoneyHelper.money_to_text(30000, "PHP").should eql("PHP 30,000")
95
- MoneyHelper.money_to_text(30000, "PYG").should eql("PYG ₲30,000")
96
- MoneyHelper.money_to_text(30000, "RUB").should eql("RUB р.30.000")
97
- MoneyHelper.money_to_text(30000, "THB").should eql("THB ฿30,000")
98
- MoneyHelper.money_to_text(30000, "TOP").should eql("TOP T$30,000")
99
- MoneyHelper.money_to_text(30000, "TMT").should eql("TMT m30,000")
100
- MoneyHelper.money_to_text(30000, "UAH").should eql("UAH ₴30,000")
101
- MoneyHelper.money_to_text(30000, "VND").should eql("VND ₫30.000")
102
- MoneyHelper.money_to_text(30000, "WST").should eql("WST T30,000")
103
- MoneyHelper.money_to_text(30000, "ZAR").should eql("ZAR R30,000")
104
- end
105
- it "includes only ISO code for currencies that don't have a symbol" do
106
- MoneyHelper.money_to_text(30000, "UZS").should eql("UZS 30,000")
107
- end
108
- it "includes only ISO code for RTL symbols" do
109
- MoneyHelper.money_to_text(30000, "AFN").should eql("AFN 30,000")
110
- MoneyHelper.money_to_text(30000, "DZD").should eql("DZD 30,000")
111
- MoneyHelper.money_to_text(30000, "BHD").should eql("BHD 30,000")
112
- MoneyHelper.money_to_text(30000, "EGP").should eql("EGP 30,000")
113
- MoneyHelper.money_to_text(30000, "IRR").should eql("IRR 30,000")
114
- MoneyHelper.money_to_text(30000, "IQD").should eql("IQD 30,000")
115
- MoneyHelper.money_to_text(30000, "JOD").should eql("JOD 30,000")
116
- MoneyHelper.money_to_text(30000, "KWD").should eql("KWD 30,000")
117
- MoneyHelper.money_to_text(30000, "LBP").should eql("LBP 30,000")
118
- MoneyHelper.money_to_text(30000, "LYD").should eql("LYD 30,000")
119
- MoneyHelper.money_to_text(30000, "MAD").should eql("MAD 30,000")
120
- MoneyHelper.money_to_text(30000, "OMR").should eql("OMR 30,000")
121
- MoneyHelper.money_to_text(30000, "JOD").should eql("JOD 30,000")
122
- MoneyHelper.money_to_text(30000, "QAR").should eql("QAR 30,000")
123
- MoneyHelper.money_to_text(30000, "SAR").should eql("SAR 30,000")
124
- MoneyHelper.money_to_text(30000, "SYP").should eql("SYP 30,000")
125
- MoneyHelper.money_to_text(30000, "TND").should eql("TND 30,000")
126
- MoneyHelper.money_to_text(30000, "AED").should eql("AED 30,000")
127
- MoneyHelper.money_to_text(30000, "YER").should eql("YER 30,000")
128
- end
129
- it "defaults to USD when an empty string or nil is passed as the currency" do
130
- MoneyHelper.money_to_text(30000, "").should eql("$30,000")
131
- MoneyHelper.money_to_text(30000, nil).should eql("$30,000")
132
- end
133
- it "returns only the formatted numeral when number_only = true" do
134
- MoneyHelper.money_to_text(30000, "EUR", true).should eql("30.000")
135
- MoneyHelper.money_to_text(30000, "AUD", true).should eql("30,000")
136
- MoneyHelper.money_to_text(30000, "AMD", true).should eql("30,000")
137
- MoneyHelper.money_to_text(30000, "AFN", true).should eql("30,000")
138
- end
139
- it "returns nil if amount passed in is whitespace, empty string, or nil" do
140
- MoneyHelper.money_to_text(" ", "USD").should be_nil
141
- MoneyHelper.money_to_text("", "USD").should be_nil
142
- MoneyHelper.money_to_text(nil, "USD").should be_nil
143
- end
144
- it "falls back to ISO code when currency can't be found" do
145
- MoneyHelper.money_to_text(10_000, "ITL").should eql("ITL 10,000")
146
- MoneyHelper.money_to_text(10_000, "ITL", true).should eql("10,000")
4
+ describe 'money_to_text' do
5
+ it 'includes ISO code and symbol' do
6
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD')).to eql('AUD $30,175.93')
7
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ARS')).to eql('ARS $30.175,93')
8
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BBD')).to eql('BBD $30,175.93')
9
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BMD')).to eql('BMD $30,175.93')
10
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BND')).to eql('BND $30,175.93')
11
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BSD')).to eql('BSD $30,175.93')
12
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BZD')).to eql('BZD $30,175.93')
13
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CAD')).to eql('CAD $30,175.93')
14
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CLP')).to eql('CLP $3.017.593')
15
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'COP')).to eql('COP $30.175,93')
16
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CUC')).to eql('CUC $30,175.93')
17
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CUP')).to eql('CUP $30,175.93')
18
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'DOP')).to eql('DOP $30,175.93')
19
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'FJD')).to eql('FJD $30,175.93')
20
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GYD')).to eql('GYD $30,175.93')
21
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HKD')).to eql('HKD $30,175.93')
22
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JMD')).to eql('JMD $30,175.93')
23
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KYD')).to eql('KYD $30,175.93')
24
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LRD')).to eql('LRD $30,175.93')
25
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MXN')).to eql('MXN $30,175.93')
26
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NAD')).to eql('NAD $30,175.93')
27
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NZD')).to eql('NZD $30,175.93')
28
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SBD')).to eql('SBD $30,175.93')
29
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SGD')).to eql('SGD $30,175.93')
30
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SRD')).to eql('SRD $30,175.93')
31
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TWD')).to eql('TWD $30,175.93')
32
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TTD')).to eql('TTD $30,175.93')
33
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'UYU')).to eql('UYU $U30.175,93')
34
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
35
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'XCD')).to eql('XCD $30,175.93')
36
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ZWL')).to eql('ZWL $30,175.93')
37
+
38
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR')).to eql('EUR 30.175,93')
39
+
40
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'FKP')).to eql('FKP £30,175.93')
41
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GBP')).to eql('GBP £30,175.93')
42
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GIP')).to eql('GIP £30,175.93')
43
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SDG')).to eql('SDG £30,175.93')
44
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SHP')).to eql('SHP £30,175.93')
45
+
46
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CNY')).to eql('CNY ¥30,175.93')
47
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JPY')).to eql('JPY ¥3,017,593')
48
+
49
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ALL')).to eql('ALL L30,175.93')
50
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HNL')).to eql('HNL L30,175.93')
51
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LSL')).to eql('LSL L30,175.93')
52
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MDL')).to eql('MDL L30,175.93')
53
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MYR')).to eql('MYR RM30,175.93')
54
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'RON')).to eql('RON Lei30.175,93')
55
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SZL')).to eql('SZL E30,175.93')
56
+
57
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ANG')).to eql('ANG ƒ30.175,93')
58
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AWG')).to eql('AWG ƒ30,175.93')
59
+
60
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BWP')).to eql('BWP P30,175.93')
61
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MOP')).to eql('MOP P30,175.93')
62
+
63
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'CRC')).to eql('CRC ₡30.175,93')
64
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SVC')).to eql('SVC ₡30,175.93')
65
+
66
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MUR')).to eql('MUR 30,175.93')
67
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NPR')).to eql('NPR Rs.30,175.93')
68
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PKR')).to eql('PKR ₨30,175.93')
69
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SCR')).to eql('SCR ₨30,175.93')
70
+
71
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD')).to eql('AMD դր.30,175.93')
72
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BDT')).to eql('BDT ৳30,175.93')
73
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BRL')).to eql('BRL R$30.175,93')
74
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GEL')).to eql('GEL 30,175.93')
75
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GHS')).to eql('GHS 30,175.93')
76
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GMD')).to eql('GMD D30,175.93')
77
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'GTQ')).to eql('GTQ Q30,175.93')
78
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'HTG')).to eql('HTG G30,175.93')
79
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IDR')).to eql('IDR Rp30.175,93')
80
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ILS')).to eql('ILS ₪30,175.93')
81
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'INR')).to eql('INR ₹30,175.93')
82
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KHR')).to eql('KHR ៛30,175.93')
83
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'KPW')).to eql('KPW 30,175.93')
84
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LAK')).to eql('LAK 30,175.93')
85
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MNT')).to eql('MNT 30,175.93')
86
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NGN')).to eql('NGN 30,175.93')
87
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'NIO')).to eql('NIO C$30,175.93')
88
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PGK')).to eql('PGK K30,175.93')
89
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PHP')).to eql('PHP 30,175.93')
90
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'PYG')).to eql('PYG ₲3,017,593')
91
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'RUB')).to eql('RUB ₽30.175,93')
92
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'THB')).to eql('THB ฿30,175.93')
93
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TMT')).to eql('TMT T30,175.93')
94
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TOP')).to eql('TOP T$30,175.93')
95
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TRY')).to eql('TRY 30.175,93')
96
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'UAH')).to eql('UAH 30,175.93')
97
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'VND')).to eql('VND ₫3.017.593')
98
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'WST')).to eql('WST T30,175.93')
99
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'ZAR')).to eql('ZAR R30,175.93')
100
+ end
101
+
102
+ it "respects currency's thousands separator and decimal mark" do
103
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
104
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR')).to eql('EUR 30.175,93')
105
+ end
106
+
107
+ it "converts from currency's minor unit to major unit" do
108
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'USD')).to eql('USD $30,175.93')
109
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JPY')).to eql('JPY ¥3,017,593')
110
+ end
111
+
112
+ it 'displays LTR for RTL symbols' do
113
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN')).to eql('AFN ؋30,175.93')
114
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'DZD')).to eql('DZD د.ج30,175.93')
115
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'BHD')).to eql('BHD د.ب3,017.593')
116
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EGP')).to eql('EGP ج.م30,175.93')
117
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IRR')).to eql('IRR 30,175.93')
118
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'IQD')).to eql('IQD ع.د3,017.593')
119
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'JOD')).to eql('JOD د.ا3,017.593')
120
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'LYD')).to eql('LYD ل.د3,017.593')
121
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'MAD')).to eql('MAD د.م.30,175.93')
122
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'QAR')).to eql('QAR ر.ق30,175.93')
123
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'SYP')).to eql('SYP £S30,175.93')
124
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'TND')).to eql('TND د.ت3,017.593')
125
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AED')).to eql('AED د.إ30,175.93')
126
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'YER')).to eql('YER ﷼30,175.93')
127
+ end
128
+
129
+ it 'defaults to USD' do
130
+ expect(MoneyHelper.money_to_text(30_175_93)).to eql('USD $30,175.93')
131
+ end
132
+
133
+ it 'omits ISO code when with_currency is false' do
134
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_currency: false)).to eql('€30.175,93')
135
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', with_currency: false)).to eql('$30,175.93')
136
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', with_currency: false)).to eql('դր.30,175.93')
137
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', with_currency: false)).to eql('؋30,175.93')
138
+ end
139
+
140
+ it 'omits symbol when with_symbol is false' do
141
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_symbol: false)).to eql('EUR 30.175,93')
142
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', with_symbol: false)).to eql('AUD 30,175.93')
143
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', with_symbol: false)).to eql('AMD 30,175.93')
144
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', with_symbol: false)).to eql('AFN 30,175.93')
145
+ end
146
+
147
+ it 'omits ISO code and symbol when both with_currency and with_symbol are false' do
148
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'EUR', with_currency: false, with_symbol: false)).to eql('30.175,93')
149
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AUD', with_currency: false, with_symbol: false)).to eql('30,175.93')
150
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AMD', with_currency: false, with_symbol: false)).to eql('30,175.93')
151
+ expect(MoneyHelper.money_to_text(30_175_93, currency: 'AFN', with_currency: false, with_symbol: false)).to eql('30,175.93')
152
+ end
153
+
154
+ it 'returns an empty string if amount passed in is whitespace, empty string, or nil' do
155
+ expect(MoneyHelper.money_to_text(' ')).to eql('')
156
+ expect(MoneyHelper.money_to_text('')).to eql('')
157
+ expect(MoneyHelper.money_to_text(nil)).to eql('')
158
+ end
159
+
160
+ it "raises an exception when currency can't be found" do
161
+ expect do
162
+ MoneyHelper.money_to_text(30_175_93, currency: 'AAA')
163
+ end.to raise_error(Money::Currency::UnknownCurrency, "Unknown currency 'aaa'")
164
+ end
165
+ end
166
+
167
+ describe 'symbol_with_optional_iso_code' do
168
+ it 'just includes the symbol for USD GBP EUR and MYR' do
169
+ expect(MoneyHelper.symbol_with_optional_iso_code('EUR')).to eql('€')
170
+ expect(MoneyHelper.symbol_with_optional_iso_code('GBP')).to eql('£')
171
+ expect(MoneyHelper.symbol_with_optional_iso_code('MYR')).to eql('RM')
172
+ expect(MoneyHelper.symbol_with_optional_iso_code('USD')).to eql('$')
173
+ end
174
+
175
+ it 'includes the iso code as well for other currencies' do
176
+ expect(MoneyHelper.symbol_with_optional_iso_code('AUD')).to eql('AUD $')
177
+ expect(MoneyHelper.symbol_with_optional_iso_code('UZS')).to eql('UZS')
178
+ expect(MoneyHelper.symbol_with_optional_iso_code('JPY')).to eql('JPY ¥')
147
179
  end
148
180
  end
149
- describe "money_range_to_text" do
150
- it "includes no indicator for currency for the upper amount in range" do
151
- MoneyHelper.money_range_to_text(30000, 40000, "USD").should eql("$30,000 - 40,000")
152
- MoneyHelper.money_range_to_text(30000, 40000, "AUD").should eql("AUD $30,000 - 40,000")
153
- MoneyHelper.money_range_to_text(30000, 40000, "AMD").should eql("AMD դր.30,000 - 40,000")
154
- MoneyHelper.money_range_to_text(30000, 40000, "AFN").should eql("AFN 30,000 - 40,000")
155
- end
156
- it "uses the special range amount delimeter when supplied" do
157
- MoneyHelper.money_range_to_text(30000, 40000, "USD", "-").should eql("$30,000-40,000")
158
- MoneyHelper.money_range_to_text(30000, 40000, "AUD", "-").should eql("AUD $30,000-40,000")
159
- MoneyHelper.money_range_to_text(30000, 40000, "AMD", "-").should eql("AMD դր.30,000-40,000")
160
- MoneyHelper.money_range_to_text(30000, 40000, "AFN", "-").should eql("AFN 30,000-40,000")
181
+
182
+ describe 'money_range_to_text' do
183
+ it 'includes no indicator for currency for the upper amount in range' do
184
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD')).to eql('USD $30,175.93 - 40,983.27')
185
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AUD')).to eql('AUD $30,175.93 - 40,983.27')
186
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AMD')).to eql('AMD դր.30,175.93 - 40,983.27')
187
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AFN')).to eql('AFN ؋30,175.93 - 40,983.27')
188
+ end
189
+
190
+ it 'uses the special range amount delimeter when supplied' do
191
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'USD', delimiter: '-')).to eql('USD $30,175.93-40,983.27')
192
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AUD', delimiter: '-')).to eql('AUD $30,175.93-40,983.27')
193
+ expect(MoneyHelper.money_range_to_text(30_175_93, 40_983_27, currency: 'AMD', delimiter: '-')).to eql('AMD դր.30,175.93-40,983.27')
194
+ 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
195
  end
196
+
162
197
  it "prefixes the text 'Under ' when low amount not given" do
163
- MoneyHelper.money_range_to_text(nil, 40000, "USD").should eql("Under $40,000")
198
+ expect(MoneyHelper.money_range_to_text(nil, 40_983_27, currency: 'USD')).to eql('Under USD $40,983.27')
164
199
  end
200
+
165
201
  it "appends the text ' and up' when high amount not given" do
166
- MoneyHelper.money_range_to_text(30000, nil, "USD").should eql("$30,000 and up")
202
+ expect(MoneyHelper.money_range_to_text(30_175_93, nil, currency: 'USD')).to eql('USD $30,175.93 and up')
203
+ end
204
+
205
+ it 'treats as a single price when low amount and high amount are identical' do
206
+ expect(MoneyHelper.money_range_to_text(30_175_93, 30_175_93, currency: 'USD')).to eql('USD $30,175.93')
207
+ end
208
+
209
+ it 'returns empty string when both amounts are nil' do
210
+ expect(MoneyHelper.money_range_to_text(nil, nil, currency: 'USD')).to eql('')
167
211
  end
168
- it "treats as a single price when low amount and high amount are identical" do
169
- MoneyHelper.money_range_to_text(30000, 30000, "USD").should eql("$30,000")
212
+
213
+ it "raises an exception when currency can't be found" do
214
+ expect do
215
+ MoneyHelper.money_range_to_text(10_000, 20_000, currency: 'ITL')
216
+ end.to raise_error(Money::Currency::UnknownCurrency, "Unknown currency 'itl'")
217
+ end
218
+ end
219
+
220
+ describe 'symbol_with_optional_iso_code' do
221
+ it 'returns the symbol only if currency is in SYMBOL_ONLY list' do
222
+ expect(MoneyHelper.symbol_with_optional_iso_code('EUR')).to eql('€')
223
+ expect(MoneyHelper.symbol_with_optional_iso_code('USD')).to eql('$')
170
224
  end
171
- it "returns nil when both amounts are nil" do
172
- MoneyHelper.money_range_to_text(nil, nil, "USD").should be_nil
225
+
226
+ it 'returns iso code and symbol if symbol is in OK_SYMBOLS' do
227
+ expect(MoneyHelper.symbol_with_optional_iso_code('INR')).to eql('INR ₹')
228
+ expect(MoneyHelper.symbol_with_optional_iso_code('KHR')).to eql('KHR ៛')
229
+ expect(MoneyHelper.symbol_with_optional_iso_code('KPW')).to eql('KPW ₩')
173
230
  end
174
- it "falls back to ISO code when currency can't be found" do
175
- MoneyHelper.money_range_to_text(10_000, 20_000, "ITL").should eql("ITL 10,000 - 20,000")
231
+
232
+ it 'returns only the iso code if symbol is not in OK_SYMBOLS' do
233
+ expect(MoneyHelper.symbol_with_optional_iso_code('CHF')).to eql('CHF')
234
+ expect(MoneyHelper.symbol_with_optional_iso_code('YER')).to eql('YER')
176
235
  end
177
236
  end
178
237
  end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+
6
+ require 'money_helper'
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe MoneyHelper do
4
+ it 'has a version' do
5
+ expect(MoneyHelper::VERSION).to_not be nil
6
+ end
7
+ end
metadata CHANGED
@@ -1,90 +1,129 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 2.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: 2014-04-12 00:00:00.000000000 Z
14
+ date: 2021-09-20 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.0'
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.0'
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: '3.0'
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
+ - - '='
40
42
  - !ruby/object:Gem::Version
41
- version: '3.0'
43
+ version: 6.16.0
42
44
  - !ruby/object:Gem::Dependency
43
- name: rspec
45
+ name: appraisal
44
46
  requirement: !ruby/object:Gem::Requirement
45
47
  requirements:
46
- - - ! '>='
48
+ - - ">="
47
49
  - !ruby/object:Gem::Version
48
50
  version: '0'
49
51
  type: :development
50
52
  prerelease: false
51
53
  version_requirements: !ruby/object:Gem::Requirement
52
54
  requirements:
53
- - - ! '>='
55
+ - - ">="
54
56
  - !ruby/object:Gem::Version
55
57
  version: '0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - "~>"
63
+ - !ruby/object:Gem::Version
64
+ version: '3'
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
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
+ - gemfiles/activesupport_40.gemfile
91
+ - gemfiles/activesupport_41.gemfile
92
+ - gemfiles/activesupport_42.gemfile
93
+ - gemfiles/activesupport_50.gemfile
94
+ - gemfiles/activesupport_51.gemfile
95
+ - gemfiles/activesupport_52.gemfile
96
+ - gemfiles/activesupport_edge.gemfile
63
97
  - lib/money_helper.rb
98
+ - lib/version.rb
99
+ - money_helper.gemspec
64
100
  - spec/money_helper_spec.rb
65
- homepage: https://github.com/syakhmi/money_helper
101
+ - spec/spec_helper.rb
102
+ - spec/version_spec.rb
103
+ homepage: https://github.com/artsy/money_helper
66
104
  licenses:
67
105
  - MIT
68
106
  metadata: {}
69
- post_install_message:
107
+ post_install_message:
70
108
  rdoc_options: []
71
109
  require_paths:
72
110
  - lib
73
111
  required_ruby_version: !ruby/object:Gem::Requirement
74
112
  requirements:
75
- - - ! '>='
113
+ - - ">="
76
114
  - !ruby/object:Gem::Version
77
115
  version: '0'
78
116
  required_rubygems_version: !ruby/object:Gem::Requirement
79
117
  requirements:
80
- - - ! '>='
118
+ - - ">="
81
119
  - !ruby/object:Gem::Version
82
- version: '0'
120
+ version: 1.3.6
83
121
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.1.10
86
- signing_key:
122
+ rubygems_version: 3.1.4
123
+ signing_key:
87
124
  specification_version: 4
88
- summary: MoneyHelper international price formatting utility
125
+ summary: MoneyHelper international price formatting utility.
89
126
  test_files:
90
127
  - spec/money_helper_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/version_spec.rb