class Hash::Diff

Constants

VERSION

Attributes

new[R]
old[R]

Public Class Methods

new(old:, new:) click to toggle source

@note Don't care given hash might be changed in future, to keep lite cost

# File lib/hash/diff.rb, line 19
def initialize(old:, new:)
  @old, @new = old.to_hash, new.to_hash
end

Public Instance Methods

appended() click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 29
def appended
  @appended ||= extract_keys(updated, @old)
end
deleted() click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 24
def deleted
  @deleted ||= extract_keys(updated, @new)
end
inspect() click to toggle source

@return [String]

# File lib/hash/diff.rb, line 58
def inspect
  "old: #{@old.inspect} / new: #{@new.inspect}"
end
kept() click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 53
def kept
  @kept ||= extract_keys(@new, updated)
end
updated() click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 34
def updated
  @updated ||= (
    old_pairs = @old.to_a
    new_pairs = @new.to_a
    ((old_pairs - new_pairs) | (new_pairs - old_pairs)).to_h
  )
end
value_updated() click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 43
def value_updated
  @value_updated ||= (
    extract_keys(
      extract_keys(updated, deleted),
      appended
    )
  )
end

Private Instance Methods

extract_keys(base, extracting) click to toggle source

@return [Hash]

# File lib/hash/diff.rb, line 65
def extract_keys(base, extracting)
  # We can drop this condition after dropped to support Ruby 2.7.x in future...
  if base.respond_to?(:except)
    base.except(*extracting.keys)
  else
    ret = base.dup
    extracting.each_key do |key|
      ret.delete key
    end
    ret
  end
end