Class: HashDeepDiff::Delta

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hash_deep_diff/delta.rb

Overview

Representation of the diff of two values examples:

- diff of { a: a } and {} is { a: { left: a, right: HashDeepDiff::NO_VALUE } }
- diff of { a: a } and { a: b } is { a: { left: a, right: b } }
- diff of {} and { a: b } is { a: { left: HashDeepDiff::NO_VALUE, right: b } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(change_key:, value:) ⇒ Delta (private)

Returns a new instance of Delta.

Parameters:

  • change_key (Array)

    list of keys to fetch values we’re comparing

  • value (Hash<(:left, :right), Object>)

    Hash object with two keys - :left and :right, that represents compared original value (at :left) and value we compare to (at :right)



74
75
76
77
# File 'lib/hash_deep_diff/delta.rb', line 74

def initialize(change_key:, value:)
  @value = value
  @change_key = change_key
end

Instance Attribute Details

#change_keyObject (readonly)

Returns the value of attribute change_key.



16
17
18
# File 'lib/hash_deep_diff/delta.rb', line 16

def change_key
  @change_key
end

#valueObject (readonly, private)

Returns the value of attribute value.



69
70
71
# File 'lib/hash_deep_diff/delta.rb', line 69

def value
  @value
end

Instance Method Details

#composite?Bool

true if both valus are Hashes

Returns:

  • (Bool)


36
37
38
# File 'lib/hash_deep_diff/delta.rb', line 36

def composite?
  !simple_left? && !simple_right?
end

#leftObject

Original value



47
48
49
# File 'lib/hash_deep_diff/delta.rb', line 47

def left
  value[:left]
end

#partial?Bool

true if at least one of the values is a Hash

Returns:

  • (Bool)


30
31
32
# File 'lib/hash_deep_diff/delta.rb', line 30

def partial?
  !composite? && !simple?
end

#placeboHashDeepDiff::Delta, NilClass

an indication that nested Hash was deleted/added

Returns:



20
21
22
23
24
25
26
# File 'lib/hash_deep_diff/delta.rb', line 20

def placebo
  return nil unless partial?

  placebo = simple_left? ? { left: NO_VALUE, right: {} } : { left: {}, right: NO_VALUE }

  self.class.new(change_key: change_key, value: placebo)
end

#rightObject

Value we compare to



52
53
54
# File 'lib/hash_deep_diff/delta.rb', line 52

def right
  value[:right]
end

#simple?Bool

true if none of the values is a Hash

Returns:

  • (Bool)


42
43
44
# File 'lib/hash_deep_diff/delta.rb', line 42

def simple?
  simple_left? && simple_right?
end

#simple_left?Bool (private)

Returns true if left value has no nested Hashes

Returns:

  • (Bool)


81
82
83
# File 'lib/hash_deep_diff/delta.rb', line 81

def simple_left?
  !left.respond_to?(:to_hash)
end

#simple_right?Bool (private)

Returns true if right value has no nested Hashes

Returns:

  • (Bool)


87
88
89
# File 'lib/hash_deep_diff/delta.rb', line 87

def simple_right?
  !right.respond_to?(:to_hash)
end

#to_hHash

see #to_hash

Returns:

  • (Hash)


58
59
60
# File 'lib/hash_deep_diff/delta.rb', line 58

def to_h
  to_hash
end

#to_hashHash

Returns:

  • (Hash)


63
64
65
# File 'lib/hash_deep_diff/delta.rb', line 63

def to_hash
  { change_key[-1] => value }
end