fixed_width_columns 0.0.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 899e47444fbc5159b1801581dc9b538716c594a4
4
- data.tar.gz: 847aad4b1447dcb64337d9632b568b533b85ceee
3
+ metadata.gz: 87ed079ca02d7387346ae9fca89d027c86514921
4
+ data.tar.gz: 881c93c2bc543a0ceb2ca7883672f49717b2c567
5
5
  SHA512:
6
- metadata.gz: 5b8786970a9574d76e2936a08cd2a85bad0ae0a9f8dee44e5f5c9c916c540839229c56b9a28ccbd8e371a959a677f69571685542d425d92d7919e0eb75eabdb3
7
- data.tar.gz: cefeacfc9eaa9e4efa85c97b56c0736eefe20131c5ba76cfc9d1fda24770fb068a525cf3f948822273d8fd40d011a049f0fdca77f75773905bf49f786be0f1cb
6
+ metadata.gz: 1e0b5d6a0e7153b97f455145800fb59ffa45753e267723739388a36ccfe6aa7964a0dab1ccf9caad05c07218316f588118e783e2d33b1557152f3d5548332aa0
7
+ data.tar.gz: 600b7c0fd3a1866dff4b408985ef6005b8f7d18ea8a628d24fb1b346c98de1e59b06345b27ddf85d04fc99dc88d0f08a5555378be92a8ca522661c2e75c498a3
@@ -4,17 +4,25 @@ class FixedWidthColumns
4
4
  class FixedWidthAttribute
5
5
  include Aduki::Initializer
6
6
  attr_writer :align, :padding
7
- attr_accessor :name, :length, :text
7
+ attr_accessor :name, :length, :text, :date_format
8
8
 
9
9
  def align ; (@align || :right).to_sym ; end
10
10
  def padding ; @padding || ' ' ; end
11
11
 
12
+ def format_date value
13
+ return value unless value.is_a? Date
14
+ return value.to_s unless date_format
15
+ value.strftime date_format
16
+ end
17
+
12
18
  def format value, override={ }
13
19
  my_length = override[:length] || self.length || value.length
14
20
  my_align = override[:align] || self.align
15
21
  my_padding = override[:padding] || self.padding
16
22
 
17
- str = value.to_s[0...my_length]
23
+ str = format_date value
24
+
25
+ str = str.to_s[0...my_length]
18
26
  case my_align
19
27
  when :left
20
28
  str.ljust my_length, my_padding
@@ -1,3 +1,3 @@
1
1
  class FixedWidthColumns
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,27 +10,29 @@ describe FixedWidthColumns do
10
10
  def credit ; (@credit * 100).to_i; end
11
11
  end
12
12
 
13
+ def dp str; Date.parse str; end
14
+
13
15
  it "should produce text formatted according to the given specification" do
14
16
  format_spec = [
15
- { name: :id , length: 5 },
16
- { name: :date , length: 14 },
17
- { name: :' ' , text: ' ' },
18
- { name: :ref , length: 24, align: :left },
19
- { name: :debit , length: 8, padding: '0' },
20
- { name: :credit , length: 8, padding: '0' },
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' },
21
23
  ]
22
24
 
23
- ft1 = FinancialTransaction.new(id: 1, date: "2003-12-28", ref: "payment thanks", debit: 0.00, credit: 123.45)
24
- ft2 = FinancialTransaction.new(id: 2, date: "2004-03-12", ref: "invoice 123" , debit: 666.66, credit: 0)
25
- ft3 = FinancialTransaction.new(id: 3, date: "2005-06-08", ref: "payment thanks", debit: 0.00, credit: 444.44)
26
- ft4 = FinancialTransaction.new(id: 4, date: "2006-06-21", ref: "credit note" , debit: 0.00, credit: 222.22)
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)
27
29
 
28
30
  formatter = FixedWidthColumns.new(columns: format_spec)
29
31
 
30
- expect(formatter.headers). to eq " id date ref debit credit"
31
- expect(formatter.format ft1).to eq " 1 2003-12-28 payment thanks 0000000000012345"
32
- expect(formatter.format ft2).to eq " 2 2004-03-12 invoice 123 0006666600000000"
33
- expect(formatter.format ft3).to eq " 3 2005-06-08 payment thanks 0000000000044444"
34
- expect(formatter.format ft4).to eq " 4 2006-06-21 credit note 0000000000022222"
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"
35
37
  end
36
38
  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.2
4
+ version: 0.0.3
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-01-30 00:00:00.000000000 Z
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aduki