Class: FLRFile
- Inherits:
-
Object
- Object
- FLRFile
- 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 when the record layout has more than one record type.
- #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) ⇒ FLRFile
constructor
A new instance of FLRFile.
- #line_type(line) ⇒ Object
- #next_record ⇒ Object
Constructor Details
#initialize(source, record_types, record_layouts, logical_first_column = 0, extra_columns = nil) ⇒ FLRFile
Returns a new instance of FLRFile.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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]} if record_layouts.values.first.is_a? Hash record_layouts = create_layouts(record_layouts) end @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
101 102 103 104 105 106 107 108 109 |
# File 'lib/hflr/fl_record_file.rb', line 101 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 when the record layout has more than one record type.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hflr/fl_record_file.rb', line 86 def <<(record) if record.is_a? Array record_type = @record_type_symbols == :none ? @record_template.keys.first : record.last @file.puts @record_template[record_type].build_line(record) else record_type = @record_type_symbols == :none ?@record_template.keys.first : record[:record_type] 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_type].build_line(record) end end |
#build_record(line) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/hflr/fl_record_file.rb', line 46 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
35 36 37 |
# File 'lib/hflr/fl_record_file.rb', line 35 def close @file.close end |
#each ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/hflr/fl_record_file.rb', line 74 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
31 32 33 |
# File 'lib/hflr/fl_record_file.rb', line 31 def finished? @file.eof? end |
#get_next_known_line_type ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/hflr/fl_record_file.rb', line 64 def get_next_known_line_type line = @file.gets record_type = line_type(line) while record_type == :unknown and (not finished?) line = @file.gets record_type = line_type(line) end record_type == :unknown ? nil : line end |
#get_record_type(line) ⇒ Object
If multiple record types, extract it from the string, otherwise just return the type of this file
40 41 42 43 44 |
# File 'lib/hflr/fl_record_file.rb', line 40 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
59 60 61 62 |
# File 'lib/hflr/fl_record_file.rb', line 59 def line_type(line) record_type = get_record_type(line) return record_type ? record_type : :unknown end |
#next_record ⇒ Object
54 55 56 57 |
# File 'lib/hflr/fl_record_file.rb', line 54 def next_record @line_number += 1 build_record(get_next_known_line_type) end |