fixed_width_columns 0.0.4 → 0.0.5
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/lib/fixed_width_columns/fixed_width_attribute.rb +10 -1
- data/lib/fixed_width_columns/version.rb +1 -1
- data/spec/format_spec.rb +26 -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: 756988f59622b5a76492b0e8d3acd441bc8c8aeb
|
4
|
+
data.tar.gz: b5cff2c8e2984bac68bc2900e7d01fa30a2f6d63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61ddfdb87282906e2e58072682c74bb4ed19f6866ebc8309cbdeaad42925ca83ad29baccf1a53281abff2f0bc306c45f082f30b2d31037c17c3e145211772467
|
7
|
+
data.tar.gz: 2a94e60ad37c995dacbe66dcbcdd685fc12246884d115036a6ef0187861702558aa996dd5b4d005e66b8b45f9fd8f0733a8e3290cc9d82092d3bc3286a0bdd20
|
@@ -31,12 +31,21 @@ class FixedWidthColumns
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
def get_attribute_value obj, segment
|
35
|
+
segment = segment.gsub(/-/, '_').to_sym
|
36
|
+
if obj.is_a? Hash
|
37
|
+
obj[segment]
|
38
|
+
else
|
39
|
+
obj.send segment
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
34
43
|
def lookup obj, str
|
35
44
|
segments = (str.to_s || "").split "."
|
36
45
|
segment = segments.shift
|
37
46
|
while !((segment == nil) || (segment.strip == '') ||(obj == nil))
|
38
47
|
begin
|
39
|
-
obj = obj
|
48
|
+
obj = get_attribute_value obj, segment
|
40
49
|
rescue Exception => e
|
41
50
|
raise "looking up #{str.inspect} ;\n can't lookup #{segment}\n on #{obj.inspect}, \n got #{e.message.inspect}"
|
42
51
|
end
|
data/spec/format_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe FixedWidthColumns do
|
|
17
17
|
|
18
18
|
def dp str; Date.parse str; end
|
19
19
|
|
20
|
-
it "
|
20
|
+
it "produces text formatted according to the given specification" do
|
21
21
|
format_spec = [
|
22
22
|
{ name: :id , length: 5 },
|
23
23
|
{ name: :date , length: 9, date_format: "%d%m%Y" },
|
@@ -43,4 +43,29 @@ describe FixedWidthColumns do
|
|
43
43
|
expect(formatter.format ft3).to eq " 3to be det payment thanks 0000000000044444 Bank"
|
44
44
|
expect(formatter.format ft4).to eq " 4 21062006 credit note 0000000000022222 Customer"
|
45
45
|
end
|
46
|
+
|
47
|
+
it "produces text looking up from a hash" do
|
48
|
+
format_spec = [
|
49
|
+
{ name: "thing.id" , length: 5 },
|
50
|
+
{ name: "thing.date" , length: 9, date_format: "%d%m%Y" },
|
51
|
+
{ name: :' ' , text: ' ' },
|
52
|
+
{ name: "thing.debit" , length: 8, padding: '0' },
|
53
|
+
{ name: "account.name", length: 24 },
|
54
|
+
]
|
55
|
+
|
56
|
+
customer = FinancialAccount.new id: 1, name: "Customer"
|
57
|
+
bank = FinancialAccount.new id: 1, name: "Bank"
|
58
|
+
ft1 = FinancialTransaction.new(id: 1, account: bank , date: dp("2003-12-28") , ref: "payment thanks", debit: 0.00, credit: 123.45)
|
59
|
+
ft2 = FinancialTransaction.new(id: 2, account: customer, date: dp("2004-03-12") , ref: "invoice 123" , debit: 666.66, credit: 0)
|
60
|
+
ft3 = FinancialTransaction.new(id: 3, account: bank , date: "to be determined", ref: "payment thanks", debit: 0.00, credit: 444.44)
|
61
|
+
ft4 = FinancialTransaction.new(id: 4, account: customer, date: dp("2006-06-21") , ref: "credit note" , debit: 0.00, credit: 222.22)
|
62
|
+
|
63
|
+
formatter = FixedWidthColumns.new(columns: format_spec)
|
64
|
+
|
65
|
+
expect(formatter.headers). to eq "thingthing.dat thing.de account.name"
|
66
|
+
expect(formatter.format({ thing: ft1, account: bank})).to eq " 1 28122003 00000000 Bank"
|
67
|
+
expect(formatter.format({ thing: ft2, account: bank})).to eq " 2 12032004 00066666 Bank"
|
68
|
+
expect(formatter.format({ thing: ft3, account: bank})).to eq " 3to be det 00000000 Bank"
|
69
|
+
expect(formatter.format({ thing: ft4, account: bank})).to eq " 4 21062006 00000000 Bank"
|
70
|
+
end
|
46
71
|
end
|