class Hashdiff::LcsCompareArrays

@private Used to compare arrays using the lcs algorithm

Public Class Methods

call(obj1, obj2, opts = {}) click to toggle source
# File lib/hashdiff/lcs_compare_arrays.rb, line 8
def call(obj1, obj2, opts = {})
  result = []

  changeset = Hashdiff.diff_array_lcs(obj1, obj2, opts) do |lcs|
    # use a's index for similarity
    lcs.each do |pair|
      prefix = Hashdiff.prefix_append_array_index(opts[:prefix], pair[0], opts)

      result.concat(Hashdiff.diff(obj1[pair[0]], obj2[pair[1]], opts.merge(prefix: prefix)))
    end
  end

  changeset.each do |change|
    next if change[0] != '-' && change[0] != '+'

    change_key = Hashdiff.prefix_append_array_index(opts[:prefix], change[1], opts)

    result << [change[0], change_key, change[2]]
  end

  result
end