Class: FixedWidthFileValidator::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/fixed_width_file_validator/file_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(data_file, parser = nil, settings = {}) ⇒ FileReader



3
4
5
6
7
8
9
10
11
# File 'lib/fixed_width_file_validator/file_reader.rb', line 3

def initialize(data_file, parser = nil, settings = {})
  @skip_top_lines = settings[:skip_top_lines] || 0
  @skip_bottom_lines = settings[:skip_bottom_lines] || 0
  @line_num = 0
  @buffer = []
  @parser = parser
  @skip_top_done = false
  @data_file = data_file
end

Instance Method Details

#closeObject



30
31
32
33
34
# File 'lib/fixed_width_file_validator/file_reader.rb', line 30

def close
  @file&.close
ensure
  @file = nil
end

#each_recordObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/fixed_width_file_validator/file_reader.rb', line 19

def each_record
  record = next_record
  until record.nil?
    # puts "#{Time.now} at #{@line_num}" if @line_num % 10000 == 0
    yield record
    record = next_record
  end
ensure
  close
end

#find_non_unique_values(field_list = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fixed_width_file_validator/file_reader.rb', line 36

def find_non_unique_values(field_list = [])
  return if field_list.empty?

  lookup_hash = build_unique_value_lookup_hash(field_list)

  result = {}
  field_list.each do |field_name|
    result[field_name] = lookup_hash[field_name].select { |_k, v| v.count > 1 }
  end
  result
end

#next_recordObject



13
14
15
16
17
# File 'lib/fixed_width_file_validator/file_reader.rb', line 13

def next_record
  line_num, content = readline_with_skip
  return unless line_num
  @parser ? @parser.parse(content, line_num, content) : content.chop
end