Sha256: 06e7783e7b3041ca9e6a55412e3b6cae1566aa0aa5015973dd849ae0ca818693

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

module Mutant
  # Class to create diffs from source code
  class Differ
    include Adamantium::Flat

    # Return source diff
    #
    # @return [String]
    #
    # @api private
    #
    def diff
      output = ''
      @diffs.each do |piece|
        hunk = Diff::LCS::Hunk.new(@old, @new, piece, CONTEXT_LINES, length_difference)
        output << hunk.diff(FORMAT)
        output << "\n"
      end
      output
    end
    memoize :diff

    # Return colorized source diff
    #
    # @return [String]
    #
    # @api private
    #
    def colorized_diff
      diff.lines.map do |line|
        self.class.colorize_line(line)
      end.join
    end
    memoize :colorized_diff

  private

    FORMAT = :unified
    CONTEXT_LINES = 3

    # Initialize differ object
    #
    # @param [String] old
    # @param [String] new
    #
    # @return [undefined]
    #
    # @api private
    #
    def initialize(old, new)
      @old, @new = lines(old), lines(new)
      @diffs = Diff::LCS.diff(@old, @new)
    end

    # Break up sorce into lines
    #
    # @param [String] source
    #
    # @return [Array<String>]
    #
    # @api private
    #
    def lines(source)
      self.class.lines(source)
    end

    # Return length difference
    #
    # @return [Fixnum]
    #
    # @api private
    #
    def length_difference
      @new.size - @old.size
    end

    # Break up source into lines
    #
    # @param [String] source
    #
    # @return [Array<String>]
    #
    # @api private
    #
    def self.lines(source)
      source.lines.map { |line| line.chomp }
    end

    # Return colorized diff line
    #
    # @param [String] line
    #
    # @return [String]
    #   returns colorized line
    #
    # @api private
    #
    def self.colorize_line(line)
      case line[0].chr
      when '+'
        Color::GREEN
      when '-'
        Color::RED
      when '@'
        line[1].chr == '@' ? Color::BLUE : Color::NONE
      else
        Color::NONE
      end.format(line)
    end

  end # Differ
end # Mutant

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutant-0.3.0.beta4 lib/mutant/differ.rb
mutant-0.3.0.beta3 lib/mutant/differ.rb
mutant-0.3.0.beta2 lib/mutant/differ.rb