Sha256: b5d10c3c9be95da2b42d1009957fbb964f1a9acc4dd9f28e3ec911649120ff7f

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

module HashDeepDiff
  # Visual representation of the difference between two values
  class Report
    # We have two cases
    #   * added - when value on the left is missing
    #   * deleted - when the value on the right is missing
    module Mode
      # for additions
      ADDITION = '+left'
      # for deletions
      DELETION = '-left'
    end

    # A report with all additions and deletions
    # @return [String]
    def to_str
      if @value.respond_to?(:to_hash) && !@value.empty?
        [@mode, diff_prefix, ' = ', "{}\n"].join +
          @value.keys.map do |key|
            Report.new(path: @path + [key], value: @value[key], mode: @mode)
          end.join("\n")
      else
        [@mode, diff_prefix, ' = ', @value.to_s].join
      end
    end

    # A report with all additions and deletions
    # @return [String]
    def to_s
      to_str
    end

    private

    # @param [Array] path Keys from compared objects to fetch the compared values
    # @param [Object] value value from a  compared object at +@path+
    # @param [Mode::ADDITION, Mode::DELETION] mode
    def initialize(path:, value:, mode: Mode::ADDITION)
      @path = path.to_ary
      @value = value
      @mode = mode
    end

    # Visual representation of keys from compared objects needed to fetch the compared values
    # @return [String]
    def diff_prefix
      # TOFIX poor naming
      @path.map { |key| "[#{key}]" }.join
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hash_deep_diff-0.5.0 lib/hash_deep_diff/report.rb