money_helper 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/money_helper.rb +19 -12
  2. data/spec/money_helper_spec.rb +174 -0
  3. metadata +64 -7
data/lib/money_helper.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  # encoding: UTF-8
2
2
 
3
+ require 'active_support/core_ext/object/blank'
4
+ require 'money'
5
+
3
6
  module MoneyHelper
4
7
 
5
- SYMBOL_ONLY = %w{USD GBP EUR RM TL} #don't use ISO code
8
+ SYMBOL_ONLY = %w{USD GBP EUR MYR TRY} #don't use ISO code
6
9
  OK_SYMBOLS = %w{
7
- $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G L ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր C$ ₦ TL K ₲ ₱ T ฿ T$ m ₴ ₫
10
+ $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր. C$ ₦ TL ₲ ₱ T ฿ T$ m ₴ ₫
8
11
  } #ok to include in string
9
12
 
10
13
  ##
11
- # Formats a single amount in the given currency into a price string
14
+ # Formats a single amount in the given currency into a price string. Defaults to USD if currency not
15
+ # given.
12
16
  #
13
17
  # = Example
14
18
  #
@@ -18,16 +22,19 @@ module MoneyHelper
18
22
  #
19
23
  # amount: (Float)
20
24
  # currency: (String)
21
- # number_only: (Boolean) optional flag to exclude currency indicators
25
+ # number_only: (Boolean) optional flag to exclude currency indicators (retains number formatting
26
+ # specific to currency)
22
27
  def self.money_to_text(amount, currency, number_only = false)
23
28
  return nil unless amount.present?
24
- currency_obj = Money::Currency.new(currency)
25
- (number_only || SYMBOL_ONLY.include?(currency) ? '' : currency + ' ') +
26
- Money.parse(amount.ceil, currency.presence).format({
29
+ currency = "USD" if currency.blank?
30
+ symbol = Money::Currency.new(currency).symbol.strip
31
+ include_symbol = !number_only && OK_SYMBOLS.include?(symbol)
32
+ (number_only || SYMBOL_ONLY.include?(currency) ? "" : currency + " ") +
33
+ Money.parse(amount.ceil, currency).format({
27
34
  no_cents: true,
28
35
  symbol_position: :before,
29
- symbol: (!number_only && OK_SYMBOLS.include?(currency_obj.symbol))
30
- })
36
+ symbol: include_symbol
37
+ }).delete(' ')
31
38
  end
32
39
 
33
40
  ##
@@ -47,11 +54,11 @@ module MoneyHelper
47
54
  # currency: (String)
48
55
  # delimiter: (String) optional
49
56
  def self.money_range_to_text(low, high, currency, delimiter = ' - ')
50
- if low.nil? && high.nil?
57
+ if low.blank? && high.blank?
51
58
  nil
52
- elsif low.nil?
59
+ elsif low.blank?
53
60
  "Under " + money_to_text(high, currency)
54
- elsif high.nil?
61
+ elsif high.blank?
55
62
  money_to_text(low, currency) + " and up"
56
63
  elsif low == high
57
64
  money_to_text(low, currency)
@@ -0,0 +1,174 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'money_helper'
4
+
5
+ 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 L30.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 RTL symbols" do
106
+ MoneyHelper.money_to_text(30000, "AFN").should eql("AFN 30,000")
107
+ MoneyHelper.money_to_text(30000, "DZD").should eql("DZD 30,000")
108
+ MoneyHelper.money_to_text(30000, "BHD").should eql("BHD 30,000")
109
+ MoneyHelper.money_to_text(30000, "EGP").should eql("EGP 30,000")
110
+ MoneyHelper.money_to_text(30000, "IRR").should eql("IRR 30,000")
111
+ MoneyHelper.money_to_text(30000, "IQD").should eql("IQD 30,000")
112
+ MoneyHelper.money_to_text(30000, "JOD").should eql("JOD 30,000")
113
+ MoneyHelper.money_to_text(30000, "KWD").should eql("KWD 30,000")
114
+ MoneyHelper.money_to_text(30000, "LBP").should eql("LBP 30,000")
115
+ MoneyHelper.money_to_text(30000, "LYD").should eql("LYD 30,000")
116
+ MoneyHelper.money_to_text(30000, "MAD").should eql("MAD 30,000")
117
+ MoneyHelper.money_to_text(30000, "OMR").should eql("OMR 30,000")
118
+ MoneyHelper.money_to_text(30000, "JOD").should eql("JOD 30,000")
119
+ MoneyHelper.money_to_text(30000, "QAR").should eql("QAR 30,000")
120
+ MoneyHelper.money_to_text(30000, "SAR").should eql("SAR 30,000")
121
+ MoneyHelper.money_to_text(30000, "SYP").should eql("SYP 30,000")
122
+ MoneyHelper.money_to_text(30000, "TND").should eql("TND 30,000")
123
+ MoneyHelper.money_to_text(30000, "AED").should eql("AED 30,000")
124
+ MoneyHelper.money_to_text(30000, "YER").should eql("YER 30,000")
125
+ end
126
+ it "defaults to USD when an empty string or nil is passed as the currency" do
127
+ MoneyHelper.money_to_text(30000, "").should eql("$30,000")
128
+ MoneyHelper.money_to_text(30000, nil).should eql("$30,000")
129
+ end
130
+ it "returns only formatted numeral when number_only = true" do
131
+ MoneyHelper.money_to_text(30000, "EUR", true).should eql("30.000")
132
+ MoneyHelper.money_to_text(30000, "AUD", true).should eql("30,000")
133
+ MoneyHelper.money_to_text(30000, "AMD", true).should eql("30,000")
134
+ MoneyHelper.money_to_text(30000, "AFN", true).should eql("30,000")
135
+ end
136
+ it "returns only the formatted numeral when number_only = true" do
137
+ MoneyHelper.money_to_text(30000, "EUR", true).should eql("30.000")
138
+ MoneyHelper.money_to_text(30000, "AUD", true).should eql("30,000")
139
+ MoneyHelper.money_to_text(30000, "AMD", true).should eql("30,000")
140
+ MoneyHelper.money_to_text(30000, "AFN", true).should eql("30,000")
141
+ end
142
+ it "returns nil if amount passed in is whitespace, empty string, or nil" do
143
+ MoneyHelper.money_to_text(" ", "USD").should be_nil
144
+ MoneyHelper.money_to_text("", "USD").should be_nil
145
+ MoneyHelper.money_to_text(nil, "USD").should be_nil
146
+ end
147
+ end
148
+ describe "money_range_to_text" do
149
+ it "includes no indicator for currency for the upper amount in range" do
150
+ MoneyHelper.money_range_to_text(30000, 40000, "USD").should eql("$30,000 - 40,000")
151
+ MoneyHelper.money_range_to_text(30000, 40000, "AUD").should eql("AUD $30,000 - 40,000")
152
+ MoneyHelper.money_range_to_text(30000, 40000, "AMD").should eql("AMD դր.30,000 - 40,000")
153
+ MoneyHelper.money_range_to_text(30000, 40000, "AFN").should eql("AFN 30,000 - 40,000")
154
+ end
155
+ it "uses the special range amount delimeter when supplied" do
156
+ MoneyHelper.money_range_to_text(30000, 40000, "USD", "-").should eql("$30,000-40,000")
157
+ MoneyHelper.money_range_to_text(30000, 40000, "AUD", "-").should eql("AUD $30,000-40,000")
158
+ MoneyHelper.money_range_to_text(30000, 40000, "AMD", "-").should eql("AMD դր.30,000-40,000")
159
+ MoneyHelper.money_range_to_text(30000, 40000, "AFN", "-").should eql("AFN 30,000-40,000")
160
+ end
161
+ it "prefixes the text 'Under ' when low amount not given" do
162
+ MoneyHelper.money_range_to_text(nil, 40000, "USD").should eql("Under $40,000")
163
+ end
164
+ it "appends the text ' and up' when high amount not given" do
165
+ MoneyHelper.money_range_to_text(30000, nil, "USD").should eql("$30,000 and up")
166
+ end
167
+ it "treats as a single price when low amount and high amount are identical" do
168
+ MoneyHelper.money_range_to_text(30000, 30000, "USD").should eql("$30,000")
169
+ end
170
+ it "returns nil when both amounts are nil" do
171
+ MoneyHelper.money_range_to_text(nil, nil, "USD").should be_nil
172
+ end
173
+ end
174
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,18 +10,68 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-12 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2013-07-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: money
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 5.1.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 5.1.1
31
+ - !ruby/object:Gem::Dependency
32
+ name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: '3.2'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.2'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
15
63
  description: A simple class to assist in formatting unambiguous prices and price ranges
16
- in international currencies
64
+ in international currencies in a Roman Script context.
17
65
  email: sahil@artsymail.net
18
66
  executables: []
19
67
  extensions: []
20
68
  extra_rdoc_files: []
21
69
  files:
22
70
  - lib/money_helper.rb
23
- homepage: http://rubygems.org/gems/money_helper
24
- licenses: []
71
+ - spec/money_helper_spec.rb
72
+ homepage: https://github.com/syakhmi/money_helper
73
+ licenses:
74
+ - MIT
25
75
  post_install_message:
26
76
  rdoc_options: []
27
77
  require_paths:
@@ -32,16 +82,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
82
  - - ! '>='
33
83
  - !ruby/object:Gem::Version
34
84
  version: '0'
85
+ segments:
86
+ - 0
87
+ hash: -2590949666229956668
35
88
  required_rubygems_version: !ruby/object:Gem::Requirement
36
89
  none: false
37
90
  requirements:
38
91
  - - ! '>='
39
92
  - !ruby/object:Gem::Version
40
93
  version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -2590949666229956668
41
97
  requirements: []
42
98
  rubyforge_project:
43
99
  rubygems_version: 1.8.25
44
100
  signing_key:
45
101
  specification_version: 3
46
102
  summary: MoneyHelper international price formatting utility
47
- test_files: []
103
+ test_files:
104
+ - spec/money_helper_spec.rb