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 +4 -4
- data/lib/fixed_width_columns/fixed_width_attribute.rb +10 -2
- data/lib/fixed_width_columns/version.rb +1 -1
- data/spec/format_spec.rb +17 -15
- 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: 87ed079ca02d7387346ae9fca89d027c86514921
|
4
|
+
data.tar.gz: 881c93c2bc543a0ceb2ca7883672f49717b2c567
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
data/spec/format_spec.rb
CHANGED
@@ -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:
|
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: "
|
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
|
31
|
-
expect(formatter.format ft1).to eq " 1
|
32
|
-
expect(formatter.format ft2).to eq " 2
|
33
|
-
expect(formatter.format ft3).to eq "
|
34
|
-
expect(formatter.format ft4).to eq " 4
|
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.
|
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
|
11
|
+
date: 2015-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aduki
|