hflr 1.2.7 → 1.4.0
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 +14 -6
- data/History.txt +19 -6
- data/README.txt +2 -1
- data/hflr.gemspec +5 -5
- data/lib/hflr/record_template.rb +2 -10
- data/test/flrfile_test.rb +206 -204
- data/test/sample_latin1.dat +7 -0
- metadata +18 -15
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YTdiYzdiZTFiMGEyMzg2YTE5YWZkM2FjN2U5ZjU5NDVlNmM5M2Y5ZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTlhZGNiZWFmNDI4NTA4OTU3NzU5NGFiYThlN2FhMmZiY2JmODAyMA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDZlMjZmZjZlOGViYjYwMGM1NDRlMzhkY2QwMDcyYTY3YmVhMmQ5ZGIxZjhj
|
10
|
+
Y2IwOWNkYzFlNDBlMTE5ZTQ3OTFiYTJlNmQyN2I5Mjk4N2U3NDRhMTljYTgw
|
11
|
+
NDEwN2Y1OTBjODQ5YzBjYTk3MjlkYjhlNWE0OTJiZmY5ZjJjNjc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
Mjg2MmFhMzFlMzI2NGY5OGI2MTBlNWRhZDAyNDAxODRjZDAxYWQzMGU3Mjhj
|
14
|
+
NzFlZDViYzZhYmMyYmZkZDE3OGNjMDc4MWI2MzU3MzFiNDExODk1MGFhOWFj
|
15
|
+
YTA1N2QxZGRiMzkyZmI4MGJlZTIzMjgzOWM4MGVmZDJlMTRlZTY=
|
data/History.txt
CHANGED
@@ -1,14 +1,27 @@
|
|
1
1
|
== 1.0.1 / 2010-01-21
|
2
2
|
|
3
|
-
* Fixed warnings
|
4
|
-
* Added better exception handling for badly formatted output data
|
5
|
-
|
3
|
+
* Fixed warnings
|
4
|
+
* Added better exception handling for badly formatted output data
|
6
5
|
|
7
6
|
== 0.11.0 / 2009-08-04
|
8
7
|
|
9
|
-
*
|
10
|
-
|
8
|
+
* Removed useless files
|
9
|
+
* Corrected version number
|
11
10
|
|
12
11
|
== 1.0.1 / 2011-02-09
|
12
|
+
|
13
13
|
* Added ability to specify file format in Ruby with Range class (see example.rb in /test.)
|
14
|
-
|
14
|
+
|
15
|
+
== 1.2.6
|
16
|
+
|
17
|
+
* NHGIS had been using this version for a long time until the Ruby 1.9 conversion
|
18
|
+
and the related work to get character-encoding to function properly.
|
19
|
+
|
20
|
+
== 1.4.0
|
21
|
+
|
22
|
+
* support for Ruby 1.9 character encoding
|
23
|
+
* Note: I saw that Colin had a series of 1.3 "releases" on his computer, but apparently
|
24
|
+
not released anywhere; so I jumped ahead in the version numbering to 1.4
|
25
|
+
* Deal with Latin-1 character encoding when using String#pack, force to Latin-1 since
|
26
|
+
that's the only encoding that makes sense for fixed width files (one byte per char).
|
27
|
+
|
data/README.txt
CHANGED
@@ -7,7 +7,8 @@
|
|
7
7
|
HFLR -- Hierarchical Fixed Length Records
|
8
8
|
|
9
9
|
Allows you to read and write files of fixed width records when the file contains one or more
|
10
|
-
than one type of record.
|
10
|
+
than one type of record. Version 1.4 has better support for Ruby 1.9 character encoding, by
|
11
|
+
assuming Latin-1 (ISO-8859-1).
|
11
12
|
|
12
13
|
Install with 'gem install hflr'
|
13
14
|
|
data/hflr.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{hflr}
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.4.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["Colin Davis"]
|
9
|
-
s.date = %q{
|
8
|
+
s.authors = ["Colin Davis", "Wade Stebbings"]
|
9
|
+
s.date = %q{2013-03-08}
|
10
10
|
s.description = %q{HFLR -- Hierarchical Fixed Length Records
|
11
11
|
|
12
12
|
Allows you to read and write files of fixed width records when the file contains one or more
|
@@ -20,12 +20,12 @@ See the tests and examples bundled with this gem.}
|
|
20
20
|
s.files = ["History.txt", "README.txt", "hflr.gemspec",
|
21
21
|
"lib/hflr.rb", "lib/hflr/fl_record_file.rb", "lib/hflr/hflr.rb", "lib/hflr/record_template.rb",
|
22
22
|
"test/customer_orders.dat", "test/customers.dat", "test/examples.rb", "test/flrfile_test.rb","test/record_template_test.rb",
|
23
|
-
"test/sample.dat", "test/sample2_out.dat", "test/sample_activities.dat", "test/sample_out.dat", "test/test_helper.rb", "test/test_hflr.rb"]
|
23
|
+
"test/sample.dat", "test/sample_latin1.dat", "test/sample2_out.dat", "test/sample_activities.dat", "test/sample_out.dat", "test/test_helper.rb", "test/test_hflr.rb"]
|
24
24
|
#s.homepage = %q{http://rubygems.org}
|
25
25
|
#s.rdoc_options = ["--main", "README.txt"]
|
26
26
|
s.require_paths = ["lib"]
|
27
27
|
|
28
|
-
s.rubygems_version = %q{1.
|
28
|
+
s.rubygems_version = %q{1.4.0}
|
29
29
|
s.summary = %q{HFLR -- Hierarchical Fixed Length Records Allows you to read and write files of fixed width records when the file contains one or more than one type of record}
|
30
30
|
s.test_files = ["test/test_hflr.rb", "test/test_helper.rb"]
|
31
31
|
end
|
data/lib/hflr/record_template.rb
CHANGED
@@ -44,10 +44,7 @@ private
|
|
44
44
|
def self.add_extra_columns(names, extra)
|
45
45
|
new_names = names.dup
|
46
46
|
# names are not case sensitive
|
47
|
-
|
48
|
-
extra.each{|n|new_names << n unless names.map{|m| m.to_s.upcase}.include? n.to_s.upcase}
|
49
|
-
|
50
|
-
|
47
|
+
extra.each{|n|new_names << n unless names.map{|m| m.to_s.upcase}.include? n.to_s.upcase}
|
51
48
|
|
52
49
|
# No matter what, include 'record_type'
|
53
50
|
unless new_names.map{|n| n.to_s.upcase}.include?("RECORD_TYPE")
|
@@ -75,7 +72,7 @@ public
|
|
75
72
|
end
|
76
73
|
|
77
74
|
def build_line(record)
|
78
|
-
line = format_fields(record).pack(@field_pattern)
|
75
|
+
line = format_fields(record).pack(@field_pattern).force_encoding("ISO-8859-1")
|
79
76
|
line[0] = @record_type_label unless @record_type_label == :none
|
80
77
|
line.tr!("\0",UnfilledChar)
|
81
78
|
return line
|
@@ -109,7 +106,6 @@ def self.check_record_layouts(layouts)
|
|
109
106
|
end
|
110
107
|
|
111
108
|
def self.create_template_class(record_type, record_type_label, layout, first_column_location, extra_columns = nil)
|
112
|
-
begin
|
113
109
|
names = layout.map {|l| l.name.to_sym}
|
114
110
|
names = add_extra_columns(names, extra_columns)
|
115
111
|
structure = Struct.new(*names)
|
@@ -118,10 +114,6 @@ begin
|
|
118
114
|
structure,
|
119
115
|
self.get_pattern(layout, first_column_location),
|
120
116
|
layout.map{|v| v.len})
|
121
|
-
rescue Exception=>msg
|
122
|
-
raise "Problem creating record of type #{record_type} labeled #{record_type_label} with exception #{msg.to_s} #{msg.backtrace}"
|
123
|
-
end
|
124
|
-
|
125
117
|
end
|
126
118
|
|
127
119
|
|
data/test/flrfile_test.rb
CHANGED
@@ -1,265 +1,274 @@
|
|
1
|
+
# encoding: iso-8859-1
|
2
|
+
require File.dirname(__FILE__) + "/../lib/hflr"
|
3
|
+
require 'test/unit'
|
1
4
|
|
2
5
|
|
3
|
-
|
4
|
-
class FLRFileTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
|
6
|
+
class FLRFileTest < Test::Unit::TestCase
|
7
7
|
|
8
8
|
def setup
|
9
9
|
var_type = Struct.new(:name,:start,:len)
|
10
|
-
|
10
|
+
|
11
11
|
# Split up your metadata by record type.
|
12
|
-
|
13
|
-
@layouts =
|
12
|
+
@layouts =
|
14
13
|
{:household=>[var_type.new(:rectypeh,1,1), var_type.new(:phone,2,1), var_type.new("mortgage",3,1)],
|
15
14
|
:person=>[var_type.new(:rectypep,1,1), var_type.new("age",2,3), var_type.new("sex",5,1), var_type.new("marst",6,1)]}
|
16
|
-
|
17
|
-
# Give the values used in the data for each record type
|
18
|
-
@record_types ={"H"=>:household,"P"=>:person}
|
15
|
+
|
16
|
+
# Give the values used in the data for each record type
|
17
|
+
@record_types ={"H"=>:household,"P"=>:person}
|
18
|
+
|
19
|
+
# @latin_layout = [var_type.new(:prefix,1,6), var_type.new(:lang,7,2),
|
20
|
+
# var_type.new(:mountain,9,10), var_type.new(:trailer,19,4)]
|
19
21
|
end
|
20
|
-
|
22
|
+
|
21
23
|
def teardown
|
22
24
|
end
|
23
|
-
|
24
|
-
|
25
|
+
|
26
|
+
|
25
27
|
def test_initialize
|
26
|
-
sample_data_path = File.dirname(__FILE__)
|
28
|
+
sample_data_path = File.dirname(__FILE__)
|
27
29
|
fwf = FLRFile.new(
|
28
30
|
File.new("#{sample_data_path}/sample.dat"),
|
29
|
-
@record_types, # Record types to read from the file, all others will be ignored
|
31
|
+
@record_types, # Record types to read from the file, all others will be ignored
|
30
32
|
@layouts,# metadata for all record types
|
31
|
-
1, # column 0 starts at logical location 1
|
33
|
+
1, # column 0 starts at logical location 1
|
32
34
|
{:household=>[:people],:person=>[:household_id,:pserial]} # extra columns by record type
|
33
35
|
)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
fwf = FLRFile.new(
|
36
|
+
|
37
|
+
# Extra columns + record_type accessors should have been created
|
38
|
+
hh_struct = fwf.record_template[:household].record_structure.new
|
39
|
+
assert hh_struct.respond_to?(:record_type),"household record should have record_type method"
|
40
|
+
p_struct = fwf.record_template[:person].record_structure.new
|
41
|
+
assert p_struct.respond_to?(:household_id),"Should have household_id as an extra column"
|
42
|
+
assert p_struct.respond_to?(:record_type),"Should have record_type method"
|
43
|
+
|
44
|
+
fwf = FLRFile.new(
|
44
45
|
File.new("#{sample_data_path}/sample.dat"),
|
45
|
-
@record_types, # Record types to read from the file, all others will be ignored
|
46
|
+
@record_types, # Record types to read from the file, all others will be ignored
|
46
47
|
@layouts,# metadata for all record types
|
47
48
|
1 )
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
|
50
|
+
# Should still have added the record_type method but none of the others
|
51
|
+
hh_struct = fwf.record_template[:household].record_structure.new
|
52
|
+
assert hh_struct.respond_to?(:record_type),"Should have record_type method"
|
53
|
+
assert !hh_struct.respond_to?(:people)
|
53
54
|
end
|
54
|
-
|
55
|
+
|
55
56
|
def test_get_record_type
|
56
|
-
sample_data_path = File.dirname(__FILE__)
|
57
|
+
sample_data_path = File.dirname(__FILE__)
|
57
58
|
fwf = FLRFile.new(
|
58
59
|
File.new("#{sample_data_path}/sample.dat"),
|
59
|
-
@record_types, # Record types to read from the file, all others will be ignored
|
60
|
+
@record_types, # Record types to read from the file, all others will be ignored
|
60
61
|
@layouts,# metadata for all record types
|
61
|
-
1, # column 0 starts at logical location 1
|
62
|
+
1, # column 0 starts at logical location 1
|
62
63
|
{:household=>[:people],:person=>[:household_id,:pserial]} # extra columns by record type
|
63
64
|
)
|
64
65
|
assert_nil fwf.get_record_type(nil)
|
65
66
|
assert_equal :household,fwf.get_record_type("H123")
|
66
67
|
assert_equal :person,fwf.get_record_type("P1234")
|
67
|
-
assert_equal nil, fwf.get_record_type("C123")
|
68
|
-
|
68
|
+
assert_equal nil, fwf.get_record_type("C123")
|
69
69
|
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
70
|
+
|
74
71
|
def test_build_record
|
75
|
-
sample_data_path = File.dirname(__FILE__)
|
72
|
+
sample_data_path = File.dirname(__FILE__)
|
76
73
|
fwf = FLRFile.new(
|
77
74
|
File.new("#{sample_data_path}/sample.dat"),
|
78
|
-
@record_types, # Record types to read from the file, all others will be ignored
|
75
|
+
@record_types, # Record types to read from the file, all others will be ignored
|
79
76
|
@layouts,# metadata for all record types
|
80
|
-
1, # column 0 starts at logical location 1
|
77
|
+
1, # column 0 starts at logical location 1
|
81
78
|
{:household=>[:people],:person=>[:household_id,:pserial]} # extra columns by record type
|
82
79
|
)
|
83
|
-
|
80
|
+
|
84
81
|
assert_equal nil, fwf.build_record(nil)
|
85
82
|
rec = fwf.build_record("H012345666665555444333")
|
86
83
|
assert_equal :household,rec[:record_type]
|
87
|
-
|
88
|
-
assert_raise RuntimeError do
|
84
|
+
|
85
|
+
assert_raise RuntimeError do
|
89
86
|
fwf.build_record("c23abbbc")
|
90
87
|
end
|
91
|
-
|
92
88
|
end
|
93
89
|
|
94
|
-
def test_fast_next_record
|
95
|
-
sample_data_path = File.dirname(__FILE__)
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
}
|
103
|
-
|
104
|
-
customer_file = FLRFile.new(File.new(sample_data_path + "/customers.dat"), :customer, layout, 1)
|
105
|
-
customer_file.set_fast
|
106
|
-
customer_file.ranges=[(0..2)]
|
107
|
-
|
108
|
-
records = []
|
109
|
-
while !customer_file.finished?
|
110
|
-
record = customer_file.next_record
|
111
|
-
|
112
|
-
puts record.inspect
|
113
|
-
records << record
|
114
|
-
end
|
115
|
-
|
116
|
-
assert_equal 3, records.size
|
90
|
+
def test_fast_next_record
|
91
|
+
sample_data_path = File.dirname(__FILE__)
|
92
|
+
layout = {:customer=>{
|
93
|
+
:name=>1..25,
|
94
|
+
:zip=>26..30,
|
95
|
+
:balance=>31..35
|
96
|
+
}
|
97
|
+
}
|
117
98
|
|
118
|
-
|
119
|
-
|
120
|
-
|
99
|
+
customer_file = FLRFile.new(File.new(sample_data_path + "/customers.dat"), :customer, layout, 1)
|
100
|
+
customer_file.set_fast
|
101
|
+
customer_file.ranges=[(0..2)]
|
121
102
|
|
122
|
-
|
103
|
+
records = []
|
104
|
+
while !customer_file.finished?
|
105
|
+
record = customer_file.next_record
|
106
|
+
puts record.inspect
|
107
|
+
records << record
|
108
|
+
end
|
109
|
+
|
110
|
+
assert_equal 3, records.size
|
123
111
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
layout = {:customer=>{
|
128
|
-
:name=>1..25,
|
129
|
-
:zip=>26..30,
|
130
|
-
:balance=>31..35
|
131
|
-
}
|
132
|
-
}
|
133
|
-
|
134
|
-
customer_file = FLRFile.new(File.new(sample_data_path + "/customers.dat"), :customer, layout, 1)
|
135
|
-
customer_file.set_fast
|
136
|
-
customer_file.ranges=[(0..1)]
|
137
|
-
|
138
|
-
records = []
|
139
|
-
while !customer_file.finished?
|
140
|
-
record = customer_file.next_record
|
141
|
-
puts record.inspect
|
142
|
-
records << record
|
112
|
+
# Check that the records aren't off by one
|
113
|
+
assert_equal "Jane Smith",records[1].name
|
114
|
+
assert_equal "John Smith",records.last.name
|
143
115
|
end
|
144
|
-
|
145
|
-
assert_equal 2, records.size
|
146
116
|
|
147
|
-
|
148
|
-
|
117
|
+
def test_partial_fast_next_line
|
118
|
+
sample_data_path = File.dirname(__FILE__)
|
149
119
|
|
150
|
-
|
120
|
+
layout = {:customer=>{
|
121
|
+
:name=>1..25,
|
122
|
+
:zip=>26..30,
|
123
|
+
:balance=>31..35
|
124
|
+
}
|
125
|
+
}
|
151
126
|
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
records = []
|
167
|
-
customer_file.each do |record|
|
168
|
-
|
169
|
-
puts record.inspect
|
170
|
-
records << record
|
127
|
+
customer_file = FLRFile.new(File.new(sample_data_path + "/customers.dat"), :customer, layout, 1)
|
128
|
+
customer_file.set_fast
|
129
|
+
customer_file.ranges=[(0..1)]
|
130
|
+
|
131
|
+
records = []
|
132
|
+
while !customer_file.finished?
|
133
|
+
record = customer_file.next_record
|
134
|
+
puts record.inspect
|
135
|
+
records << record
|
136
|
+
end
|
137
|
+
|
138
|
+
assert_equal 2, records.size
|
139
|
+
# Check that the records aren't off by one
|
140
|
+
assert_equal "Jane Smith",records[1].name
|
171
141
|
end
|
172
|
-
|
173
|
-
assert_equal 2, records.size
|
174
142
|
|
175
|
-
|
176
|
-
|
143
|
+
def test_fast_each
|
144
|
+
sample_data_path = File.dirname(__FILE__)
|
177
145
|
|
178
|
-
|
146
|
+
layout = {:customer=>{
|
147
|
+
:name=>1..25,
|
148
|
+
:zip=>26..30,
|
149
|
+
:balance=>31..35
|
150
|
+
}
|
151
|
+
}
|
179
152
|
|
153
|
+
customer_file = FLRFile.new(File.new(sample_data_path + "/customers.dat"), :customer, layout, 1)
|
154
|
+
customer_file.set_fast
|
155
|
+
customer_file.ranges=[(0..1)]
|
156
|
+
|
157
|
+
records = []
|
158
|
+
customer_file.each do |record|
|
159
|
+
puts record.inspect
|
160
|
+
records << record
|
161
|
+
end
|
162
|
+
|
163
|
+
assert_equal 2, records.size
|
164
|
+
|
165
|
+
# Check that the records aren't off by one
|
166
|
+
assert_equal "Jane Smith",records[1].name
|
167
|
+
end
|
180
168
|
|
181
|
-
|
182
169
|
def test_each
|
183
|
-
sample_data_path = File.dirname(__FILE__)
|
184
|
-
|
170
|
+
sample_data_path = File.dirname(__FILE__)
|
171
|
+
fwf = FLRFile.new(
|
185
172
|
File.new("#{sample_data_path}/sample.dat"),
|
186
|
-
@record_types,
|
173
|
+
@record_types,
|
187
174
|
@layouts,
|
188
175
|
1,
|
189
176
|
{:household=>[:record_type,:people],:person=>[:household_id,:pserial,:record_type]})
|
190
|
-
|
177
|
+
|
191
178
|
records = []
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
179
|
+
|
180
|
+
fwf.each do |record|
|
181
|
+
records << record
|
182
|
+
end
|
183
|
+
assert records.first.respond_to?(:record_type)
|
184
|
+
assert_equal :person, records.last.record_type
|
185
|
+
assert_equal :household,records[0].record_type
|
186
|
+
assert_equal :person, records[1].record_type
|
201
187
|
end
|
202
|
-
|
188
|
+
|
203
189
|
def test_next_record
|
204
|
-
sample_data_path = File.dirname(__FILE__)
|
190
|
+
sample_data_path = File.dirname(__FILE__)
|
205
191
|
fwf = FLRFile.new(
|
206
192
|
File.new("#{sample_data_path}/sample.dat"), # data is in this file
|
207
|
-
@record_types, # Records of different types have these labels
|
193
|
+
@record_types, # Records of different types have these labels
|
208
194
|
@layouts, # metadata for creating record structs
|
209
195
|
1, # All metadata starting column locations are to be shifted 1 left
|
210
196
|
{:household=>[:people],:person=>[:household_id,:pserial]}) # Extra columns not to come from metadata
|
211
|
-
|
212
|
-
|
213
|
-
|
197
|
+
|
198
|
+
records = []
|
199
|
+
while rec = fwf.next_record do
|
214
200
|
records << rec
|
201
|
+
end
|
215
202
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
assert_equal :household,records[0].record_type
|
230
|
-
assert_equal :person, records[1].record_type
|
231
|
-
|
203
|
+
assert_equal :household, records.first.record_type
|
204
|
+
|
205
|
+
assert records.first.respond_to?(:record_type)
|
206
|
+
|
207
|
+
# The last record is a person type and should not have a 'people' accessor
|
208
|
+
assert !records.last.respond_to?(:people)
|
209
|
+
|
210
|
+
# Should have added these accessors from the extra_columns argument above
|
211
|
+
assert records.first.respond_to?(:people)
|
212
|
+
assert records.last.respond_to?(:household_id)
|
213
|
+
|
214
|
+
assert_equal :household,records[0].record_type
|
215
|
+
assert_equal :person, records[1].record_type
|
232
216
|
end
|
233
|
-
|
234
|
-
def test_open
|
217
|
+
|
218
|
+
def test_open
|
235
219
|
record = Struct.new(:rectypeh,:phone,:mortgage,:record_type)
|
236
|
-
|
237
|
-
sample_data_path = File.dirname(__FILE__)
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
@record_types, # Records of different types have these labels
|
220
|
+
|
221
|
+
sample_data_path = File.dirname(__FILE__)
|
222
|
+
FLRFile.open(
|
223
|
+
"#{sample_data_path}/sample_out.dat", # data is in this file
|
224
|
+
"w",# open file for writing
|
225
|
+
@record_types, # Records of different types have these labels
|
242
226
|
@layouts, # metadata for creating record structs
|
243
|
-
1) do |fwf
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
227
|
+
1) do |fwf| # All metadata starting column locations are to be shifted 1 left
|
228
|
+
assert_equal FLRFile, fwf.class
|
229
|
+
fwf << record.new("H",1,2,:household)
|
230
|
+
fwf << ["H",1,3,:household]
|
231
|
+
end
|
232
|
+
|
249
233
|
assert File.exists?("#{sample_data_path}/sample.dat") # data is in this file
|
250
|
-
|
234
|
+
|
251
235
|
var = Struct.new(:name,:start,:len)
|
252
236
|
l = {:customer=>[var.new("name",1,25),var.new("age",26,3)]}
|
253
237
|
fwf = FLRFile.new(File.new("sample2_out.dat","w"),
|
254
|
-
:customer, l,1)
|
255
|
-
|
238
|
+
:customer, l,1)
|
239
|
+
|
256
240
|
fwf << ["joe",25,:customer]
|
257
241
|
fwf.close
|
258
|
-
|
259
242
|
end
|
260
|
-
|
261
243
|
|
262
|
-
def
|
244
|
+
def test_latin1
|
245
|
+
record = Struct.new(:rectypeh,:phone,:mortgage,:record_type)
|
246
|
+
|
247
|
+
sample_data_path = File.dirname(__FILE__)
|
248
|
+
FLRFile.open(
|
249
|
+
"#{sample_data_path}/sample_out.dat", # data is in this file
|
250
|
+
"w",# open file for writing
|
251
|
+
@record_types, # Records of different types have these labels
|
252
|
+
@layouts, # metadata for creating record structs
|
253
|
+
1) do |fwf| # All metadata starting column locations are to be shifted 1 left
|
254
|
+
assert_equal FLRFile, fwf.class
|
255
|
+
fwf << record.new("H",1,2,:household)
|
256
|
+
fwf << ["H",1,3,:household]
|
257
|
+
end
|
258
|
+
|
259
|
+
assert File.exists?("#{sample_data_path}/sample.dat") # data is in this file
|
260
|
+
|
261
|
+
var = Struct.new(:name,:start,:len)
|
262
|
+
l = {:customer=>[var.new("name",1,25),var.new("age",26,3)]}
|
263
|
+
fwf = FLRFile.new(File.new("sample2_out.dat","w",:encoding=>"ISO-8859-1"),
|
264
|
+
:customer, l,1)
|
265
|
+
|
266
|
+
latin1_name = "joe do#{241.chr}a".force_encoding("ISO-8859-1")
|
267
|
+
fwf << [latin1_name,25,:customer]
|
268
|
+
fwf.close
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_line_type
|
263
272
|
sample_data_path = File.dirname(__FILE__)
|
264
273
|
fwf = FLRFile.new(
|
265
274
|
File.new("#{sample_data_path}/sample.dat"),
|
@@ -269,42 +278,35 @@ def test_line_type
|
|
269
278
|
{:household=>[:people],:person=>[:household_id,:pserial]} # extra columns by record type
|
270
279
|
)
|
271
280
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
end
|
277
|
-
|
278
|
-
def test_ranges
|
279
|
-
end
|
281
|
+
assert_equal :unknown, fwf.line_type(nil)
|
282
|
+
assert_equal :household,fwf.line_type("H123")
|
283
|
+
assert_equal :person,fwf.line_type("P123")
|
284
|
+
assert_equal :unknown, fwf.line_type("C123")
|
285
|
+
end
|
280
286
|
|
281
|
-
def
|
282
|
-
end
|
287
|
+
def test_ranges
|
288
|
+
end
|
283
289
|
|
290
|
+
def test_in_range
|
291
|
+
end
|
284
292
|
|
285
|
-
|
286
293
|
def test_get_next_known_line_type
|
287
294
|
sample_data_path = File.dirname(__FILE__)
|
288
|
-
|
295
|
+
fwf = FLRFile.new(
|
289
296
|
File.new("#{sample_data_path}/sample_activities.dat"),
|
290
297
|
@record_types, # Record types to read from the file, all others will be ignored
|
291
298
|
@layouts,# metadata for all record types
|
292
299
|
1, # column 0 starts at logical location 1
|
293
300
|
{:household=>[:people],:person=>[:household_id,:pserial]} # extra columns by record type
|
294
301
|
)
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
unless rec.strip.empty?
|
300
|
-
|
301
|
-
assert ["P","H"].include?(rec[0..0])
|
302
|
+
# By reading the sample_activities file with only the household and person record types know
|
303
|
+
# we should get the activity and who records to be skipped.
|
304
|
+
while rec=fwf.get_next_known_line_type
|
302
305
|
|
306
|
+
unless rec.strip.empty?
|
307
|
+
assert ["P","H"].include?(rec[0..0])
|
303
308
|
end
|
309
|
+
end
|
304
310
|
end
|
305
|
-
|
306
|
-
end
|
307
|
-
|
308
|
-
end
|
309
|
-
|
310
311
|
|
312
|
+
end
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hflr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Davis
|
8
|
-
|
8
|
+
- Wade Stebbings
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
12
13
|
dependencies: []
|
13
|
-
description: "HFLR -- Hierarchical Fixed Length Records\n\nAllows you to read and
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
description: ! "HFLR -- Hierarchical Fixed Length Records\n\nAllows you to read and
|
15
|
+
write files of fixed width records when the file contains one or more\nthan one
|
16
|
+
type of record. \n\nInstall with 'gem install hflr'\n\nSee the tests and examples
|
17
|
+
bundled with this gem."
|
17
18
|
email: colin.c.davis@gmail.com
|
18
19
|
executables: []
|
19
20
|
extensions: []
|
@@ -34,34 +35,36 @@ files:
|
|
34
35
|
- test/flrfile_test.rb
|
35
36
|
- test/record_template_test.rb
|
36
37
|
- test/sample.dat
|
38
|
+
- test/sample_latin1.dat
|
37
39
|
- test/sample2_out.dat
|
38
40
|
- test/sample_activities.dat
|
39
41
|
- test/sample_out.dat
|
40
42
|
- test/test_helper.rb
|
41
43
|
- test/test_hflr.rb
|
42
|
-
homepage:
|
44
|
+
homepage:
|
43
45
|
licenses: []
|
44
46
|
metadata: {}
|
45
|
-
post_install_message:
|
47
|
+
post_install_message:
|
46
48
|
rdoc_options: []
|
47
49
|
require_paths:
|
48
50
|
- lib
|
49
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
50
52
|
requirements:
|
51
|
-
- - '>='
|
53
|
+
- - ! '>='
|
52
54
|
- !ruby/object:Gem::Version
|
53
55
|
version: '0'
|
54
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
57
|
requirements:
|
56
|
-
- - '>='
|
58
|
+
- - ! '>='
|
57
59
|
- !ruby/object:Gem::Version
|
58
60
|
version: '0'
|
59
61
|
requirements: []
|
60
|
-
rubyforge_project:
|
61
|
-
rubygems_version: 2.
|
62
|
-
signing_key:
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.0.2
|
64
|
+
signing_key:
|
63
65
|
specification_version: 4
|
64
|
-
summary: HFLR -- Hierarchical Fixed Length Records Allows you to read and write files
|
66
|
+
summary: HFLR -- Hierarchical Fixed Length Records Allows you to read and write files
|
67
|
+
of fixed width records when the file contains one or more than one type of record
|
65
68
|
test_files:
|
66
69
|
- test/test_hflr.rb
|
67
70
|
- test/test_helper.rb
|