Class: HashDeepDiff::Comparison
- Inherits:
-
Object
- Object
- HashDeepDiff::Comparison
- Defined in:
- lib/hash_deep_diff/comparison.rb
Overview
Representation of the recursive difference between two hashes main parts are
-
path - empty for original hashes, otherwise path to values being compared
-
left - basically left.dig(path), left value of two being compared
-
right - basically right.dig(path), right value of two being compared
Examples:
- { one: :a } compared with { one: :b } does not have nesting so we compare keys and values
- { one: { two: :a, zero: z } } compared with { one: { two: :b, three: :c } } has nesting, so is represented as
- { two: :a } compared with { two: :b, three: :c }, as there is no more nesting we compare keys and values
and have the following comparisons
{ one: { two: :a } } compared to { one: { two: :b } } - value was changed
i.e :a vas replaced with :b on path [:one, :two]
{ one: { zero: z } } compared to NO_VALUE - value was deleted
i.e :z vas replaced with NO_VALUE on path [:one, :zero]
NO_VALUE compared to { one: { three: :c } } compared - value was added
i.e NO_VALUE vas replaced with :c on path [:one, :three]
[
#<HashDeepDiff::Delta
@delta={:two=>{:left=>:a, :right=>:b}},
@prefix=[:one],
@value={:left=>:a, :right=>:b}>,
#<HashDeepDiff::Delta
@delta={:zero=>{:left=>:z, :right=>HashDeepDiff::NO_VALUE}},
@prefix=[:one],
@value={:left=>:z, :right=>HashDeepDiff::NO_VALUE}>,
#<HashDeepDiff::Delta
@delta={:three=>{:left=>HashDeepDiff::NO_VALUE, :right=>:c}},
@prefix=[:one],
@value={:left=>HashDeepDiff::NO_VALUE, :right=>:c}>
]
Instance Attribute Summary collapse
-
#left ⇒ Hash
readonly
Original version of the Hash.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#right ⇒ Hash
readonly
Hash that the original is compared to.
Instance Method Summary collapse
-
#common_keys ⇒ Array
private
All keys from both original and compared objects.
- #comparison ⇒ Array<HashDeepDiff::Delta> private
- #diff ⇒ Array<HashDeepDiff::Delta>
-
#initialize(left, right, prefix = []) ⇒ Comparison
constructor
private
A new instance of Comparison.
- #report ⇒ String
-
#value_left(key) ⇒ Object
private
Original value.
-
#value_right(key) ⇒ Object
private
Value we compare to.
- #values_equal?(key) ⇒ Bool private
Constructor Details
#initialize(left, right, prefix = []) ⇒ Comparison (private)
Returns a new instance of Comparison.
65 66 67 68 69 |
# File 'lib/hash_deep_diff/comparison.rb', line 65 def initialize(left, right, prefix = []) @left = left.to_hash @right = right.to_hash @path = prefix.to_ary end |
Instance Attribute Details
#left ⇒ Hash (readonly)
Returns original version of the Hash.
44 45 46 |
# File 'lib/hash_deep_diff/comparison.rb', line 44 def left @left end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
44 |
# File 'lib/hash_deep_diff/comparison.rb', line 44 attr_reader :left, :right, :path |
#right ⇒ Hash (readonly)
Returns Hash that the original is compared to.
44 |
# File 'lib/hash_deep_diff/comparison.rb', line 44 attr_reader :left, :right, :path |
Instance Method Details
#common_keys ⇒ Array (private)
All keys from both original and compared objects
100 101 102 |
# File 'lib/hash_deep_diff/comparison.rb', line 100 def common_keys (left.keys + right.keys).uniq end |
#comparison ⇒ Array<HashDeepDiff::Delta> (private)
72 73 74 75 76 77 78 |
# File 'lib/hash_deep_diff/comparison.rb', line 72 def comparison common_keys.each_with_object([]) do |key, memo| next if values_equal?(key) memo << Delta.new(path: path + [key], value: { left: value_left(key), right: value_right(key) }) end end |
#diff ⇒ Array<HashDeepDiff::Delta>
52 53 54 55 56 57 58 |
# File 'lib/hash_deep_diff/comparison.rb', line 52 def diff comparison.flat_map do |delta| # if there are nested hashes we need to compare them furter # if no we return difference between values (HashDeepDiff::Delta) delta.complex? ? self.class.new(delta.left, delta.right, delta.path).diff : delta end end |
#report ⇒ String
47 48 49 |
# File 'lib/hash_deep_diff/comparison.rb', line 47 def report diff.join("\n") end |
#value_left(key) ⇒ Object (private)
Original value
88 89 90 |
# File 'lib/hash_deep_diff/comparison.rb', line 88 def value_left(key) left[key] || NO_VALUE end |
#value_right(key) ⇒ Object (private)
Value we compare to
94 95 96 |
# File 'lib/hash_deep_diff/comparison.rb', line 94 def value_right(key) right[key] || NO_VALUE end |
#values_equal?(key) ⇒ Bool (private)
82 83 84 |
# File 'lib/hash_deep_diff/comparison.rb', line 82 def values_equal?(key) value_right(key).instance_of?(value_left(key).class) && (value_right(key) == value_left(key)) end |