money_helper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/money_helper.rb +63 -0
  2. metadata +47 -0
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+
3
+ module MoneyHelper
4
+
5
+ SYMBOL_ONLY = %w{USD GBP EUR RM TL} #don't use ISO code
6
+ OK_SYMBOLS = %w{
7
+ $ £ € ¥ 元 р. L ƒ ৳ P R$ K ₡ D ლ ₵ Q G L ₹ Rp ₪ ₩ ₭ R RM ₨ ₮ դր C$ ₦ TL K ₲ ₱ T ฿ T$ m ₴ ₫
8
+ } #ok to include in string
9
+
10
+ ##
11
+ # Formats a single amount in the given currency into a price string
12
+ #
13
+ # = Example
14
+ #
15
+ # $10,000; HKD $10,000 for (10000, "USD") and (10000, "HKD"), respectively
16
+ #
17
+ # = Arguments
18
+ #
19
+ # amount: (Float)
20
+ # currency: (String)
21
+ # number_only: (Boolean) optional flag to exclude currency indicators
22
+ def self.money_to_text(amount, currency, number_only = false)
23
+ 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({
27
+ no_cents: true,
28
+ symbol_position: :before,
29
+ symbol: (!number_only && OK_SYMBOLS.include?(currency_obj.symbol))
30
+ })
31
+ end
32
+
33
+ ##
34
+ # Formats a low and high amount in the given currency into a price string
35
+ #
36
+ # = Example
37
+ #
38
+ # $10,000 - 20,000 for (10000, 20000, "USD")
39
+ # HKD $10,000 - 20,000 for (10000, 20000, "HKD")
40
+ # $10,000 ... 20,000 for (10000, 20000, "USD", " ... ")
41
+ # HKD $10,000 ... 20,000 for (10000, 20000, "HKD", " ... ")
42
+ #
43
+ # = Arguments
44
+ #
45
+ # low: (Float)
46
+ # high: (Float)
47
+ # currency: (String)
48
+ # delimiter: (String) optional
49
+ def self.money_range_to_text(low, high, currency, delimiter = ' - ')
50
+ if low.nil? && high.nil?
51
+ nil
52
+ elsif low.nil?
53
+ "Under " + money_to_text(high, currency)
54
+ elsif high.nil?
55
+ money_to_text(low, currency) + " and up"
56
+ elsif low == high
57
+ money_to_text(low, currency)
58
+ else
59
+ [ money_to_text(low, currency), money_to_text(high, currency, true) ].compact.join(delimiter)
60
+ end
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sahil Yakhmi
9
+ - Joey Aghion
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-07-12 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: A simple class to assist in formatting unambiguous prices and price ranges
16
+ in international currencies
17
+ email: sahil@artsymail.net
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/money_helper.rb
23
+ homepage: http://rubygems.org/gems/money_helper
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.25
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: MoneyHelper international price formatting utility
47
+ test_files: []