Class: CommaSplice::LineCorrector

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_line, value_line, left_bounds = 0, right_bounds = -1,, separator = ',') ⇒ LineCorrector

Returns a new instance of LineCorrector.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/comma_splice/line_corrector.rb', line 5

def initialize(header_line, value_line, left_bounds = 0, right_bounds = -1, separator = ',')
  header_line = Line.new(header_line, separator) unless header_line.is_a?(Line)
  value_line  = Line.new(value_line, separator) unless value_line.is_a?(Line)

  @header_line = header_line
  @value_line = value_line
  @headers = header_line.values
  @values = value_line.values
  @left_bounds = left_bounds
  @right_bounds = right_bounds
  @separator = separator

  raise 'right bounds must be negative' unless right_bounds.negative?
  raise 'left bounds must be not be negative' if left_bounds.negative?
end

Instance Attribute Details

#header_lineObject (readonly)

Returns the value of attribute header_line.



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

def header_line
  @header_line
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#left_boundsObject (readonly)

Returns the value of attribute left_bounds.



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

def left_bounds
  @left_bounds
end

#right_boundsObject (readonly)

Returns the value of attribute right_bounds.



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

def right_bounds
  @right_bounds
end

#separatorObject (readonly)

Returns the value of attribute separator.



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

def separator
  @separator
end

#value_lineObject (readonly)

Returns the value of attribute value_line.



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

def value_line
  @value_line
end

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#all_optionsObject



34
35
36
# File 'lib/comma_splice/line_corrector.rb', line 34

def all_options
  corrector.ranked_options
end

#correctedObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/comma_splice/line_corrector.rb', line 42

def corrected
  # you want to provide this with the smallest set of possibilities
  # for performance reasons. Left and right bounds limit the values
  # where the comma error could be

  # For instance, with the following headers:
  # [playid,playtype,genre,timestamp,artist,title,albumtitle,label,prepost,programtype,iswebcast,isrequest]
  # the only values that could contain an extra comma are "artist,title,albumtitle,label"
  # therefore our left_bounds = 4, right_bounds = -5

  values_before = values[0...left_bounds]
  values_after = values.slice(right_bounds + 1, -(right_bounds + 1))
  generate_csv_line([values_before, corrector.correction, values_after].flatten)
end

#needs_correcting?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/comma_splice/line_corrector.rb', line 21

def needs_correcting?
  @values&.size&.positive? && @headers.size != @values.size
end

#needs_manual_input?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/comma_splice/line_corrector.rb', line 25

def needs_manual_input?
  corrector.needs_manual_input?
end

#option_countObject



29
30
31
32
# File 'lib/comma_splice/line_corrector.rb', line 29

def option_count
  puts corrector.best_options
  corrector.best_options.size
end

#originalObject



38
39
40
# File 'lib/comma_splice/line_corrector.rb', line 38

def original
  generate_csv_line(@values)
end


57
58
59
# File 'lib/comma_splice/line_corrector.rb', line 57

def print_all_options
  corrector.print_all_options
end