class SuperDiff::OperationTreeBuilders::MultilineString

Attributes

original_actual[R]
original_expected[R]
sequence_matcher[R]

Public Class Methods

applies_to?(expected, actual) click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 4
def self.applies_to?(expected, actual)
  expected.is_a?(::String) && actual.is_a?(::String) &&
    (expected.include?("\n") || actual.include?("\n"))
end
new(*args) click to toggle source
Calls superclass method
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 9
def initialize(*args)
  super(*args)

  @original_expected = @expected
  @original_actual = @actual
  @expected = split_into_lines(@expected)
  @actual = split_into_lines(@actual)
  @sequence_matcher = PatienceDiff::SequenceMatcher.new
end

Protected Instance Methods

build_operation_tree() click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 33
def build_operation_tree
  OperationTrees::MultilineString.new([])
end
unary_operations() click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 21
def unary_operations
  opcodes.flat_map do |code, a_start, a_end, b_start, b_end|
    if code == :delete
      add_delete_operations(a_start..a_end)
    elsif code == :insert
      add_insert_operations(b_start..b_end)
    else
      add_noop_operations(b_start..b_end)
    end
  end
end

Private Instance Methods

add_delete_operations(indices) click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 49
def add_delete_operations(indices)
  indices.map do |index|
    Operations::UnaryOperation.new(
      name: :delete,
      collection: expected,
      key: index,
      index: index,
      value: expected[index],
    )
  end
end
add_insert_operations(indices) click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 61
def add_insert_operations(indices)
  indices.map do |index|
    Operations::UnaryOperation.new(
      name: :insert,
      collection: actual,
      key: index,
      index: index,
      value: actual[index],
    )
  end
end
add_noop_operations(indices) click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 73
def add_noop_operations(indices)
  indices.map do |index|
    Operations::UnaryOperation.new(
      name: :noop,
      collection: actual,
      key: index,
      index: index,
      value: actual[index],
    )
  end
end
opcodes() click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 45
def opcodes
  sequence_matcher.diff_opcodes(expected, actual)
end
split_into_lines(string) click to toggle source
# File lib/super_diff/operation_tree_builders/multiline_string.rb, line 41
def split_into_lines(string)
  string.scan(/.+(?:\r|\n|\r\n|\Z)/)
end