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(path:, value:) ⇒ Delta (private)

Returns a new instance of Delta.

Parameters:

  • path (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)



104
105
106
107
# File 'lib/hash_deep_diff/delta.rb', line 104

def initialize(path:, value:)
  @value = value
  @change_key = HashDeepDiff::ChangeKey.new(path: path)
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.



99
100
101
# File 'lib/hash_deep_diff/delta.rb', line 99

def value
  @value
end

Instance Method Details

#additionObject

added element(s)



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

def addition
  return right unless array_with_array?

  return right - left
end

#array_with_array?TrueClass, FalseClass (private)

true if both left and right are arrays

Returns:

  • (TrueClass, FalseClass)


131
132
133
# File 'lib/hash_deep_diff/delta.rb', line 131

def array_with_array?
  left.respond_to?(:to_ary) && right.respond_to?(:to_ary)
end

#complex?TrueClass, FalseClass

true if any value is an Array with hashes

Returns:

  • (TrueClass, FalseClass)


28
29
30
# File 'lib/hash_deep_diff/delta.rb', line 28

def complex?
  complex_left? || complex_right?
end

#complex_left?TrueClass, FalseClass

true if left part is an Array with hashes

Returns:

  • (TrueClass, FalseClass)


40
41
42
# File 'lib/hash_deep_diff/delta.rb', line 40

def complex_left?
  left.respond_to?(:to_ary) && left.any? { |el| el.respond_to?(:to_hash) }
end

#complex_right?TrueClass, FalseClass

true if right part is an Array with hashes

Returns:

  • (TrueClass, FalseClass)


34
35
36
# File 'lib/hash_deep_diff/delta.rb', line 34

def complex_right?
  right.respond_to?(:to_ary) && right.any? { |el| el.respond_to?(:to_hash) }
end

#composite?TrueClass, FalseClass

true if both valus are Hashes

Returns:

  • (TrueClass, FalseClass)


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

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

#deletionObject

removed element(s)



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

def deletion
  return left unless array_with_array?

  return left - right
end

#leftObject

Original value



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

def left
  value[:left]
end

#partial?TrueClass, FalseClass

true if at least one of the values is a Hash

Returns:

  • (TrueClass, FalseClass)


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

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

#placeboArray<HashDeepDiff::Delta>

an indication that nested Hash was deleted/added

Returns:



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

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

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

#placebo_elmentArray, Hash (private)

an indication of added/removed nested Hash

Returns:

  • (Array, Hash)


111
112
113
114
115
# File 'lib/hash_deep_diff/delta.rb', line 111

def placebo_elment
  return [{}] if complex_left? || complex_right?

  return {}
end

#rightObject

Value we compare to



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

def right
  value[:right]
end

#simple?TrueClass, FalseClass

true if none of the values is a Hash

Returns:

  • (TrueClass, FalseClass)


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

def simple?
  simple_left? && simple_right?
end

#simple_left?TrueClass, FalseClass (private)

true if left value has no nested Hashes

Returns:

  • (TrueClass, FalseClass)


119
120
121
# File 'lib/hash_deep_diff/delta.rb', line 119

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

#simple_right?TrueClass, FalseClass (private)

true if right value has no nested Hashes

Returns:

  • (TrueClass, FalseClass)


125
126
127
# File 'lib/hash_deep_diff/delta.rb', line 125

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

#to_hHash

see #to_hash

Returns:

  • (Hash)


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

def to_h
  to_hash
end

#to_hashHash

Returns:

  • (Hash)


93
94
95
# File 'lib/hash_deep_diff/delta.rb', line 93

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