fixed_width_columns 0.0.4 → 0.0.5

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: 244ceeb51aa917293d512f505d566c33a3f257f0
4
- data.tar.gz: 1e170450c23a0e550f3ec3f73843bad46fbd2f4d
3
+ metadata.gz: 756988f59622b5a76492b0e8d3acd441bc8c8aeb
4
+ data.tar.gz: b5cff2c8e2984bac68bc2900e7d01fa30a2f6d63
5
5
  SHA512:
6
- metadata.gz: 93d9465cb92d92b850d39ca456c2bedf81f90c0f851a9faf814f174b9b7d5c0875d974d2c39f7384e72b22e0c2917d1660c1234645c97bcb854ba9f516a3ba40
7
- data.tar.gz: df7df1a412d06e4c01056ef4b9a02bac8b8a4cfc978644f6afdfdfe678caac4185d1053b77af8908832c15e802979d8efebc7034e307123cccbff172b9b3f5fb
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.send segment.gsub(/-/, '_').to_sym
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
@@ -1,3 +1,3 @@
1
1
  class FixedWidthColumns
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  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 "should produce text formatted according to the given specification" do
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixed_width_columns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conan Dalton