formatting 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa1abfad65855a703931fa20362f151000298872
4
- data.tar.gz: 344114e77b5029eb1a39664743923c2d24f9860b
3
+ metadata.gz: 86d1360d37ac53df729455a549d27360133a2926
4
+ data.tar.gz: b1a7d79c9b51f887b3900b3b384e27903d0c2907
5
5
  SHA512:
6
- metadata.gz: d45a173c7ead2b95c59d8cafdbb154e3e54d615f8d6148a67ac7fb19fd40c9fe02bd38948f454b37d74375482662d9050e0b8a0f574e2a7f4bef5ec7db68ea29
7
- data.tar.gz: 3aa14ccd4b6fd33fd18fd8da8638409de22673dfb3ef2dbe5d6a783c7c7f18f641acc3049eeb05a5befe33f46704ea7979bce664532f954448ba47ed5ca96828
6
+ metadata.gz: 7f548175ebee4b7c92fa55706cb9cb49316aba8ab32b74e30600d35e114299bc2f32aa34aea5c716ef53c9ddc69d08871be57d47cc65477deaff69b76158b791
7
+ data.tar.gz: 1bc2c988fa321b29b252e760095e276f7f1822db22f296b04b16acd696b5a7e5cdc023cdfd76fd41e955c9db1634cfba7ccc1c0b3973d8f56561bf0564908a3d
@@ -8,29 +8,21 @@ module Formatting
8
8
  end
9
9
 
10
10
  class FormatCurrency
11
- pattr_initialize :record_or_currency, :amount_or_method, :opts
11
+ attr_private :record_or_currency, :amount_or_method, :opts,
12
+ :format_string, :skip_currency
12
13
 
13
- def format
14
- format_string = opts.fetch(:format, "<amount> <currency>")
15
- skip_currency = opts.fetch(:skip_currency, false)
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
- if amount_or_method.is_a?(Symbol)
30
- amount = record_or_currency.public_send(amount_or_method)
31
- else
32
- amount = amount_or_method
33
- end
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)
@@ -8,16 +8,22 @@ module Formatting
8
8
  end
9
9
 
10
10
  class FormatNumber
11
- pattr_initialize :input_number, :opts
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
@@ -1,3 +1,3 @@
1
1
  module Formatting
2
- VERSION = "0.0.10"
2
+ VERSION = "0.0.11"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh