fixed_width_file_validator 0.4.1 → 0.5.1
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_file_validator/file_format.rb +24 -7
- data/lib/fixed_width_file_validator/file_reader.rb +13 -4
- data/lib/fixed_width_file_validator/record_formatter.rb +44 -0
- data/lib/fixed_width_file_validator/record_parser.rb +1 -1
- data/lib/fixed_width_file_validator/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89bb3fdfe0ee80fb5dd745811c0c0e537ffe4ec4
|
4
|
+
data.tar.gz: 3a38f104702b48cad5877016771ee64c41ff1b2b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 460e4afe1463c190ba3eb0d9a728617150965f0c0f9ad884d1a61b31d35d57a4249a6e2fa22d91ea3235e682873b1599a533bdd13357e25bf545e4b0f5d456e5
|
7
|
+
data.tar.gz: 711d13b90813f99decba451d3a60b0c83420486ebab91900a84876a446913f022d7dc60270a964d821c7a6e78dc0cf95944aa0c4d7705423410b5f30fabf8fc3
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'fixed_width_file_validator/file_reader'
|
2
2
|
require 'fixed_width_file_validator/validator'
|
3
3
|
require 'fixed_width_file_validator/record_parser'
|
4
|
+
require 'fixed_width_file_validator/record_formatter'
|
4
5
|
|
5
6
|
require 'yaml'
|
6
7
|
|
@@ -20,12 +21,18 @@ module FixedWidthFileValidator
|
|
20
21
|
@fields = {}
|
21
22
|
@unique_fields = []
|
22
23
|
@parser_field_list = []
|
24
|
+
@formatter_field_list = []
|
23
25
|
@file_settings = {}
|
24
26
|
@column = 1
|
25
27
|
@config_file = config_file
|
26
28
|
|
27
|
-
|
28
|
-
@
|
29
|
+
if @config_file.is_a?(String)
|
30
|
+
File.open(@config_file) do |f|
|
31
|
+
@raw_config = symbolize(YAML.safe_load(f, [], [], true))
|
32
|
+
end
|
33
|
+
else
|
34
|
+
# config_file must be an IO object
|
35
|
+
@raw_config = symbolize(YAML.safe_load(config_file, [], [], true))
|
29
36
|
end
|
30
37
|
|
31
38
|
load_config(@record_type)
|
@@ -53,14 +60,16 @@ module FixedWidthFileValidator
|
|
53
60
|
reader.close
|
54
61
|
end
|
55
62
|
|
56
|
-
|
63
|
+
def record_formatter
|
64
|
+
@record_formatter ||= RecordFormatter.new(@formatter_field_list, file_settings[:encoding])
|
65
|
+
end
|
57
66
|
|
58
67
|
def record_parser
|
59
|
-
|
60
|
-
@parser ||= RecordParser.new(@parser_field_list, file_settings[:encoding])
|
61
|
-
# rubocop:enable Naming/MemoizedInstanceVariableName
|
68
|
+
@record_parser ||= RecordParser.new(@parser_field_list, file_settings[:encoding])
|
62
69
|
end
|
63
70
|
|
71
|
+
private
|
72
|
+
|
64
73
|
def load_config(record_type)
|
65
74
|
format_config = config_for(record_type)
|
66
75
|
format_config[:fields].each do |field_config|
|
@@ -68,6 +77,7 @@ module FixedWidthFileValidator
|
|
68
77
|
key = field_config[:field_name].to_sym
|
69
78
|
@fields[key] = field_config
|
70
79
|
@parser_field_list << parser_params(key)
|
80
|
+
@formatter_field_list << formatter_params(key)
|
71
81
|
@column = fields[key][:end_column] + 1
|
72
82
|
end
|
73
83
|
@file_settings = { skip_top_lines: format_config[:skip_top_lines] || 0,
|
@@ -83,12 +93,14 @@ module FixedWidthFileValidator
|
|
83
93
|
end_column = start_column + width - 1
|
84
94
|
validations = field_config[:validate]
|
85
95
|
validations = [validations] unless validations.is_a?(Array)
|
96
|
+
format = field_config[:format] || "%-#{width}s" # defaults to left aligned text
|
86
97
|
|
87
98
|
{
|
88
99
|
field_name: field_name,
|
89
100
|
start_column: start_column,
|
90
101
|
end_column: end_column,
|
91
|
-
validations: validations
|
102
|
+
validations: validations,
|
103
|
+
output_format: format
|
92
104
|
}
|
93
105
|
end
|
94
106
|
|
@@ -105,6 +117,11 @@ module FixedWidthFileValidator
|
|
105
117
|
{ name: field_name, position: (f[:start_column] - 1..f[:end_column] - 1) }
|
106
118
|
end
|
107
119
|
|
120
|
+
def formatter_params(field_name)
|
121
|
+
f = fields[field_name]
|
122
|
+
{ name: field_name, position: f[:start_column], width: f[:end_column] - f[:start_column] + 1, format: f[:output_format] }
|
123
|
+
end
|
124
|
+
|
108
125
|
def config_for(record_type)
|
109
126
|
format_config = @raw_config[record_type]
|
110
127
|
format_fields = format_config[:fields]
|
@@ -1,19 +1,19 @@
|
|
1
1
|
module FixedWidthFileValidator
|
2
2
|
class FileReader
|
3
|
-
def initialize(
|
3
|
+
def initialize(data_file, parser = nil, settings = {})
|
4
4
|
@skip_top_lines = settings[:skip_top_lines] || 0
|
5
5
|
@skip_bottom_lines = settings[:skip_bottom_lines] || 0
|
6
|
-
@data_file_path = file_name
|
7
6
|
@line_num = 0
|
8
7
|
@buffer = []
|
9
8
|
@parser = parser
|
10
9
|
@skip_top_done = false
|
10
|
+
@data_file = data_file
|
11
11
|
end
|
12
12
|
|
13
13
|
def next_record
|
14
14
|
line_num, content = readline_with_skip
|
15
15
|
return unless line_num
|
16
|
-
@parser ? @parser.parse(content, line_num, content) : content.
|
16
|
+
@parser ? @parser.parse(content, line_num, content) : content.chop
|
17
17
|
end
|
18
18
|
|
19
19
|
def each_record
|
@@ -47,8 +47,17 @@ module FixedWidthFileValidator
|
|
47
47
|
|
48
48
|
private
|
49
49
|
|
50
|
+
def open_data_file
|
51
|
+
@file = if @data_file.is_a?(String)
|
52
|
+
File.open(@data_file, 'r')
|
53
|
+
else
|
54
|
+
# assume data_file is an IO object
|
55
|
+
@data_file
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
50
59
|
def readline_with_skip
|
51
|
-
@file ||=
|
60
|
+
@file ||= open_data_file
|
52
61
|
skip_top
|
53
62
|
skip_bottom
|
54
63
|
readline
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module FixedWidthFileValidator
|
2
|
+
class RecordFormatter
|
3
|
+
attr_reader :field_list, :encoding
|
4
|
+
|
5
|
+
def initialize(field_list, encoding = nil)
|
6
|
+
@field_list = field_list.sort_by { |a| a[:position] }
|
7
|
+
@encoding = encoding || 'ISO-8859-1'
|
8
|
+
end
|
9
|
+
|
10
|
+
def record_width
|
11
|
+
last_field = @field_list.last
|
12
|
+
last_field[:position] + last_field[:width] - 1
|
13
|
+
end
|
14
|
+
|
15
|
+
def string_for(record)
|
16
|
+
out = ''
|
17
|
+
last_pos = 1
|
18
|
+
@field_list.each do |field|
|
19
|
+
current_pos = field[:position]
|
20
|
+
out << ' ' * (current_pos - last_pos)
|
21
|
+
out << formatted_value(record[field[:name]], field[:format], field[:width])
|
22
|
+
last_pos = current_pos + field[:width]
|
23
|
+
end
|
24
|
+
out
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# format the string using given format
|
30
|
+
# if the result is shorter than width, pad space to the left
|
31
|
+
# if the result is longer than width, truncate the last characters
|
32
|
+
def formatted_value(value, format, width)
|
33
|
+
value_str = value ? format(format, value) : ' ' * width # all space for nil value
|
34
|
+
length = value_str.length
|
35
|
+
if length > width
|
36
|
+
value_str.slice(0..width - 1)
|
37
|
+
elsif length < width
|
38
|
+
' ' * (width - length) + value_str
|
39
|
+
else
|
40
|
+
value_str
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -7,7 +7,7 @@ module FixedWidthFileValidator
|
|
7
7
|
@encoding = encoding || 'ISO-8859-1'
|
8
8
|
end
|
9
9
|
|
10
|
-
def parse(line, line_num, raw_line)
|
10
|
+
def parse(line, line_num = 0, raw_line = '_NA_')
|
11
11
|
record = { _line_num: line_num, _raw: raw_line }
|
12
12
|
encoded = line.encode(@encoding, 'binary', invalid: :replace, undef: :replace)
|
13
13
|
field_list.each do |field|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixed_width_file_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Li Lin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -63,6 +63,7 @@ files:
|
|
63
63
|
- lib/fixed_width_file_validator.rb
|
64
64
|
- lib/fixed_width_file_validator/file_format.rb
|
65
65
|
- lib/fixed_width_file_validator/file_reader.rb
|
66
|
+
- lib/fixed_width_file_validator/record_formatter.rb
|
66
67
|
- lib/fixed_width_file_validator/record_parser.rb
|
67
68
|
- lib/fixed_width_file_validator/report_formatter.rb
|
68
69
|
- lib/fixed_width_file_validator/string_helper.rb
|