hflr 0.10.1 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -3
- data/README.txt +0 -0
- data/Rakefile +1 -0
- data/hflr.gemspec +4 -6
- data/lib/hflr.rb +1 -1
- data/lib/hflr/fl_record_file.rb +18 -13
- data/lib/hflr/hflr.rb +0 -0
- data/lib/hflr/record_template.rb +7 -7
- data/test/customer_orders.dat +0 -0
- data/test/customers.dat +0 -0
- data/test/examples.rb +5 -3
- data/test/{fixed_length_record_file_test.rb → flrfile_test.rb} +13 -12
- data/test/record_template_test.rb +0 -0
- data/test/sample.dat +1 -0
- data/test/sample2_out.dat +0 -0
- data/test/sample_activities.dat +0 -0
- data/test/sample_out.dat +0 -0
- data/test/test_helper.rb +0 -0
- data/test/test_hflr.rb +1 -1
- metadata +5 -9
- data/bin/hflr +0 -8
- data/spec/hflr_spec.rb +0 -7
- data/spec/spec_helper.rb +0 -16
data/History.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
==
|
1
|
+
== 0.11.0 / 2009-08-04
|
2
2
|
|
3
|
-
*
|
4
|
-
*
|
3
|
+
* Removed useless files
|
4
|
+
* Corrected version number
|
data/README.txt
CHANGED
File without changes
|
data/Rakefile
CHANGED
data/hflr.gemspec
CHANGED
@@ -2,12 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{hflr}
|
5
|
-
s.version = "
|
5
|
+
s.version = "0.11.0"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Colin Davis"]
|
9
|
-
s.date = %q{2009-08-
|
10
|
-
s.default_executable = %q{hflr}
|
9
|
+
s.date = %q{2009-08-04}
|
11
10
|
s.description = %q{HFLR -- Hierarchical Fixed Length Records
|
12
11
|
|
13
12
|
Allows you to read and write files of fixed width records when the file contains one or more
|
@@ -17,9 +16,8 @@ Install with 'gem install hflr'
|
|
17
16
|
|
18
17
|
See the tests and examples bundled with this gem.}
|
19
18
|
s.email = %q{colin.c.davis@gmail.com}
|
20
|
-
s.
|
21
|
-
s.
|
22
|
-
s.files = ["History.txt", "README.txt", "Rakefile", "bin/hflr", "lib/hflr.rb", "lib/hflr/fl_record_file.rb", "lib/hflr/hflr.rb", "lib/hflr/record_template.rb", "sample2_out.dat", "spec/hflr_spec.rb", "spec/spec_helper.rb", "test/customer_orders.dat", "test/customers.dat", "test/examples.rb", "test/fixed_length_record_file_test.rb", "test/record_template_test.rb", "test/sample.dat", "test/sample2_out.dat", "test/sample_activities.dat", "test/sample_out.dat", "test/test_helper.rb", "test/test_hflr.rb"]
|
19
|
+
s.extra_rdoc_files = ["History.txt", "README.txt"]
|
20
|
+
s.files = ["History.txt", "README.txt", "Rakefile", "hflr.gemspec", "lib/hflr.rb", "lib/hflr/fl_record_file.rb", "lib/hflr/hflr.rb", "lib/hflr/record_template.rb", "spec/hflr_spec.rb", "spec/spec_helper.rb", "test/customer_orders.dat", "test/customers.dat", "test/examples.rb", "test/fixed_length_record_file_test.rb", "test/record_template_test.rb", "test/sample.dat", "test/sample2_out.dat", "test/sample_activities.dat", "test/sample_out.dat", "test/test_helper.rb", "test/test_hflr.rb"]
|
23
21
|
s.homepage = %q{http://ruff.rubyforge.org}
|
24
22
|
s.rdoc_options = ["--main", "README.txt"]
|
25
23
|
s.require_paths = ["lib"]
|
data/lib/hflr.rb
CHANGED
data/lib/hflr/fl_record_file.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/record_template')
|
1
2
|
|
2
3
|
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + "/record_template")
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
-
class
|
7
|
+
class FLRFile
|
8
8
|
|
9
9
|
include Enumerable
|
10
10
|
|
@@ -22,8 +22,7 @@ class FixedLengthRecordFile
|
|
22
22
|
@record_template = HFLR::RecordTemplate.create(record_layouts, @record_type_symbols, logical_first_column, extra_columns)
|
23
23
|
else
|
24
24
|
@record_template = HFLR::RecordTemplate.create(record_layouts, @record_type_symbols, logical_first_column)
|
25
|
-
end
|
26
|
-
|
25
|
+
end
|
27
26
|
end
|
28
27
|
|
29
28
|
def finished?
|
@@ -59,12 +58,15 @@ def line_type(line)
|
|
59
58
|
return record_type ? record_type : :unknown
|
60
59
|
end
|
61
60
|
|
62
|
-
def get_next_known_line_type
|
63
|
-
line = @file.gets
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
61
|
+
def get_next_known_line_type
|
62
|
+
line = @file.gets
|
63
|
+
record_type = line_type(line)
|
64
|
+
while record_type == :unknown and (not finished?)
|
65
|
+
line = @file.gets
|
66
|
+
record_type = line_type(line)
|
67
|
+
end
|
68
|
+
record_type == :unknown ? nil : line
|
69
|
+
|
68
70
|
end
|
69
71
|
|
70
72
|
def each
|
@@ -77,16 +79,19 @@ def each
|
|
77
79
|
end
|
78
80
|
end
|
79
81
|
|
80
|
-
# This will take a Hash or Struct orArray; if an Array the record type must be the last element
|
82
|
+
# This will take a Hash or Struct orArray; if an Array the record type must be the last element when
|
83
|
+
# the record layout has more than one record type.
|
81
84
|
def <<(record)
|
82
85
|
if record.is_a? Array
|
83
|
-
|
86
|
+
record_type = @record_type_symbols == :none ? @record_template.keys.first : record.last
|
87
|
+
@file.puts @record_template[record_type].build_line(record)
|
84
88
|
else
|
89
|
+
record_type = @record_type_symbols == :none ?@record_template.keys.first : record[:record_type]
|
85
90
|
if @record_template[record[:record_type]] == nil then
|
86
91
|
raise "Record type problem in output: #{record[:record_type].to_s} type on record, #{@record_template.keys.join(",")} types of templates"
|
87
92
|
end
|
88
93
|
|
89
|
-
@file.puts @record_template[
|
94
|
+
@file.puts @record_template[record_type].build_line(record)
|
90
95
|
end
|
91
96
|
end
|
92
97
|
|
data/lib/hflr/hflr.rb
CHANGED
File without changes
|
data/lib/hflr/record_template.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
module HFLR
|
3
3
|
|
4
4
|
class RecordTemplate
|
5
|
-
|
6
|
-
|
5
|
+
UnfilledChar = ' '
|
6
|
+
MissingOutput = "ZZZZZZZZZZZZZZZZZZZZZ"
|
7
7
|
|
8
8
|
attr_reader :record_structure, :field_pattern,:record_type,:record_type_label
|
9
9
|
attr_accessor :strip_whitespace
|
@@ -36,7 +36,7 @@ def self.create(record_layouts, record_type_symbols, first_column_location, extr
|
|
36
36
|
return templates
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
private
|
40
40
|
# If the name exists already do not replace it, but add extra columns not to be mapped by the unpack field patterns
|
41
41
|
# and ensure the record_type variable is added.
|
42
42
|
# Since 'record_type' may not be in the metadata we don't want to map it to a
|
@@ -53,12 +53,12 @@ end
|
|
53
53
|
return new_names
|
54
54
|
end
|
55
55
|
|
56
|
-
def self.get_pattern(layout, first_column_location=0)
|
57
|
-
|
58
|
-
|
56
|
+
def self.get_pattern(layout, first_column_location=0)
|
59
57
|
layout.map {|l| '@' + (l.start - first_column_location).to_s + 'A' + l.len.to_s}.to_s
|
60
58
|
end
|
61
|
-
|
59
|
+
|
60
|
+
public
|
61
|
+
|
62
62
|
def build_record(line)
|
63
63
|
rec = line.unpack(@field_pattern)
|
64
64
|
rec.map{|f| f.strip!} if @strip_whitespace
|
data/test/customer_orders.dat
CHANGED
File without changes
|
data/test/customers.dat
CHANGED
File without changes
|
data/test/examples.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require File.dirname(__FILE__) + "/../lib/hflr"
|
2
2
|
|
3
3
|
|
4
4
|
# Read a file with only one record type (no record type markers)
|
@@ -10,8 +10,10 @@ Column.new("name",1,25),
|
|
10
10
|
Column.new("zip",26,5),
|
11
11
|
Column.new("balance",31,5)]}
|
12
12
|
|
13
|
-
customer_file =
|
13
|
+
customer_file = FLRFile.new(File.new("customers.dat"), :customer, columns, 1, [:line_number])
|
14
14
|
|
15
|
+
|
16
|
+
# You can read through the file and access the fields with methods named after the columns:
|
15
17
|
customer_file.each do |record|
|
16
18
|
puts "Customer #{customer_file.line_number.to_s} #{record.name} #{record.zip} "
|
17
19
|
end
|
@@ -32,7 +34,7 @@ layouts = {:customer=>[
|
|
32
34
|
Column.new("date",9,10),]}
|
33
35
|
|
34
36
|
|
35
|
-
customer_orders_file =
|
37
|
+
customer_orders_file = FLRFile.new(
|
36
38
|
File.new("customer_orders.dat"),
|
37
39
|
{"C"=>:customer,"O"=>:order},# Use these characters as record type markers
|
38
40
|
layouts,
|
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
|
3
3
|
|
4
|
-
class
|
4
|
+
class FLRFileTest < Test::Unit::TestCase
|
5
5
|
|
6
6
|
|
7
7
|
|
@@ -24,7 +24,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
24
24
|
|
25
25
|
def test_initialize
|
26
26
|
sample_data_path = File.dirname(__FILE__)
|
27
|
-
fwf =
|
27
|
+
fwf = FLRFile.new(
|
28
28
|
File.new("#{sample_data_path}/sample.dat"),
|
29
29
|
@record_types, # Record types to read from the file, all others will be ignored
|
30
30
|
@layouts,# metadata for all record types
|
@@ -40,7 +40,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
40
40
|
assert p_struct.respond_to?(:record_type),"Should have record_type method"
|
41
41
|
|
42
42
|
|
43
|
-
fwf =
|
43
|
+
fwf = FLRFile.new(
|
44
44
|
File.new("#{sample_data_path}/sample.dat"),
|
45
45
|
@record_types, # Record types to read from the file, all others will be ignored
|
46
46
|
@layouts,# metadata for all record types
|
@@ -54,7 +54,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
54
54
|
|
55
55
|
def test_get_record_type
|
56
56
|
sample_data_path = File.dirname(__FILE__)
|
57
|
-
fwf =
|
57
|
+
fwf = FLRFile.new(
|
58
58
|
File.new("#{sample_data_path}/sample.dat"),
|
59
59
|
@record_types, # Record types to read from the file, all others will be ignored
|
60
60
|
@layouts,# metadata for all record types
|
@@ -73,7 +73,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
73
73
|
|
74
74
|
def test_build_record
|
75
75
|
sample_data_path = File.dirname(__FILE__)
|
76
|
-
fwf =
|
76
|
+
fwf = FLRFile.new(
|
77
77
|
File.new("#{sample_data_path}/sample.dat"),
|
78
78
|
@record_types, # Record types to read from the file, all others will be ignored
|
79
79
|
@layouts,# metadata for all record types
|
@@ -94,7 +94,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
94
94
|
|
95
95
|
def test_each
|
96
96
|
sample_data_path = File.dirname(__FILE__)
|
97
|
-
fwf =
|
97
|
+
fwf = FLRFile.new(
|
98
98
|
File.new("#{sample_data_path}/sample.dat"),
|
99
99
|
@record_types,
|
100
100
|
@layouts,
|
@@ -115,7 +115,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
115
115
|
|
116
116
|
def test_next_record
|
117
117
|
sample_data_path = File.dirname(__FILE__)
|
118
|
-
fwf =
|
118
|
+
fwf = FLRFile.new(
|
119
119
|
File.new("#{sample_data_path}/sample.dat"), # data is in this file
|
120
120
|
@record_types, # Records of different types have these labels
|
121
121
|
@layouts, # metadata for creating record structs
|
@@ -125,6 +125,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
125
125
|
records = []
|
126
126
|
while rec = fwf.next_record do
|
127
127
|
records << rec
|
128
|
+
|
128
129
|
end
|
129
130
|
|
130
131
|
assert_equal :household, records.first.record_type
|
@@ -147,13 +148,13 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
147
148
|
record = Struct.new(:rectypeh,:phone,:mortgage,:record_type)
|
148
149
|
|
149
150
|
sample_data_path = File.dirname(__FILE__)
|
150
|
-
|
151
|
+
FLRFile.open(
|
151
152
|
"#{sample_data_path}/sample_out.dat", # data is in this file
|
152
153
|
"w",# open file for writing
|
153
154
|
@record_types, # Records of different types have these labels
|
154
155
|
@layouts, # metadata for creating record structs
|
155
156
|
1) do |fwf|# All metadata starting column locations are to be shifted 1 left
|
156
|
-
assert_equal
|
157
|
+
assert_equal FLRFile, fwf.class
|
157
158
|
fwf << record.new("H",1,2,:household)
|
158
159
|
fwf << ["H",1,3,:household]
|
159
160
|
end
|
@@ -162,7 +163,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
162
163
|
|
163
164
|
var = Struct.new(:name,:start,:len)
|
164
165
|
l = {:customer=>[var.new("name",1,25),var.new("age",26,3)]}
|
165
|
-
fwf =
|
166
|
+
fwf = FLRFile.new(File.new("sample2_out.dat","w"),
|
166
167
|
:customer, l,1)
|
167
168
|
|
168
169
|
fwf << ["joe",25,:customer]
|
@@ -173,7 +174,7 @@ class FixedLengthRecordFileTest < Test::Unit::TestCase
|
|
173
174
|
|
174
175
|
def test_line_type
|
175
176
|
sample_data_path = File.dirname(__FILE__)
|
176
|
-
fwf =
|
177
|
+
fwf = FLRFile.new(
|
177
178
|
File.new("#{sample_data_path}/sample.dat"),
|
178
179
|
@record_types, # Record types to read from the file, all others will be ignored
|
179
180
|
@layouts,# metadata for all record types
|
@@ -189,7 +190,7 @@ end
|
|
189
190
|
|
190
191
|
def test_get_next_known_line_type
|
191
192
|
sample_data_path = File.dirname(__FILE__)
|
192
|
-
fwf =
|
193
|
+
fwf = FLRFile.new(
|
193
194
|
File.new("#{sample_data_path}/sample_activities.dat"),
|
194
195
|
@record_types, # Record types to read from the file, all others will be ignored
|
195
196
|
@layouts,# metadata for all record types
|
File without changes
|
data/test/sample.dat
CHANGED
data/test/sample2_out.dat
CHANGED
File without changes
|
data/test/sample_activities.dat
CHANGED
File without changes
|
data/test/sample_out.dat
CHANGED
File without changes
|
data/test/test_helper.rb
CHANGED
File without changes
|
data/test/test_hflr.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hflr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Colin Davis
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-04 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -32,27 +32,23 @@ description: |-
|
|
32
32
|
|
33
33
|
See the tests and examples bundled with this gem.
|
34
34
|
email: colin.c.davis@gmail.com
|
35
|
-
executables:
|
36
|
-
|
35
|
+
executables: []
|
36
|
+
|
37
37
|
extensions: []
|
38
38
|
|
39
39
|
extra_rdoc_files:
|
40
40
|
- History.txt
|
41
41
|
- README.txt
|
42
|
-
- bin/hflr
|
43
42
|
files:
|
44
43
|
- History.txt
|
45
44
|
- README.txt
|
46
45
|
- Rakefile
|
47
|
-
- bin/hflr
|
48
46
|
- hflr.gemspec
|
49
47
|
- lib/hflr.rb
|
50
48
|
- lib/hflr/fl_record_file.rb
|
51
49
|
- lib/hflr/hflr.rb
|
52
50
|
- lib/hflr/record_template.rb
|
53
51
|
- sample2_out.dat
|
54
|
-
- spec/hflr_spec.rb
|
55
|
-
- spec/spec_helper.rb
|
56
52
|
- tasks/ann.rake
|
57
53
|
- tasks/bones.rake
|
58
54
|
- tasks/gem.rake
|
@@ -69,7 +65,7 @@ files:
|
|
69
65
|
- test/customer_orders.dat
|
70
66
|
- test/customers.dat
|
71
67
|
- test/examples.rb
|
72
|
-
- test/
|
68
|
+
- test/flrfile_test.rb
|
73
69
|
- test/record_template_test.rb
|
74
70
|
- test/sample.dat
|
75
71
|
- test/sample2_out.dat
|
data/bin/hflr
DELETED
data/spec/hflr_spec.rb
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
|
2
|
-
require File.expand_path(
|
3
|
-
File.join(File.dirname(__FILE__), %w[.. lib hflr]))
|
4
|
-
|
5
|
-
Spec::Runner.configure do |config|
|
6
|
-
# == Mock Framework
|
7
|
-
#
|
8
|
-
# RSpec uses it's own mocking framework by default. If you prefer to
|
9
|
-
# use mocha, flexmock or RR, uncomment the appropriate line:
|
10
|
-
#
|
11
|
-
# config.mock_with :mocha
|
12
|
-
# config.mock_with :flexmock
|
13
|
-
# config.mock_with :rr
|
14
|
-
end
|
15
|
-
|
16
|
-
# EOF
|