Sha256: 37456528fa1a6c96bb0af2c41015f2165cc53c050ef28c8de8ae7951271cff8b

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'ostruct'

module HashDiff
  class Comparison < Struct.new(:left, :right)
  
    def diff
      @side ||= :both
      @diff ||= differences(left, right)
    end

    def left_diff
      @side = :left
      @left_diff ||= differences(left, right)
    end

    def right_diff
      @side = :right
      @right_diff ||= differences(left, right)
    end

    private

    def clone(left, right)
      self.dup.tap do |inst|
        inst.left  = left
        inst.right = right
      end
    end

    def differences(left, right)
      combined_attribute_keys(left, right).reduce({}, &reduction_strategy)
    end

    def reduction_strategy(opts={})
      lambda do |diff, key|
        diff[key] = report(key) if not equal?(key)
        diff
      end
    end

    def combined_attribute_keys(left, right)
      (left.keys + right.keys).uniq
    end

    def equal?(key)
      left.key?(key) && right.key?(key) && left[key] == right[key]
    end

    def hash?(value)
      value.is_a? Hash
    end

    def comparable_key?(key)
      hash?(left[key]) and hash?(right[key])
    end

    def report(key)
      if comparable_key?(key)
        clone(left[key], right[key]).diff
      else
        report_by_side(key)
      end
    end

    def report_by_side(key)
      case @side
      when :left  then right[key]
      when :right then left[key]
      when :both  then [left[key], right[key]]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hash_diff-0.6.0 lib/hash_diff/comparison.rb
hash_diff-0.5.1 lib/hash_diff/comparison.rb