hflr 1.4.1 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -29,3 +29,7 @@
29
29
 
30
30
  * conditionally use force_encoding, if method available
31
31
 
32
+
33
+ == 1.5.1
34
+
35
+ * Add set_random() and get_random_record(record_number) for random access mode. See examples.rb in test directory for example code.
data/hflr.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{hflr}
5
- s.version = "1.4.1"
5
+ s.version = "1.5.1"
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", "Wade Stebbings"]
@@ -25,7 +25,7 @@ See the tests and examples bundled with this gem.}
25
25
  #s.rdoc_options = ["--main", "README.txt"]
26
26
  s.require_paths = ["lib"]
27
27
 
28
- s.rubygems_version = %q{1.4.1}
28
+ s.rubygems_version = %q{1.5.1}
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
@@ -24,20 +24,44 @@ class FLRFile
24
24
  end
25
25
  end
26
26
 
27
- def set_fast
28
- @fast = !@record_type_labels.is_a?(Hash)
29
- unless @fast
30
- raise "Cannot set fast mode with more than one record type."
27
+ def set_random
28
+ if @fast
29
+ raise "Cannot set random access mode with fast mode already set."
30
+ end
31
+
32
+ @random = !@record_type_labels.is_a?(Hash)
33
+ @random or raise "Cannot set random mode with more than one record type."
34
+ @width = get_record_width_from_file
35
+ end
36
+
37
+ def get_random_record(record_number)
38
+ return record_number if record_number.nil?
39
+ offset = record_number * @width
40
+ begin
41
+ @file.pos = offset
42
+ line = @file.read(@width)
43
+ rescue Exception=>msg
44
+ return nil
45
+ end
46
+ build_record(line)
31
47
  end
32
- if @fast
48
+
49
+
50
+ def set_fast
51
+ if @random
52
+ raise "Already set random access mode"
53
+ end
54
+
55
+ @fast = !@record_type_labels.is_a?(Hash)
56
+ unless @fast
57
+ raise "Cannot set fast mode with more than one record type."
58
+ end
59
+ if @fast
33
60
  @width = get_record_width_from_file
34
61
 
35
-
36
62
  records_to_take = 100000000 / @width
37
-
38
63
  @buffer_size = @width * records_to_take
39
64
 
40
-
41
65
  @position=0
42
66
  @current_buffer=nil
43
67
  end
@@ -214,6 +238,7 @@ def offsets_to_read(ranges, width)
214
238
  end
215
239
 
216
240
  def get_record_width_from_file
241
+ @file.rewind
217
242
  width = @file.gets.size
218
243
  @file.rewind
219
244
  width
data/test/examples.rb CHANGED
@@ -23,7 +23,24 @@ end
23
23
  def show(record)
24
24
  print record.members.map{|m| m.to_s + ": " + record[m].to_s}.join(", ") + "\n"
25
25
  end
26
+
27
+
28
+ # You can read the file in random order:
29
+ customer_file.set_random
30
+ record = customer_file.get_random_record(2)
31
+ puts "Record 2 randomly accessed: #{record.name} #{record.zip} "
32
+ record = customer_file.get_random_record(0)
33
+ puts "Record 0 randomly accessed: #{record.name} #{record.zip} "
34
+ record = customer_file.get_random_record(1)
35
+ puts "Record 1 randomly accessed: #{record.name} #{record.zip} "
36
+
37
+ record = customer_file.get_random_record(11)
38
+ puts "Returns a nil if record is off the end of the file: #{record.to_s}"
26
39
 
40
+
41
+
42
+
43
+ # You can read through a file with more than one record type:
27
44
  # metadata for customer_orders file
28
45
  layouts = {:customer=>[
29
46
  Column.new("name",1,25),
metadata CHANGED
@@ -1,21 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hflr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
5
- prerelease:
4
+ version: 1.5.1
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Colin Davis
9
9
  - Wade Stebbings
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
  date: 2013-03-08 00:00:00.000000000 Z
14
14
  dependencies: []
15
- description: ! "HFLR -- Hierarchical Fixed Length Records\n\nAllows you to read and
16
- write files of fixed width records when the file contains one or more\nthan one
17
- type of record. \n\nInstall with 'gem install hflr'\n\nSee the tests and examples
18
- bundled with this gem."
15
+ description: "HFLR -- Hierarchical Fixed Length Records\n\nAllows you to read and\
16
+ \ write files of fixed width records when the file contains one or more\nthan one\
17
+ \ type of record. \n\nInstall with 'gem install hflr'\n\nSee the tests and examples\
18
+ \ bundled with this gem."
19
19
  email: colin.c.davis@gmail.com
20
20
  executables: []
21
21
  extensions: []
@@ -42,31 +42,32 @@ files:
42
42
  - test/sample_out.dat
43
43
  - test/test_helper.rb
44
44
  - test/test_hflr.rb
45
- homepage:
45
+ homepage:
46
46
  licenses: []
47
- post_install_message:
47
+ post_install_message:
48
48
  rdoc_options: []
49
49
  require_paths:
50
50
  - lib
51
51
  required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
52
  requirements:
54
- - - ! '>='
53
+ - - ">="
55
54
  - !ruby/object:Gem::Version
56
- version: '0'
57
- required_rubygems_version: !ruby/object:Gem::Requirement
55
+ version: !binary |-
56
+ MA==
58
57
  none: false
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - ! '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: '0'
62
+ version: !binary |-
63
+ MA==
64
+ none: false
63
65
  requirements: []
64
- rubyforge_project:
66
+ rubyforge_project:
65
67
  rubygems_version: 1.8.24
66
- signing_key:
68
+ signing_key:
67
69
  specification_version: 3
68
- summary: HFLR -- Hierarchical Fixed Length Records Allows you to read and write files
69
- of fixed width records when the file contains one or more than one type of record
70
+ summary: 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
70
71
  test_files:
71
72
  - test/test_hflr.rb
72
73
  - test/test_helper.rb