formatting 0.0.1 → 0.0.3
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 +4 -4
- data/README.md +1 -0
- data/lib/formatting/currency.rb +8 -0
- data/lib/formatting/version.rb +1 -1
- data/spec/currency_spec.rb +8 -0
- 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: 3955f66b2db50346419ee47e7504648cf7b521f9
|
4
|
+
data.tar.gz: fe2d09e425d414518a440950da09e68516091d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d67d72c11459baa553c190d991fc4eee953d7e3f523f2ca71a6cd3aff8e3d1c6915bf4c80c4d9b9ae7f0b30149c3fde1472394215cb6dd38813466af68809b71
|
7
|
+
data.tar.gz: 049d9d2cce7c44b6a20e8f611d07215241de9b5521090a28a0d19b25faade22a169bd019ee80be316be9365e0464cf295295df05accba251cd7435a10b6272eb
|
data/README.md
CHANGED
data/lib/formatting/currency.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
module Formatting
|
2
|
+
class NotARecordError < StandardError; end
|
3
|
+
|
2
4
|
module Currency
|
3
5
|
include Number
|
4
6
|
|
5
7
|
def format_currency(record, amount_or_method, opts = {})
|
8
|
+
if record.is_a?(Symbol)
|
9
|
+
raise NotARecordError, "Expected an object that could tell us its currency; got #{record.inspect}"
|
10
|
+
end
|
11
|
+
|
6
12
|
opts = Formatting.defaults.merge(opts)
|
7
13
|
|
8
14
|
format_string = opts.fetch(:format, "<amount> <currency>")
|
@@ -17,6 +23,8 @@ module Formatting
|
|
17
23
|
amount = amount_or_method
|
18
24
|
end
|
19
25
|
|
26
|
+
amount = 0 if amount.nil?
|
27
|
+
|
20
28
|
amount = format_number(amount, opts)
|
21
29
|
apply_format_string(format_string, amount, currency)
|
22
30
|
end
|
data/lib/formatting/version.rb
CHANGED
data/spec/currency_spec.rb
CHANGED
@@ -21,6 +21,14 @@ describe Formatting do
|
|
21
21
|
item.stub(price: 2)
|
22
22
|
expect_formatted(item, :price).to eq space_to_nbsp("2.0")
|
23
23
|
end
|
24
|
+
|
25
|
+
it "complains if the 'record' looks like a method name (a likely mistake)" do
|
26
|
+
expect { Formatting.format_currency(:item, 123) }.to raise_error(Formatting::NotARecordError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "treats nil as 0" do
|
31
|
+
expect_formatted(item, nil).to include space_to_nbsp("0.0")
|
24
32
|
end
|
25
33
|
|
26
34
|
context "formatting" do
|