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
-
#composite? ⇒ Bool
true if both valus are Hashes.
-
#initialize(change_key:, value:) ⇒ Delta
constructor
private
A new instance of Delta.
-
#left ⇒ Object
Original value.
-
#partial? ⇒ Bool
true if at least one of the values is a Hash.
-
#placebo ⇒ HashDeepDiff::Delta, NilClass
an indication that nested Hash was deleted/added.
-
#right ⇒ Object
Value we compare to.
-
#simple? ⇒ Bool
true if none of the values is a Hash.
-
#simple_left? ⇒ Bool
private
Returns true if left value has no nested Hashes.
-
#simple_right? ⇒ Bool
private
Returns true if right value has no nested Hashes.
-
#to_h ⇒ Hash
see #to_hash.
- #to_hash ⇒ Hash
Constructor Details
#initialize(change_key:, value:) ⇒ Delta (private)
Returns a new instance of Delta.
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_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.
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
36 37 38 |
# File 'lib/hash_deep_diff/delta.rb', line 36 def composite? !simple_left? && !simple_right? end |
#left ⇒ Object
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
30 31 32 |
# File 'lib/hash_deep_diff/delta.rb', line 30 def partial? !composite? && !simple? end |
#placebo ⇒ HashDeepDiff::Delta, NilClass
an indication that nested Hash was deleted/added
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 |
#right ⇒ Object
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
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
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
87 88 89 |
# File 'lib/hash_deep_diff/delta.rb', line 87 def simple_right? !right.respond_to?(:to_hash) end |
#to_h ⇒ Hash
see #to_hash
58 59 60 |
# File 'lib/hash_deep_diff/delta.rb', line 58 def to_h to_hash end |
#to_hash ⇒ Hash
63 64 65 |
# File 'lib/hash_deep_diff/delta.rb', line 63 def to_hash { change_key[-1] => value } end |