Class: FixedLengthRecordFile
- Inherits:
-
Object
- Object
- FixedLengthRecordFile
- Includes:
- Enumerable
- Defined in:
- lib/hflr/fl_record_file.rb
Instance Attribute Summary collapse
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#record_template ⇒ Object
readonly
Returns the value of attribute record_template.
Class Method Summary collapse
-
.open(path, mode, record_types, record_layouts, logical_first_column = 0) ⇒ Object
Use when creating a new HFLR file.
Instance Method Summary collapse
-
#<<(record) ⇒ Object
This will take a Hash or Struct orArray; if an Array the record type must be the last element.
- #build_record(line) ⇒ Object
- #close ⇒ Object
- #each ⇒ Object
- #finished? ⇒ Boolean
- #get_next_known_line_type ⇒ Object
-
#get_record_type(line) ⇒ Object
If multiple record types, extract it from the string, otherwise just return the type of this file.
-
#initialize(source, record_types, record_layouts, logical_first_column = 0, extra_columns = nil) ⇒ FixedLengthRecordFile
constructor
A new instance of FixedLengthRecordFile.
- #line_type(line) ⇒ Object
- #next_record ⇒ Object
Constructor Details
#initialize(source, record_types, record_layouts, logical_first_column = 0, extra_columns = nil) ⇒ FixedLengthRecordFile
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/hflr/fl_record_file.rb', line 13 def initialize(source, record_types, record_layouts, logical_first_column=0, extra_columns = nil) # Allow record layouts like # {:type1=>[:var1=>1..5,:var2=>7..8],:type2=>[:var1=>1..1,:var2=>3..4]} # ... todo @line_number = 0 @file = source @record_type_labels=record_types @record_type_symbols = record_types.is_a?(Hash) ? record_types.invert : :none if extra_columns then @record_template = HFLR::RecordTemplate.create(record_layouts, @record_type_symbols, logical_first_column, extra_columns) else @record_template = HFLR::RecordTemplate.create(record_layouts, @record_type_symbols, logical_first_column) end end |
Instance Attribute Details
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
11 12 13 |
# File 'lib/hflr/fl_record_file.rb', line 11 def line_number @line_number end |
#record_template ⇒ Object (readonly)
Returns the value of attribute record_template.
11 12 13 |
# File 'lib/hflr/fl_record_file.rb', line 11 def record_template @record_template end |
Class Method Details
.open(path, mode, record_types, record_layouts, logical_first_column = 0) ⇒ Object
Use when creating a new HFLR file
94 95 96 97 98 99 100 101 102 |
# File 'lib/hflr/fl_record_file.rb', line 94 def self.open(path, mode, record_types, record_layouts, logical_first_column=0) file = File.open(path, mode) begin hflr_file = new(file, record_types, record_layouts, logical_first_column) yield hflr_file ensure file.close end end |
Instance Method Details
#<<(record) ⇒ Object
This will take a Hash or Struct orArray; if an Array the record type must be the last element
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/hflr/fl_record_file.rb', line 81 def <<(record) if record.is_a? Array @file.puts @record_template[record.last].build_line(record) else if @record_template[record[:record_type]] == nil then raise "Record type problem in output: #{record[:record_type].to_s} type on record, #{@record_template.keys.join(",")} types of templates" end @file.puts @record_template[record[:record_type]].build_line(record) end end |
#build_record(line) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/hflr/fl_record_file.rb', line 44 def build_record(line) return nil if line.nil? record_type = line_type(line) raise "Unknown record type at line #{@line_number.to_s}" if record_type == :unknown return @record_template[record_type].build_record(line.chomp) end |
#close ⇒ Object
33 34 35 |
# File 'lib/hflr/fl_record_file.rb', line 33 def close @file.close end |
#each ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/hflr/fl_record_file.rb', line 70 def each @file.each_line do |line| @line_number += 1 unless line_type(line) == :unknown data = build_record(line) yield data end end end |
#finished? ⇒ Boolean
29 30 31 |
# File 'lib/hflr/fl_record_file.rb', line 29 def finished? @file.eof? end |
#get_next_known_line_type ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/hflr/fl_record_file.rb', line 62 def get_next_known_line_type line = @file.gets while line_type(line) == :unknown and (not finished?) line = @file.gets end return line end |
#get_record_type(line) ⇒ Object
If multiple record types, extract it from the string, otherwise just return the type of this file
38 39 40 41 42 |
# File 'lib/hflr/fl_record_file.rb', line 38 def get_record_type(line) return nil if line.nil? return nil if line.strip.empty? @record_type_labels.is_a?(Hash) ? @record_type_labels[line[0..0]] : @record_type_labels end |
#line_type(line) ⇒ Object
57 58 59 60 |
# File 'lib/hflr/fl_record_file.rb', line 57 def line_type(line) record_type = get_record_type(line) return record_type ? record_type : :unknown end |
#next_record ⇒ Object
52 53 54 55 |
# File 'lib/hflr/fl_record_file.rb', line 52 def next_record @line_number += 1 build_record(get_next_known_line_type) end |