formatting 0.0.3 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3955f66b2db50346419ee47e7504648cf7b521f9
4
- data.tar.gz: fe2d09e425d414518a440950da09e68516091d90
3
+ metadata.gz: 0fee06b2bd9c054f9a95f99d0c32dbe00062eba7
4
+ data.tar.gz: 009dad129635e6941f3dfcfc1fde91aef601c13b
5
5
  SHA512:
6
- metadata.gz: d67d72c11459baa553c190d991fc4eee953d7e3f523f2ca71a6cd3aff8e3d1c6915bf4c80c4d9b9ae7f0b30149c3fde1472394215cb6dd38813466af68809b71
7
- data.tar.gz: 049d9d2cce7c44b6a20e8f611d07215241de9b5521090a28a0d19b25faade22a169bd019ee80be316be9365e0464cf295295df05accba251cd7435a10b6272eb
6
+ metadata.gz: 8cdb71265b3c0566c73e414ee907e2335cc4533042234462e82aadfc19a159558a36014ee5f53a39eaa274674cc82126dd0b0d5fe6f67c6e84af4c5164d5eb40
7
+ data.tar.gz: 9a446f71f65a60f67effac58f61ce6853c555a3336dec342d12522d93fafbb4d073405e1982eb42abbc7e2620c3d0ea149389f00d939c386845361bd17f5f950
data/README.md CHANGED
@@ -79,8 +79,6 @@ Or install it yourself as:
79
79
 
80
80
  ## TODO
81
81
 
82
- * Actually use i18n for separators
83
- * Don't depend on `i18n`?
84
- * Rename? This name is boring and also generic enough that collisions seem likely.
82
+ * Use real i18n in specs so they're less fragile and ugly
85
83
  * Document options
86
- * Change some defaults: round: 2, min_decimals: 2
84
+ * Rename? This name is boring and also generic enough that collisions seem likely.
data/formatting.gemspec CHANGED
@@ -17,8 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "i18n"
21
-
22
20
  spec.add_development_dependency "bundler", "~> 1.3"
23
21
  spec.add_development_dependency "rake"
24
22
  spec.add_development_dependency "rspec"
@@ -23,7 +23,7 @@ module Formatting
23
23
  amount = amount_or_method
24
24
  end
25
25
 
26
- amount = 0 if amount.nil?
26
+ return "" if amount.nil?
27
27
 
28
28
  amount = format_number(amount, opts)
29
29
  apply_format_string(format_string, amount, currency)
@@ -3,13 +3,15 @@ module Formatting
3
3
  def format_number(number, opts = {})
4
4
  opts = Formatting.defaults.merge(opts)
5
5
 
6
- thousands_separator = opts.fetch(:thousands_separator, NON_BREAKING_SPACE)
7
- decimal_separator = opts.fetch(:decimal_separator, ".")
8
- round = opts.fetch(:round, nil)
9
- min_decimals = opts.fetch(:min_decimals, nil)
6
+ thousands_separator = opts.fetch(:thousands_separator) { default_thousands_separator }
7
+ decimal_separator = opts.fetch(:decimal_separator) { default_decimal_separator }
8
+ round = opts.fetch(:round, 2)
9
+ min_decimals = opts.fetch(:min_decimals, 2)
10
10
  explicit_sign = opts.fetch(:explicit_sign, false)
11
11
  blank_when_zero = opts.fetch(:blank_when_zero, false)
12
12
 
13
+ has_decimals = number.to_s.include?(".")
14
+
13
15
  if blank_when_zero
14
16
  return "" if number.zero?
15
17
  end
@@ -18,11 +20,10 @@ module Formatting
18
20
  number = 0 if number.zero?
19
21
 
20
22
  if round
21
- number = number.round(round)
23
+ number = number.round(round) if has_decimals
22
24
  end
23
25
 
24
26
  integer, decimals = number.to_s.split(".")
25
- decimals ||= "0"
26
27
 
27
28
  integer.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{thousands_separator}")
28
29
 
@@ -31,10 +32,29 @@ module Formatting
31
32
  end
32
33
 
33
34
  if min_decimals
35
+ decimals ||= "0"
34
36
  decimals = decimals.ljust(min_decimals, "0")
35
37
  end
36
38
 
37
- [integer, decimals].join(decimal_separator)
39
+ [integer, decimals].compact.join(decimal_separator)
40
+ end
41
+
42
+ private
43
+
44
+ def default_thousands_separator
45
+ t_format(:delimiter, NON_BREAKING_SPACE)
46
+ end
47
+
48
+ def default_decimal_separator
49
+ t_format(:separator, ".")
50
+ end
51
+
52
+ def t_format(key, default)
53
+ if defined?(I18n)
54
+ I18n.t(key, scope: "number.format", default: default)
55
+ else
56
+ default
57
+ end
38
58
  end
39
59
  end
40
60
  end
@@ -1,3 +1,3 @@
1
1
  module Formatting
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -14,12 +14,12 @@ describe Formatting do
14
14
 
15
15
  context "method signature" do
16
16
  it "can take a record and a value" do
17
- expect_formatted(item, 1).to eq space_to_nbsp("1.0")
17
+ expect_formatted(item, 1).to eq space_to_nbsp("1.00")
18
18
  end
19
19
 
20
20
  it "can take a record and a method name" do
21
21
  item.stub(price: 2)
22
- expect_formatted(item, :price).to eq space_to_nbsp("2.0")
22
+ expect_formatted(item, :price).to eq space_to_nbsp("2.00")
23
23
  end
24
24
 
25
25
  it "complains if the 'record' looks like a method name (a likely mistake)" do
@@ -27,8 +27,8 @@ describe Formatting do
27
27
  end
28
28
  end
29
29
 
30
- it "treats nil as 0" do
31
- expect_formatted(item, nil).to include space_to_nbsp("0.0")
30
+ it "returns an empty string for nil" do
31
+ expect_formatted(item, nil).to eq ""
32
32
  end
33
33
 
34
34
  context "formatting" do
@@ -42,7 +42,7 @@ describe Formatting do
42
42
 
43
43
  it "applies default options" do
44
44
  Formatting.defaults = { currency: "FOO" }
45
- expect_formatted(item, 1).to eq space_to_nbsp("1.0 FOO")
45
+ expect_formatted(item, 1).to eq space_to_nbsp("1.00 FOO")
46
46
  end
47
47
  end
48
48
 
@@ -53,34 +53,34 @@ describe Formatting do
53
53
 
54
54
  it "is read from the record's #currency if present" do
55
55
  item.stub(currency: "SEK")
56
- expect_formatted(item, 1).to eq space_to_nbsp("1.0 SEK")
56
+ expect_formatted(item, 1).to eq space_to_nbsp("1.00 SEK")
57
57
  end
58
58
 
59
59
  it "is not added if the record's #currency is blank" do
60
60
  item.stub(currency: "")
61
- expect_formatted(item, 1).to eq space_to_nbsp("1.0")
61
+ expect_formatted(item, 1).to eq space_to_nbsp("1.00")
62
62
  end
63
63
 
64
64
  it "is not added if the record does not respond to #currency" do
65
- expect_formatted(item, 1).to eq space_to_nbsp("1.0")
65
+ expect_formatted(item, 1).to eq space_to_nbsp("1.00")
66
66
  end
67
67
  end
68
68
 
69
69
  context "format string option" do
70
70
  it "is used if provided" do
71
71
  expect_formatted(item, 123, format: "C: <currency> A: <amount>", currency: "XYZ").
72
- to eq space_to_nbsp("C: XYZ A: 123.0")
72
+ to eq space_to_nbsp("C: XYZ A: 123.00")
73
73
  end
74
74
 
75
75
  it "will have spaces turned into non-breaking spaces" do
76
76
  expect_actual = expect_formatted(item, 123, format: "<amount> <currency>", currency: "XYZ")
77
- expect_actual.to eq space_to_nbsp("123.0 XYZ")
78
- expect_actual.not_to eq "123.0 XYZ"
77
+ expect_actual.to eq space_to_nbsp("123.00 XYZ")
78
+ expect_actual.not_to eq "123.00 XYZ"
79
79
  end
80
80
 
81
81
  it "is stripped" do
82
82
  expect_formatted(item, 123, format: "<amount> <currency>", currency: nil).
83
- to eq space_to_nbsp("123.0")
83
+ to eq space_to_nbsp("123.00")
84
84
  end
85
85
  end
86
86
  end
data/spec/number_spec.rb CHANGED
@@ -10,7 +10,7 @@ end
10
10
 
11
11
  describe Formatting do
12
12
  describe ".format_number" do
13
- it "groups thousands" do
13
+ it "formats a number" do
14
14
  expect_formatted(1234567.89).to eq space_to_nbsp("1 234 567.89")
15
15
  end
16
16
 
@@ -19,19 +19,70 @@ describe Formatting do
19
19
  expect_formatted(12.3456789).to eq "12.346"
20
20
  end
21
21
 
22
+ context "thousands separator" do
23
+ context "with I18n" do
24
+ let(:i18n) { stub_const("I18n", double) }
25
+
26
+ it "uses I18n.t('number.format.delimiter') if present" do
27
+ allow(i18n).to receive(:t).with(:separator, instance_of(Hash))
28
+ expect(i18n).to receive(:t).
29
+ with(:delimiter, scope: "number.format", default: space_to_nbsp(" ")).
30
+ and_return(";")
31
+ expect_formatted(1234).to include "1;234"
32
+ end
33
+ end
34
+
35
+ context "without I18n" do
36
+ it "defaults to a non-breaking space" do
37
+ expect_formatted(1234).to include space_to_nbsp("1 234")
38
+ expect_formatted(1234).not_to include "1 234"
39
+ end
40
+ end
41
+
42
+ it "can be customized" do
43
+ expect_formatted(1234, thousands_separator: ":").to include "1:234"
44
+ end
45
+ end
46
+
47
+ context "decimal separator" do
48
+ context "with I18n" do
49
+ let(:i18n) { stub_const("I18n", double) }
50
+
51
+ it "uses I18n.t('number.format.separator') if present" do
52
+ allow(i18n).to receive(:t).with(:delimiter, instance_of(Hash))
53
+ expect(i18n).to receive(:t).with(:separator, scope: "number.format", default: ".").and_return(";")
54
+ expect_formatted(1.2).to eq "1;20"
55
+ end
56
+ end
57
+
58
+ context "without I18n" do
59
+ it "defaults to ." do
60
+ expect_formatted(1.2).to eq "1.20"
61
+ end
62
+ end
63
+
64
+ it "can be customized" do
65
+ expect_formatted(1.2, decimal_separator: ":").to eq "1:20"
66
+ end
67
+ end
68
+
22
69
  context "rounding" do
23
- it "doesn't round by default" do
24
- expect_formatted(12.3456789).to eq "12.3456789"
70
+ it "rounds to 2 decimals by defaults" do
71
+ expect_formatted(12.3456789).to eq "12.35"
25
72
  end
26
73
 
27
74
  it "rounds to the given number of decimals" do
28
- expect_formatted(12.3456789, round: 2).to eq "12.35"
75
+ expect_formatted(12.3456789, round: 3).to eq "12.346"
76
+ end
77
+
78
+ it "doesn't round if given false" do
79
+ expect_formatted(12.3456789, round: false).to eq "12.3456789"
29
80
  end
30
81
  end
31
82
 
32
83
  context "blanking when zero" do
33
84
  it "does not happen by default" do
34
- expect_formatted(0).to eq "0.0"
85
+ expect_formatted(0).to eq "0.00"
35
86
  end
36
87
 
37
88
  it "can be enforced" do
@@ -41,31 +92,40 @@ describe Formatting do
41
92
 
42
93
 
43
94
  context "minimum number of decimals" do
44
- it "is not enforced by default" do
45
- expect_formatted(12).to eq "12.0"
46
- expect_formatted(12.3).to eq "12.3"
95
+ it "defaults to two decimals" do
96
+ expect_formatted(12).to eq "12.00"
97
+ expect_formatted(12.3).to eq "12.30"
47
98
  end
48
99
 
49
100
  it "can be enforced" do
50
- expect_formatted(12.3, min_decimals: 2).to eq "12.30"
101
+ expect_formatted(12.3, min_decimals: 3).to eq "12.300"
51
102
  end
103
+
104
+ it "is not enforced if given false" do
105
+ expect_formatted(0, min_decimals: false).to eq "0"
106
+ expect_formatted(12, min_decimals: false).to eq "12"
107
+ expect_formatted(0.0, min_decimals: false).to eq "0.0"
108
+ expect_formatted(12.0, min_decimals: false).to eq "12.0"
109
+ expect_formatted(12.3, min_decimals: false).to eq "12.3"
110
+ end
111
+
52
112
  end
53
113
 
54
114
  context "explicit sign" do
55
115
  it "is not included by default" do
56
- expect_formatted(1).to eq "1.0"
57
- expect_formatted(0).to eq "0.0"
58
- expect_formatted(-1).to eq "-1.0"
116
+ expect_formatted(1).to eq "1.00"
117
+ expect_formatted(0).to eq "0.00"
118
+ expect_formatted(-1).to eq "-1.00"
59
119
  end
60
120
 
61
121
  it "never shows 0 as negative" do
62
- expect_formatted(-0.0).to eq "0.0"
122
+ expect_formatted(-0.0).to eq "0.00"
63
123
  end
64
124
 
65
125
  it "can be enforced" do
66
- expect_formatted(1, explicit_sign: true).to eq "+1.0"
67
- expect_formatted(0, explicit_sign: true).to eq "0.0"
68
- expect_formatted(-1, explicit_sign: true).to eq "-1.0"
126
+ expect_formatted(1, explicit_sign: true).to eq "+1.00"
127
+ expect_formatted(0, explicit_sign: true).to eq "0.00"
128
+ expect_formatted(-1, explicit_sign: true).to eq "-1.00"
69
129
  end
70
130
  end
71
131
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: formatting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-09 00:00:00.000000000 Z
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: i18n
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ! '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ! '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bundler
29
15
  requirement: !ruby/object:Gem::Requirement