Sha256: 79768e46a012efdea5a798e850f9d972a81ca9b427436158255c17ef8a4fc13d

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

require "fix_tsv_conflict/conflict"
require "fix_tsv_conflict/logging"
require "fix_tsv_conflict/resolver"

module FixTSVConflict
  class Repairman
    include Logging

    attr_reader :stdin, :stderr

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

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

    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

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

    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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fix_tsv_conflict-0.3.0 lib/fix_tsv_conflict/repairman.rb
fix_tsv_conflict-0.2.1 lib/fix_tsv_conflict/repairman.rb
fix_tsv_conflict-0.2.0 lib/fix_tsv_conflict/repairman.rb