Class: HashDeepDiff::Delta
- Inherits:
-
Object
- Object
- HashDeepDiff::Delta
- 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
-
#change_key ⇒ Object
readonly
Returns the value of attribute change_key.
-
#value ⇒ Object
readonly
private
Returns the value of attribute value.
Instance Method Summary collapse
-
#addition ⇒ Object
added element(s).
-
#array_with_array? ⇒ TrueClass, FalseClass
private
true if both left and right are arrays.
-
#complex? ⇒ TrueClass, FalseClass
true if any value is an
Array
with hashes. -
#complex_left? ⇒ TrueClass, FalseClass
true if left part is an
Array
with hashes. -
#complex_right? ⇒ TrueClass, FalseClass
true if right part is an
Array
with hashes. -
#composite? ⇒ TrueClass, FalseClass
true if both valus are Hashes.
-
#deletion ⇒ Object
removed element(s).
-
#initialize(path:, value:) ⇒ Delta
constructor
private
A new instance of Delta.
-
#left ⇒ Object
Original value.
-
#partial? ⇒ TrueClass, FalseClass
true if at least one of the values is a Hash.
-
#placebo ⇒ Array<HashDeepDiff::Delta>
an indication that nested Hash was deleted/added.
-
#placebo_elment ⇒ Array, Hash
private
an indication of added/removed nested Hash.
-
#right ⇒ Object
Value we compare to.
-
#simple? ⇒ TrueClass, FalseClass
true if none of the values is a Hash.
-
#simple_left? ⇒ TrueClass, FalseClass
private
true if left value has no nested Hashes.
-
#simple_right? ⇒ TrueClass, FalseClass
private
true if right value has no nested Hashes.
-
#to_h ⇒ Hash
see #to_hash.
- #to_hash ⇒ Hash
Constructor Details
#initialize(path:, value:) ⇒ Delta (private)
Returns a new instance of Delta.
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_key ⇒ Object (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 |
#value ⇒ Object (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
#addition ⇒ Object
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
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
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
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
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
52 53 54 |
# File 'lib/hash_deep_diff/delta.rb', line 52 def composite? !simple_left? && !simple_right? end |
#deletion ⇒ Object
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 |
#left ⇒ Object
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
46 47 48 |
# File 'lib/hash_deep_diff/delta.rb', line 46 def partial? !composite? && !simple? && !complex_left? && !complex_right? end |
#placebo ⇒ Array<HashDeepDiff::Delta>
an indication that nested Hash was deleted/added
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_elment ⇒ Array, Hash (private)
an indication of added/removed nested 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 |
#right ⇒ Object
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
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
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
125 126 127 |
# File 'lib/hash_deep_diff/delta.rb', line 125 def simple_right? !right.respond_to?(:to_hash) && !complex_right? end |
#to_h ⇒ Hash
see #to_hash
88 89 90 |
# File 'lib/hash_deep_diff/delta.rb', line 88 def to_h to_hash end |
#to_hash ⇒ Hash
93 94 95 |
# File 'lib/hash_deep_diff/delta.rb', line 93 def to_hash { change_key[-1] => value } end |