Sha256: 045935592af9df1c9e2cf05a4d56dac397a1fc7c5bec58654e496dc325040140

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module DynportTools
  class Differ
    attr_accessor :diff_all, :use_return, :symbolize_keys
  
  
    def initialize(options = {})
      self.diff_all = options[:diff_all] != false
      self.use_return = options[:use_return] == true
    end
  
    def diff(a, b)
      if both?(a, b, Hash)
        diff_hash_values(a, b, a.keys + (self.diff_all ? b.keys : []))
      elsif both?(a, b, Array)
        diff_hash_values(a, b, all_array_indexes(a, b))
      else
        [a, b] if a != b
      end
    end
  
    def diff_to_message_lines(the_diff, prefix = nil)
      if the_diff.is_a?(Array)
        ["expected #{expected_value(the_diff.first)} to be #{expected_value(the_diff.at(1))}"]
      elsif the_diff.is_a?(Hash)
        the_diff.map do |key, diff|
          if diff.is_a?(Array)
            "expected #{merge_prefixes(prefix, key)} to #{use_return ? "return" : "be"} #{expected_value(diff.first)} but #{use_return ? "did" : "was"} #{expected_value(diff.at(1))}"
          else
            diff_to_message_lines(diff, merge_prefixes(prefix, key))
          end
        end.flatten
      else
        []
      end
    end
  
  private
    def expected_value(value)
      "<#{value.inspect}>"
    end
  
    def all_array_indexes(a, b)
      0.upto([a.length, b.length].max - 1).to_a
    end
  
    def diff_hash_values(a, b, keys)
      ret = keys.uniq.inject({}) do |hash, key|
        value_a = a[key]
        value_b = b[key]
        if symbolize_keys
          value_a ||= a[key.is_a?(Symbol) ? key.to_s : key.to_sym]
          value_b ||= b[key.is_a?(Symbol) ? key.to_s : key.to_sym]
        end
        if diff = diff(value_a, value_b)
          hash[key] = diff
        end
        hash
      end
      ret.empty? ? nil : ret
    end
  
    def both?(a, b, clazz)
      a.is_a?(clazz) && b.is_a?(clazz)
    end
  
    def merge_prefixes(prefix, key)
      key_s = key.is_a?(Hash) ? key.inspect : key
      prefix ? "#{prefix}[#{key_s}]" : key_s
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
dynport_tools-0.2.10 lib/dynport_tools/differ.rb
dynport_tools-0.2.9 lib/dynport_tools/differ.rb
dynport_tools-0.2.8 lib/dynport_tools/differ.rb
dynport_tools-0.2.6 lib/dynport_tools/differ.rb