Class: FixTSVConflict::Repairman

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/fix_tsv_conflict/repairman.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#blank, #dump, #error, #info, #log, #notice, #warn

Constructor Details

#initialize(stdin: $stdin, stderr: $stderr) ⇒ Repairman



11
12
13
14
# File 'lib/fix_tsv_conflict/repairman.rb', line 11

def initialize(stdin: $stdin, stderr: $stderr)
  @stdin  = stdin
  @stderr = stderr
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



9
10
11
# File 'lib/fix_tsv_conflict/repairman.rb', line 9

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



9
10
11
# File 'lib/fix_tsv_conflict/repairman.rb', line 9

def stdin
  @stdin
end

Instance Method Details

#handle(left, lbranch, right, rbranch) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/fix_tsv_conflict/repairman.rb', line 56

def handle(left, lbranch, right, rbranch)
  conflict = Conflict.new(left, lbranch, right, rbranch)
  print_conflict(conflict)
  result = resolver.resolve(conflict)
  print_result(result)
  result
end

#load_tabs_count(header) ⇒ Object



52
53
54
# File 'lib/fix_tsv_conflict/repairman.rb', line 52

def load_tabs_count(header)
  resolver.tabs = header.count(TAB)
end


64
65
66
67
68
69
# File 'lib/fix_tsv_conflict/repairman.rb', line 64

def print_conflict(conflict)
  info "Found a conflict:"
  blank
  dump conflict.to_a
  blank
end


71
72
73
74
75
76
77
# File 'lib/fix_tsv_conflict/repairman.rb', line 71

def print_result(result)
  notice "Resolved to:"
  blank
  dump result
  blank
  blank
end

#repair(source) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fix_tsv_conflict/repairman.rb', line 20

def repair(source)
  result = []
  branch = nil
  left,  lbranch = [], nil
  right, rbranch = [], nil

  source.each_line.with_index do |line, i|
    if i.zero?
      load_tabs_count(line)
      result << line
    elsif line.start_with?(LEFT)
      lbranch = line.chomp.split(" ").last
      branch = left
    elsif line.start_with?(SEP)
      branch = right
    elsif line.start_with?(RIGHT)
      rbranch = line.chomp.split(" ").last
      result += handle(left, lbranch, right, rbranch)
      branch = nil
      left.clear
      right.clear
    else
      if branch
        branch << line
      else
        result << line
      end
    end
  end
  result.join
end

#resolverObject



16
17
18
# File 'lib/fix_tsv_conflict/repairman.rb', line 16

def resolver
  @resolver ||= Resolver.new(stdin: stdin, stderr: stderr)
end