money_helper 1.0.0 → 1.0.1

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
+ SHA1:
3
+ metadata.gz: 0ec1d057d296f1729ab55b1becee98afad5c50b7
4
+ data.tar.gz: 786a1ae33bf67e9d1215be82a7856c0138d5c292
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: a3ee33d19f4019bfae4b788fb62ca1ccf11d7264c6794b2f5acd5a39cb3ed3ceb967d9897d94c3e6d85ccd99a0e9416d298f688052ad2a6e1ab12affd356ac2f
7
+ data.tar.gz: 038e37ac696e39834b2a88cd00bdc43fd0771aed279a9dde5d499a5e719e6e46a1a9e3ffbe7d3effe907175163f72c93b713fd656680c634810b1a2f01cfbe9c
data/lib/money_helper.rb CHANGED
@@ -25,19 +25,27 @@ module MoneyHelper
25
25
  # currency: (String)
26
26
  # number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
27
27
  # specific to currency)
28
- def self.money_to_text(amount, currency, number_only = false)
28
+ def self.money_to_text(amount, currency, number_only = false, options = {})
29
29
  return nil unless amount.present?
30
30
  currency = "USD" if currency.blank?
31
31
  valid_currency = code_valid?(currency) ? currency : "USD"
32
32
  symbol = symbol_for_code(currency)
33
33
  include_symbol = !number_only && symbol.present? && OK_SYMBOLS.include?(symbol)
34
34
  subunit_factor = Money::Currency.new(valid_currency).subunit_to_unit
35
+ money_options = { no_cents: true, symbol_position: :before, symbol: include_symbol }.merge(options)
35
36
  (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(' ')
37
+ Money.new(amount*subunit_factor.ceil, valid_currency).format(money_options).delete(' ')
38
+ end
39
+
40
+ def self.symbol_with_optional_iso_code(currency = 'USD')
41
+ symbol = symbol_for_code(currency)
42
+ if SYMBOL_ONLY.include?(currency)
43
+ symbol
44
+ elsif symbol && OK_SYMBOLS.include?(symbol)
45
+ "#{iso_for_currency(currency)} #{symbol}"
46
+ else
47
+ "#{iso_for_currency(currency)}"
48
+ end
41
49
  end
42
50
 
43
51
  ##
@@ -76,11 +84,17 @@ module MoneyHelper
76
84
  Money::Currency.stringified_keys.include?(code.downcase)
77
85
  end
78
86
 
87
+ def self.iso_for_currency(code)
88
+ return unless code && code_valid?(code)
89
+ Money::Currency.new(code).iso_code.tap do |iso_code|
90
+ iso_code.strip! if iso_code.present?
91
+ end
92
+ end
93
+
79
94
  def self.symbol_for_code(code)
80
95
  return unless code && code_valid?(code)
81
96
  Money::Currency.new(code).symbol.tap do |symbol|
82
97
  symbol.strip! if symbol.present?
83
98
  end
84
99
  end
85
-
86
100
  end
@@ -145,6 +145,23 @@ describe MoneyHelper do
145
145
  expect(MoneyHelper.money_to_text(10_000, "ITL")).to eql("ITL 10,000")
146
146
  expect(MoneyHelper.money_to_text(10_000, "ITL", true)).to eql("10,000")
147
147
  end
148
+ it 'allows options to be passed through and cents displayed' do
149
+ expect(MoneyHelper.money_to_text(10_000.1, 'USD', nil, no_cents: false)).to eq '$10,000.10'
150
+ expect(MoneyHelper.money_to_text(10_000.1, 'USD')).to eq '$10,000'
151
+ end
152
+ end
153
+ describe 'symbol_with_optional_iso_code' do
154
+ it 'just includes the symbol for USD GBP EUR and MYR' do
155
+ expect(MoneyHelper.symbol_with_optional_iso_code("EUR")).to eql("€")
156
+ expect(MoneyHelper.symbol_with_optional_iso_code("GBP")).to eql("£")
157
+ expect(MoneyHelper.symbol_with_optional_iso_code("MYR")).to eql("RM")
158
+ expect(MoneyHelper.symbol_with_optional_iso_code("USD")).to eql("$")
159
+ end
160
+ it 'includes the iso code as well for other currencies' do
161
+ expect(MoneyHelper.symbol_with_optional_iso_code("AUD")).to eql("AUD $")
162
+ expect(MoneyHelper.symbol_with_optional_iso_code("UZS")).to eql("UZS")
163
+ expect(MoneyHelper.symbol_with_optional_iso_code("JPY")).to eql("JPY ¥")
164
+ end
148
165
  end
149
166
  describe "money_range_to_text" do
150
167
  it "includes no indicator for currency for the upper amount in range" do
@@ -175,4 +192,19 @@ describe MoneyHelper do
175
192
  expect(MoneyHelper.money_range_to_text(10_000, 20_000, "ITL")).to eql("ITL 10,000 - 20,000")
176
193
  end
177
194
  end
195
+ describe "symbol_with_optional_iso_code" do
196
+ it "returns the symbol only if currency is in SYMBOL_ONLY list" do
197
+ expect(MoneyHelper.symbol_with_optional_iso_code("EUR")).to eql("€")
198
+ expect(MoneyHelper.symbol_with_optional_iso_code("USD")).to eql("$")
199
+ end
200
+ it "returns iso code and symbol if symbol is in OK_SYMBOLS" do
201
+ expect(MoneyHelper.symbol_with_optional_iso_code("INR")).to eql("INR ₹")
202
+ expect(MoneyHelper.symbol_with_optional_iso_code("KHR")).to eql("KHR ៛")
203
+ expect(MoneyHelper.symbol_with_optional_iso_code("KPW")).to eql("KPW ₩")
204
+ end
205
+ it "returns only the iso code if symbol is not in OK_SYMBOLS" do
206
+ expect(MoneyHelper.symbol_with_optional_iso_code("CHF")).to eql("CHF")
207
+ expect(MoneyHelper.symbol_with_optional_iso_code("YER")).to eql("YER")
208
+ end
209
+ end
178
210
  end
metadata CHANGED
@@ -1,11 +1,13 @@
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: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sahil Yakhmi
8
8
  - Joey Aghion
9
+ - Matt Zikherman
10
+ - Sarah Weir
9
11
  autorequire:
10
12
  bindir: bin
11
13
  cert_chain: []
@@ -29,16 +31,16 @@ dependencies:
29
31
  name: activesupport
30
32
  requirement: !ruby/object:Gem::Requirement
31
33
  requirements:
32
- - - ~>
34
+ - - '>='
33
35
  - !ruby/object:Gem::Version
34
- version: '4'
36
+ version: '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: '4'
43
+ version: '0'
42
44
  - !ruby/object:Gem::Dependency
43
45
  name: rspec
44
46
  requirement: !ruby/object:Gem::Requirement
@@ -55,14 +57,14 @@ dependencies:
55
57
  version: '3'
56
58
  description: A simple module to assist in formatting unambiguous prices and price
57
59
  ranges in international currencies in a Roman Script context.
58
- email: sahil@artsymail.net
60
+ email: matt@artsymail.net
59
61
  executables: []
60
62
  extensions: []
61
63
  extra_rdoc_files: []
62
64
  files:
63
65
  - lib/money_helper.rb
64
66
  - spec/money_helper_spec.rb
65
- homepage: https://github.com/syakhmi/money_helper
67
+ homepage: https://github.com/artsy/money_helper
66
68
  licenses:
67
69
  - MIT
68
70
  metadata: {}
@@ -72,12 +74,12 @@ require_paths:
72
74
  - lib
73
75
  required_ruby_version: !ruby/object:Gem::Requirement
74
76
  requirements:
75
- - - ! '>='
77
+ - - '>='
76
78
  - !ruby/object:Gem::Version
77
79
  version: '0'
78
80
  required_rubygems_version: !ruby/object:Gem::Requirement
79
81
  requirements:
80
- - - ! '>='
82
+ - - '>='
81
83
  - !ruby/object:Gem::Version
82
84
  version: '0'
83
85
  requirements: []
@@ -88,3 +90,4 @@ specification_version: 4
88
90
  summary: MoneyHelper international price formatting utility
89
91
  test_files:
90
92
  - spec/money_helper_spec.rb
93
+ has_rdoc: