class CommaSplice::FileCorrector

Attributes

csv_content[R]
end_column[R]
end_line[R]
file_contents[R]
start_column[R]
start_line[R]

Public Class Methods

new(file_path, start_line: nil, end_line:nil, start_column: nil, end_column: nil) click to toggle source
# File lib/comma_splice/file_corrector.rb, line 5
def initialize(file_path, start_line: nil, end_line:nil, start_column: nil, end_column: nil)
  @file_path       = file_path
  @file_contents   = File.read(file_path, encoding: 'utf-8')

  @content_finder = ContentFinder.new(@file_contents, start_line, end_line)
  @csv_content   = @content_finder.content
  @start_line    = @content_finder.start_line
  @end_line      = @content_finder.end_line

  if start_column && end_column
    @start_column = start_column
    @end_column = end_column
  else
    finder = VariableColumnFinder.new(@csv_content[0], @csv_content[1..-1])
    @start_column = finder.start_column
    @end_column = finder.end_column
  end

  raise CommaSplice::Error, "empty contents #{file_path}" unless @csv_content.present?
end

Public Instance Methods

bad_lines() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 30
def bad_lines
  line_correctors.select(&:needs_correcting?).collect(&:original)
end
corrected() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 42
def corrected
  @corrected ||= [
    @file_contents.lines[0, @start_line],
    corrected_lines,
    @file_contents.lines[@end_line, -1]
  ].flatten
end
header() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 26
def header
  @header ||= Line.new(csv_content.first)
end
needs_correcting?() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 34
def needs_correcting?
  bad_lines.size.positive?
end
needs_manual_input?() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 38
def needs_manual_input?
  line_correctors.any?(&:needs_manual_input?)
end
save(path) click to toggle source
# File lib/comma_splice/file_corrector.rb, line 54
def save(path)
  File.open(path, 'w+') do |f|
    corrected.each_with_index do |line, index|
      # don't add an extra line break at the end
      f.puts line if corrected.size > index && line
    end
  end
end
save!() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 50
def save!
  save(@file_path)
end
to_json() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 63
def to_json
  @content_finder.parsed.try(:to_json)
end

Private Instance Methods

corrected_lines() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 75
def corrected_lines
  line_correctors.collect do |line|
    if line.needs_correcting?
      line.corrected
    else
      line.original
    end
  end
end
line_correctors() click to toggle source
# File lib/comma_splice/file_corrector.rb, line 69
def line_correctors
  @line_correctors ||= csv_content.collect do |line|
    LineCorrector.new(header, Line.new(line), @start_column, @end_column)
  end
end