fixed_width_columns 0.0.3 → 0.0.4
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 +15 -1
- data/lib/fixed_width_columns/version.rb +1 -1
- data/spec/format_spec.rb +24 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 244ceeb51aa917293d512f505d566c33a3f257f0
|
4
|
+
data.tar.gz: 1e170450c23a0e550f3ec3f73843bad46fbd2f4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 93d9465cb92d92b850d39ca456c2bedf81f90c0f851a9faf814f174b9b7d5c0875d974d2c39f7384e72b22e0c2917d1660c1234645c97bcb854ba9f516a3ba40
|
7
|
+
data.tar.gz: df7df1a412d06e4c01056ef4b9a02bac8b8a4cfc978644f6afdfdfe678caac4185d1053b77af8908832c15e802979d8efebc7034e307123cccbff172b9b3f5fb
|
@@ -31,8 +31,22 @@ class FixedWidthColumns
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
def lookup obj, str
|
35
|
+
segments = (str.to_s || "").split "."
|
36
|
+
segment = segments.shift
|
37
|
+
while !((segment == nil) || (segment.strip == '') ||(obj == nil))
|
38
|
+
begin
|
39
|
+
obj = obj.send segment.gsub(/-/, '_').to_sym
|
40
|
+
rescue Exception => e
|
41
|
+
raise "looking up #{str.inspect} ;\n can't lookup #{segment}\n on #{obj.inspect}, \n got #{e.message.inspect}"
|
42
|
+
end
|
43
|
+
segment = segments.shift
|
44
|
+
end
|
45
|
+
obj
|
46
|
+
end
|
47
|
+
|
34
48
|
def string_for thing
|
35
|
-
format(self.text || thing
|
49
|
+
format(self.text || lookup(thing, name))
|
36
50
|
end
|
37
51
|
end
|
38
52
|
end
|
data/spec/format_spec.rb
CHANGED
@@ -2,9 +2,14 @@ require 'spec_helper'
|
|
2
2
|
require 'fixed_width_columns'
|
3
3
|
|
4
4
|
describe FixedWidthColumns do
|
5
|
+
class FinancialAccount
|
6
|
+
include Aduki::Initializer
|
7
|
+
attr_accessor :id, :name
|
8
|
+
end
|
9
|
+
|
5
10
|
class FinancialTransaction
|
6
11
|
include Aduki::Initializer
|
7
|
-
attr_accessor :id, :date, :ref
|
12
|
+
attr_accessor :id, :date, :ref, :account
|
8
13
|
attr_writer :debit, :credit
|
9
14
|
def debit ; (@debit * 100).to_i; end
|
10
15
|
def credit ; (@credit * 100).to_i; end
|
@@ -14,25 +19,28 @@ describe FixedWidthColumns do
|
|
14
19
|
|
15
20
|
it "should produce text formatted according to the given specification" do
|
16
21
|
format_spec = [
|
17
|
-
{ name: :id
|
18
|
-
{ name: :date
|
19
|
-
{ name: :' '
|
20
|
-
{ name: :ref
|
21
|
-
{ name: :debit
|
22
|
-
{ name: :credit
|
22
|
+
{ name: :id , length: 5 },
|
23
|
+
{ name: :date , length: 9, date_format: "%d%m%Y" },
|
24
|
+
{ name: :' ' , text: ' ' },
|
25
|
+
{ name: :ref , length: 24, align: :left },
|
26
|
+
{ name: :debit , length: 8, padding: '0' },
|
27
|
+
{ name: :credit , length: 8, padding: '0' },
|
28
|
+
{ name: "account.name", length: 24 },
|
23
29
|
]
|
24
30
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
31
|
+
customer = FinancialAccount.new id: 1, name: "Customer"
|
32
|
+
bank = FinancialAccount.new id: 1, name: "Bank"
|
33
|
+
ft1 = FinancialTransaction.new(id: 1, account: bank , date: dp("2003-12-28") , ref: "payment thanks", debit: 0.00, credit: 123.45)
|
34
|
+
ft2 = FinancialTransaction.new(id: 2, account: customer, date: dp("2004-03-12") , ref: "invoice 123" , debit: 666.66, credit: 0)
|
35
|
+
ft3 = FinancialTransaction.new(id: 3, account: bank , date: "to be determined", ref: "payment thanks", debit: 0.00, credit: 444.44)
|
36
|
+
ft4 = FinancialTransaction.new(id: 4, account: customer, date: dp("2006-06-21") , ref: "credit note" , debit: 0.00, credit: 222.22)
|
29
37
|
|
30
38
|
formatter = FixedWidthColumns.new(columns: format_spec)
|
31
39
|
|
32
|
-
expect(formatter.headers). to eq " id date ref debit credit"
|
33
|
-
expect(formatter.format ft1).to eq " 1 28122003 payment thanks 0000000000012345"
|
34
|
-
expect(formatter.format ft2).to eq " 2 12032004 invoice 123 0006666600000000"
|
35
|
-
expect(formatter.format ft3).to eq " 3to be det payment thanks 0000000000044444"
|
36
|
-
expect(formatter.format ft4).to eq " 4 21062006 credit note 0000000000022222"
|
40
|
+
expect(formatter.headers). to eq " id date ref debit credit account.name"
|
41
|
+
expect(formatter.format ft1).to eq " 1 28122003 payment thanks 0000000000012345 Bank"
|
42
|
+
expect(formatter.format ft2).to eq " 2 12032004 invoice 123 0006666600000000 Customer"
|
43
|
+
expect(formatter.format ft3).to eq " 3to be det payment thanks 0000000000044444 Bank"
|
44
|
+
expect(formatter.format ft4).to eq " 4 21062006 credit note 0000000000022222 Customer"
|
37
45
|
end
|
38
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixed_width_columns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aduki
|