Sha256: 1ea1eb037d7ea4ae343f9b8ad2e521240a8b753430e61c835fd6b98a67033f69

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require_relative 'acts_as_delta'

module HashDeepDiff
  module Delta
    # Representation of the pure left diff
    # i.e element that are missing in the hash on the right of the comparison
    # for example left diff of { a: a } and {} is { a: a }
    class Inner
      include Delta::ActsAsDelta

      def to_str
        if @delta.values.first.respond_to?(:to_hash)
          if @delta.values.first.keys == %i[left right]
            if complex?
              HashDeepDiff::Comparison.new(left, right, path).report
            else
              lines = <<~Q
                -left#{diff_prefix} = #{left}
                +right#{diff_prefix} = #{right}
              Q
              lines.strip
            end
          else
            @delta.values.first.keys.map do |key|
              self.class.new(path: path + [key], value: @delta.values.first[key])
            end.join("\n").strip
          end
        else
          lines = <<~Q
            -left#{diff_prefix} = #{left}
            +right#{diff_prefix} = #{right}
          Q
          lines.strip
        end
      end

      def complex?
        left.respond_to?(:to_hash) && right.respond_to?(:to_hash)
      end

      def left
        @delta.values.first[:left]
      end

      def right
        @delta.values.first[:right]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_deep_diff-0.3.2 lib/hash_deep_diff/delta/inner.rb