fixed_width_columns 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fixed_width_columns.rb +12 -14
- data/lib/fixed_width_columns/config.rb +17 -0
- data/lib/fixed_width_columns/fixed_width_attribute.rb +1 -1
- data/lib/fixed_width_columns/formatter.rb +15 -0
- data/lib/fixed_width_columns/library.rb +24 -0
- data/lib/fixed_width_columns/preprocessor.rb +15 -0
- data/lib/fixed_width_columns/version.rb +2 -2
- data/spec/format_spec.rb +2 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2c1cbb084ea41811459364fd5545025c30c6110
|
4
|
+
data.tar.gz: b559e69f43c144d8809377f5bdf38677e08c0bc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5945d4eb08bfbe878c6160cd24507aed601b0c794af0fbea7af3d7d861ba2065d1c2a5d4974a3898872018715ab0b1e58eff2edf78c7272e61163637aadea65d
|
7
|
+
data.tar.gz: 469964abcb7d33f9c906287605ae57f5ed769c42196de674339072f68bfc94291c33c2bf1880406e88d086b309cec8218ed55e5953dfcb8e2077ee6ae3248e41
|
data/lib/fixed_width_columns.rb
CHANGED
@@ -1,18 +1,16 @@
|
|
1
1
|
require 'aduki'
|
2
|
-
require "fixed_width_columns/version"
|
3
|
-
require "fixed_width_columns/fixed_width_attribute"
|
4
|
-
|
5
|
-
class FixedWidthColumns
|
6
|
-
include Aduki::Initializer
|
7
|
-
include Aduki::Initializer
|
8
|
-
attr_accessor :columns
|
9
|
-
aduki columns: FixedWidthAttribute
|
10
2
|
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
module FixedWidthColumns
|
4
|
+
FILTERS = { }
|
5
|
+
TRANSFORMS = { }
|
14
6
|
|
15
|
-
def
|
16
|
-
|
17
|
-
end
|
7
|
+
def self.transformer name, proc ; TRANSFORMS[name] = proc ; end
|
8
|
+
def self.filter name, proc ; FILTERS[name] = proc ; end
|
18
9
|
end
|
10
|
+
|
11
|
+
require "fixed_width_columns/version"
|
12
|
+
require "fixed_width_columns/library"
|
13
|
+
require "fixed_width_columns/fixed_width_attribute"
|
14
|
+
require "fixed_width_columns/formatter"
|
15
|
+
require "fixed_width_columns/preprocessor"
|
16
|
+
require "fixed_width_columns/config"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'aduki'
|
2
|
+
|
3
|
+
module FixedWidthColumns
|
4
|
+
class Config < Aduki::Initializable
|
5
|
+
attr_accessor :name, :target, :label
|
6
|
+
aduki formatter: FixedWidthColumns::Formatter
|
7
|
+
aduki preprocessors: FixedWidthColumns::PreProcessor
|
8
|
+
|
9
|
+
def to_text items, header=nil
|
10
|
+
pre_process(items).inject(headers? header) { |ll, i| ll << formatter.format(i) }.join "\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
@@export_configs = { }
|
14
|
+
def headers? header ; header ? [formatter.headers] : [] ; end
|
15
|
+
def pre_process items ; (preprocessors || []).inject(items) { |list, pp| pp.apply list } ; end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'aduki'
|
2
|
+
|
3
|
+
module FixedWidthColumns
|
4
|
+
class Formatter < Aduki::Initializable
|
5
|
+
aduki columns: FixedWidthColumns::FixedWidthAttribute
|
6
|
+
|
7
|
+
def headers
|
8
|
+
columns.map { |col| col.format(col.name.to_s, :padding => ' ') }.join
|
9
|
+
end
|
10
|
+
|
11
|
+
def format thing
|
12
|
+
columns.map { |col| col.string_for thing }.join
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'aduki'
|
2
|
+
|
3
|
+
module FixedWidthColumns
|
4
|
+
class Library < Aduki::Initializable
|
5
|
+
attr_accessor :config_root
|
6
|
+
aduki_initialize :export_configs, Hash
|
7
|
+
|
8
|
+
def export_config_for klass, name
|
9
|
+
export_configs_for(klass).detect { |cfg| cfg.name == name }
|
10
|
+
end
|
11
|
+
|
12
|
+
def export_configs_for klass
|
13
|
+
klass = klass.class_name if klass.is_a? Class
|
14
|
+
export_configs[klass] ||= load_export_configs_for klass
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_export_configs_for klass
|
18
|
+
path = File.join config_root, "#{klass}.yml"
|
19
|
+
return [] unless File.exists?(path)
|
20
|
+
cfgs = YAML::load File.read path
|
21
|
+
cfgs.map { |k,v| FixedWidthColumns::Config.new v.merge("target" => klass, "name" => k) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'aduki'
|
2
|
+
|
3
|
+
module FixedWidthColumns
|
4
|
+
class PreProcessor < Aduki::Initializable
|
5
|
+
attr_accessor :filter, :transform, :library
|
6
|
+
|
7
|
+
def filter_sym ; (@filter || "true").to_sym ; end
|
8
|
+
def do_filter items ; items.select &(FixedWidthColumns::FILTERS[filter_sym]) ; end
|
9
|
+
def maybe_transform items ; @transform ? do_transform(items) : items ; end
|
10
|
+
def do_transform items ; FixedWidthColumns::TRANSFORMS[@transform.to_sym].call items ; end
|
11
|
+
def apply items ; maybe_transform do_filter items ; end
|
12
|
+
def to_s ; "filter:#{@filter};transform:#{@transform}" ; end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0.
|
1
|
+
module FixedWidthColumns
|
2
|
+
VERSION = "0.0.6"
|
3
3
|
end
|
data/spec/format_spec.rb
CHANGED
@@ -35,7 +35,7 @@ describe FixedWidthColumns do
|
|
35
35
|
ft3 = FinancialTransaction.new(id: 3, account: bank , date: "to be determined", ref: "payment thanks", debit: 0.00, credit: 444.44)
|
36
36
|
ft4 = FinancialTransaction.new(id: 4, account: customer, date: dp("2006-06-21") , ref: "credit note" , debit: 0.00, credit: 222.22)
|
37
37
|
|
38
|
-
formatter = FixedWidthColumns.new(columns: format_spec)
|
38
|
+
formatter = FixedWidthColumns::Formatter.new(columns: format_spec)
|
39
39
|
|
40
40
|
expect(formatter.headers). to eq " id date ref debit credit account.name"
|
41
41
|
expect(formatter.format ft1).to eq " 1 28122003 payment thanks 0000000000012345 Bank"
|
@@ -60,7 +60,7 @@ describe FixedWidthColumns do
|
|
60
60
|
ft3 = FinancialTransaction.new(id: 3, account: bank , date: "to be determined", ref: "payment thanks", debit: 0.00, credit: 444.44)
|
61
61
|
ft4 = FinancialTransaction.new(id: 4, account: customer, date: dp("2006-06-21") , ref: "credit note" , debit: 0.00, credit: 222.22)
|
62
62
|
|
63
|
-
formatter = FixedWidthColumns.new(columns: format_spec)
|
63
|
+
formatter = FixedWidthColumns::Formatter.new(columns: format_spec)
|
64
64
|
|
65
65
|
expect(formatter.headers). to eq "thingthing.dat thing.de account.name"
|
66
66
|
expect(formatter.format({ thing: ft1, account: bank})).to eq " 1 28122003 00000000 Bank"
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Conan Dalton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aduki
|
@@ -96,7 +96,11 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- fixed_width_columns.gemspec
|
98
98
|
- lib/fixed_width_columns.rb
|
99
|
+
- lib/fixed_width_columns/config.rb
|
99
100
|
- lib/fixed_width_columns/fixed_width_attribute.rb
|
101
|
+
- lib/fixed_width_columns/formatter.rb
|
102
|
+
- lib/fixed_width_columns/library.rb
|
103
|
+
- lib/fixed_width_columns/preprocessor.rb
|
100
104
|
- lib/fixed_width_columns/version.rb
|
101
105
|
- spec/format_spec.rb
|
102
106
|
- spec/spec_helper.rb
|