formatting 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/formatting/currency.rb +37 -21
- data/lib/formatting/number.rb +15 -8
- data/lib/formatting/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86d1360d37ac53df729455a549d27360133a2926
|
4
|
+
data.tar.gz: b1a7d79c9b51f887b3900b3b384e27903d0c2907
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f548175ebee4b7c92fa55706cb9cb49316aba8ab32b74e30600d35e114299bc2f32aa34aea5c716ef53c9ddc69d08871be57d47cc65477deaff69b76158b791
|
7
|
+
data.tar.gz: 1bc2c988fa321b29b252e760095e276f7f1822db22f296b04b16acd696b5a7e5cdc023cdfd76fd41e955c9db1634cfba7ccc1c0b3973d8f56561bf0564908a3d
|
data/lib/formatting/currency.rb
CHANGED
@@ -8,29 +8,21 @@ module Formatting
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class FormatCurrency
|
11
|
-
|
11
|
+
attr_private :record_or_currency, :amount_or_method, :opts,
|
12
|
+
:format_string, :skip_currency
|
12
13
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
unless skip_currency
|
18
|
-
currency = opts.fetch(:currency) {
|
19
|
-
case record_or_currency
|
20
|
-
when String, Symbol
|
21
|
-
record_or_currency
|
22
|
-
else
|
23
|
-
record_or_currency.respond_to?(:currency) ? record_or_currency.currency : nil
|
24
|
-
end
|
25
|
-
}
|
26
|
-
currency = nil if currency == false
|
27
|
-
end
|
14
|
+
def initialize(record_or_currency, amount_or_method, opts)
|
15
|
+
@record_or_currency = record_or_currency
|
16
|
+
@amount_or_method = amount_or_method
|
17
|
+
@opts = opts
|
28
18
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
19
|
+
@format_string = opts.fetch(:format, "<amount> <currency>")
|
20
|
+
@skip_currency = opts.fetch(:skip_currency, false)
|
21
|
+
end
|
22
|
+
|
23
|
+
def format
|
24
|
+
currency = determine_currency
|
25
|
+
amount = determine_amount
|
34
26
|
|
35
27
|
return "" if amount.nil?
|
36
28
|
|
@@ -40,6 +32,30 @@ module Formatting
|
|
40
32
|
|
41
33
|
private
|
42
34
|
|
35
|
+
def default_currency
|
36
|
+
case record_or_currency
|
37
|
+
when String, Symbol
|
38
|
+
record_or_currency
|
39
|
+
else
|
40
|
+
record_or_currency.respond_to?(:currency) ? record_or_currency.currency : nil
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def determine_currency
|
45
|
+
return nil if skip_currency
|
46
|
+
|
47
|
+
currency = opts.fetch(:currency) { default_currency }
|
48
|
+
currency == false ? nil : currency
|
49
|
+
end
|
50
|
+
|
51
|
+
def determine_amount
|
52
|
+
if amount_or_method.is_a?(Symbol)
|
53
|
+
record_or_currency.public_send(amount_or_method)
|
54
|
+
else
|
55
|
+
amount_or_method
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
43
59
|
def apply_format_string(format_string, amount, currency)
|
44
60
|
out = format_string.dup
|
45
61
|
out.gsub!("<amount>", amount)
|
data/lib/formatting/number.rb
CHANGED
@@ -8,16 +8,22 @@ module Formatting
|
|
8
8
|
end
|
9
9
|
|
10
10
|
class FormatNumber
|
11
|
-
|
11
|
+
attr_private :input_number,
|
12
|
+
:thousands_separator, :decimal_separator,
|
13
|
+
:round, :min_decimals, :explicit_sign, :blank_when_zero
|
14
|
+
|
15
|
+
def initialize(input_number, opts)
|
16
|
+
@input_number = input_number
|
17
|
+
|
18
|
+
@thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
|
19
|
+
@decimal_separator = opts.fetch(:decimal_separator) { default_decimal_separator }
|
20
|
+
@round = opts.fetch(:round, 2)
|
21
|
+
@min_decimals = opts.fetch(:min_decimals, 2)
|
22
|
+
@explicit_sign = opts.fetch(:explicit_sign, false)
|
23
|
+
@blank_when_zero = opts.fetch(:blank_when_zero, false)
|
24
|
+
end
|
12
25
|
|
13
26
|
def format
|
14
|
-
thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
|
15
|
-
decimal_separator = opts.fetch(:decimal_separator) { default_decimal_separator }
|
16
|
-
round = opts.fetch(:round, 2)
|
17
|
-
min_decimals = opts.fetch(:min_decimals, 2)
|
18
|
-
explicit_sign = opts.fetch(:explicit_sign, false)
|
19
|
-
blank_when_zero = opts.fetch(:blank_when_zero, false)
|
20
|
-
|
21
27
|
number = input_number
|
22
28
|
|
23
29
|
has_decimals = number.to_s.include?(".")
|
@@ -35,6 +41,7 @@ module Formatting
|
|
35
41
|
|
36
42
|
integer, decimals = number.to_s.split(".")
|
37
43
|
|
44
|
+
# Separate groups by thousands separator.
|
38
45
|
integer.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{thousands_separator}")
|
39
46
|
|
40
47
|
if explicit_sign
|
data/lib/formatting/version.rb
CHANGED