class HashDiff::Comparison
Attributes
left[R]
right[R]
Public Class Methods
new(left, right)
click to toggle source
# File lib/hash_diff/comparison.rb, line 3 def initialize(left, right) @left = left @right = right end
Public Instance Methods
diff()
click to toggle source
# File lib/hash_diff/comparison.rb, line 10 def diff @diff ||= find_differences { |l, r| [l, r] } end
left_diff()
click to toggle source
# File lib/hash_diff/comparison.rb, line 14 def left_diff @left_diff ||= find_differences { |_, r| r } end
right_diff()
click to toggle source
# File lib/hash_diff/comparison.rb, line 18 def right_diff @right_diff ||= find_differences { |l, _| l } end
Protected Instance Methods
find_differences(&reporter)
click to toggle source
# File lib/hash_diff/comparison.rb, line 24 def find_differences(&reporter) combined_keys.each_with_object({ }, &comparison_strategy(reporter)) end
Private Instance Methods
combined_keys()
click to toggle source
# File lib/hash_diff/comparison.rb, line 36 def combined_keys (left.keys + right.keys).uniq end
comparable?(key)
click to toggle source
# File lib/hash_diff/comparison.rb, line 48 def comparable?(key) hash?(left[key]) && hash?(right[key]) end
comparison_strategy(reporter)
click to toggle source
# File lib/hash_diff/comparison.rb, line 30 def comparison_strategy(reporter) lambda do |key, diff| diff[key] = report_difference(key, reporter) unless equal?(key) end end
equal?(key)
click to toggle source
# File lib/hash_diff/comparison.rb, line 40 def equal?(key) value_with_default(left, key) == value_with_default(right, key) end
hash?(value)
click to toggle source
# File lib/hash_diff/comparison.rb, line 44 def hash?(value) value.is_a?(Hash) end
report_difference(key, reporter)
click to toggle source
# File lib/hash_diff/comparison.rb, line 52 def report_difference(key, reporter) if comparable?(key) self.class.new(left[key], right[key]).find_differences(&reporter) else reporter.call( value_with_default(left, key), value_with_default(right, key) ) end end
value_with_default(obj, key)
click to toggle source
# File lib/hash_diff/comparison.rb, line 63 def value_with_default(obj, key) obj.fetch(key, NO_VALUE) end