fixed_width_columns 0.0.3 → 0.0.4

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: 87ed079ca02d7387346ae9fca89d027c86514921
4
- data.tar.gz: 881c93c2bc543a0ceb2ca7883672f49717b2c567
3
+ metadata.gz: 244ceeb51aa917293d512f505d566c33a3f257f0
4
+ data.tar.gz: 1e170450c23a0e550f3ec3f73843bad46fbd2f4d
5
5
  SHA512:
6
- metadata.gz: 1e0b5d6a0e7153b97f455145800fb59ffa45753e267723739388a36ccfe6aa7964a0dab1ccf9caad05c07218316f588118e783e2d33b1557152f3d5548332aa0
7
- data.tar.gz: 600b7c0fd3a1866dff4b408985ef6005b8f7d18ea8a628d24fb1b346c98de1e59b06345b27ddf85d04fc99dc88d0f08a5555378be92a8ca522661c2e75c498a3
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.send(name))
49
+ format(self.text || lookup(thing, name))
36
50
  end
37
51
  end
38
52
  end
@@ -1,3 +1,3 @@
1
1
  class FixedWidthColumns
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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 , length: 5 },
18
- { name: :date , length: 9, date_format: "%d%m%Y" },
19
- { name: :' ' , text: ' ' },
20
- { name: :ref , length: 24, align: :left },
21
- { name: :debit , length: 8, padding: '0' },
22
- { name: :credit , length: 8, padding: '0' },
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
- ft1 = FinancialTransaction.new(id: 1, date: dp("2003-12-28") , ref: "payment thanks", debit: 0.00, credit: 123.45)
26
- ft2 = FinancialTransaction.new(id: 2, date: dp("2004-03-12") , ref: "invoice 123" , debit: 666.66, credit: 0)
27
- ft3 = FinancialTransaction.new(id: 3, date: "to be determined", ref: "payment thanks", debit: 0.00, credit: 444.44)
28
- ft4 = FinancialTransaction.new(id: 4, date: dp("2006-06-21") , ref: "credit note" , debit: 0.00, credit: 222.22)
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.3
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-02-01 00:00:00.000000000 Z
11
+ date: 2015-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aduki