Class: CommaSplice::FileCorrector

Inherits:
Object
  • Object
show all
Defined in:
lib/comma_splice/file_corrector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, start_line: nil, end_line: nil, start_column: nil, end_column: nil, separator: ',') ⇒ FileCorrector

Returns a new instance of FileCorrector.

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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, separator: ',')
  @file_path       = file_path
  @file_contents   = File.read(file_path, encoding: 'utf-8')
  @separator       = separator


  @content_finder = ContentFinder.new(@file_contents, start_line, end_line, separator)
  @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], @separator)
    @start_column = finder.start_column
    @end_column = finder.end_column
  end

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

Instance Attribute Details

#csv_contentObject (readonly)

Returns the value of attribute csv_content.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def csv_content
  @csv_content
end

#end_columnObject (readonly)

Returns the value of attribute end_column.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def end_column
  @end_column
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def end_line
  @end_line
end

#file_contentsObject (readonly)

Returns the value of attribute file_contents.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def file_contents
  @file_contents
end

#separatorObject (readonly)

Returns the value of attribute separator.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def separator
  @separator
end

#start_columnObject (readonly)

Returns the value of attribute start_column.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def start_column
  @start_column
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



3
4
5
# File 'lib/comma_splice/file_corrector.rb', line 3

def start_line
  @start_line
end

Instance Method Details

#bad_linesObject



31
32
33
# File 'lib/comma_splice/file_corrector.rb', line 31

def bad_lines
  line_correctors.select(&:needs_correcting?).collect(&:original)
end

#correctedObject



43
44
45
46
47
48
49
# File 'lib/comma_splice/file_corrector.rb', line 43

def corrected
  @corrected ||= [
    @file_contents.lines[0, @start_line],
    corrected_lines,
    @file_contents.lines[@end_line, -1]
  ].flatten
end

#headerObject



27
28
29
# File 'lib/comma_splice/file_corrector.rb', line 27

def header
  @header ||= Line.new(csv_content.first, @separator)
end

#needs_correcting?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/comma_splice/file_corrector.rb', line 35

def needs_correcting?
  bad_lines.size.positive?
end

#needs_manual_input?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/comma_splice/file_corrector.rb', line 39

def needs_manual_input?
  line_correctors.any?(&:needs_manual_input?)
end

#save(path) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/comma_splice/file_corrector.rb', line 55

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!Object



51
52
53
# File 'lib/comma_splice/file_corrector.rb', line 51

def save!
  save(@file_path)
end

#to_json(*_args) ⇒ Object



64
65
66
# File 'lib/comma_splice/file_corrector.rb', line 64

def to_json(*_args)
  @content_finder.parsed.try(:to_json)
end