Class: JsonDeepCompare::NodeComparison
- Inherits:
-
Object
- Object
- JsonDeepCompare::NodeComparison
- Defined in:
- lib/json-deep-compare/node_comparison.rb
Defined Under Namespace
Classes: Difference
Constant Summary collapse
- ExcerptPadding =
15
Instance Attribute Summary collapse
-
#lval ⇒ Object
readonly
Returns the value of attribute lval.
-
#rval ⇒ Object
readonly
Returns the value of attribute rval.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
- #blank?(value) ⇒ Boolean
- #blank_equality? ⇒ Boolean
- #differences ⇒ Object
- #equal? ⇒ Boolean
- #equality_proc ⇒ Object
- #excerptable_difference? ⇒ Boolean
- #excerpted_difference ⇒ Object
-
#initialize(lval, rval, selector, options = {}) ⇒ NodeComparison
constructor
A new instance of NodeComparison.
- #leaf? ⇒ Boolean
- #left_to_right? ⇒ Boolean
- #lval_for_equality ⇒ Object
- #right_to_left? ⇒ Boolean
- #rval_for_equality ⇒ Object
- #selector_excluded? ⇒ Boolean
- #substitution ⇒ Object
- #value_inspect(value) ⇒ Object
Constructor Details
#initialize(lval, rval, selector, options = {}) ⇒ NodeComparison
Returns a new instance of NodeComparison.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/json-deep-compare/node_comparison.rb', line 6 def initialize(lval, rval, selector, = {}) @lval, @rval, @selector, @options = lval, rval, selector, @children = [] if lval.is_a?(Hash) if rval.is_a?(Hash) lval.each do |key, left_sub_value| @children << NodeComparison.new( left_sub_value, rval[key], "#{selector} > .#{key}", ) end end elsif lval.is_a?(Array) if rval.is_a?(Array) lval.each_with_index do |left_sub_value, i| @children << NodeComparison.new( left_sub_value, rval[i], "#{selector} :nth-child(#{i+1})", ) end end end end |
Instance Attribute Details
#lval ⇒ Object (readonly)
Returns the value of attribute lval.
4 5 6 |
# File 'lib/json-deep-compare/node_comparison.rb', line 4 def lval @lval end |
#rval ⇒ Object (readonly)
Returns the value of attribute rval.
4 5 6 |
# File 'lib/json-deep-compare/node_comparison.rb', line 4 def rval @rval end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
4 5 6 |
# File 'lib/json-deep-compare/node_comparison.rb', line 4 def selector @selector end |
Instance Method Details
#blank?(value) ⇒ Boolean
34 35 36 |
# File 'lib/json-deep-compare/node_comparison.rb', line 34 def blank?(value) value.respond_to?(:empty?) ? value.empty? : !value end |
#blank_equality? ⇒ Boolean
38 39 40 |
# File 'lib/json-deep-compare/node_comparison.rb', line 38 def blank_equality? @options[:blank_equality] end |
#differences ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/json-deep-compare/node_comparison.rb', line 42 def differences if equal? [] else if leaf? if excerptable_difference? [excerpted_difference] else [Difference.new( @selector, "expected to be :lval but was :rval", lval: value_inspect(@lval), rval: value_inspect(@rval) )] end else @children.map(&:differences).compact.flatten end end end |
#equal? ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/json-deep-compare/node_comparison.rb', line 61 def equal? if leaf? if selector_excluded? true elsif equality_proc equality_proc.call(lval_for_equality, rval_for_equality) elsif blank_equality? && blank?(lval_for_equality) && blank?(rval_for_equality) true else lval_for_equality == rval_for_equality end else @children.all?(&:equal?) end end |
#equality_proc ⇒ Object
77 78 79 |
# File 'lib/json-deep-compare/node_comparison.rb', line 77 def equality_proc @options[:equality] end |
#excerptable_difference? ⇒ Boolean
81 82 83 84 85 |
# File 'lib/json-deep-compare/node_comparison.rb', line 81 def excerptable_difference? @lval.is_a?(String) and @rval.is_a?(String) && ( @lval.size > ExcerptPadding * 2 || @rval.size > ExcerptPadding * 2 ) end |
#excerpted_difference ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/json-deep-compare/node_comparison.rb', line 87 def excerpted_difference difference_start = (0..@lval.length).detect { |i| @lval[i] != @rval[i] } range_start = if difference_start > ExcerptPadding difference_start - ExcerptPadding else 0 end left_excerpt = @lval[range_start..difference_start+ExcerptPadding] right_excerpt = @rval[range_start..difference_start+ExcerptPadding] if difference_start - ExcerptPadding > 0 left_excerpt = "..." + left_excerpt right_excerpt = "..." + right_excerpt end if difference_start + ExcerptPadding < @lval.length left_excerpt = left_excerpt + '...' end if difference_start + ExcerptPadding < @rval.length right_excerpt = right_excerpt + '...' end Difference.new( @selector, "differs starting at char :difference_start: :lval differs from :rval", difference_start: difference_start.to_s, lval: left_excerpt.inspect, rval: right_excerpt.inspect ) end |
#leaf? ⇒ Boolean
114 115 116 |
# File 'lib/json-deep-compare/node_comparison.rb', line 114 def leaf? @children.empty? end |
#left_to_right? ⇒ Boolean
118 119 120 |
# File 'lib/json-deep-compare/node_comparison.rb', line 118 def left_to_right? @options[:direction] == :left end |
#lval_for_equality ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/json-deep-compare/node_comparison.rb', line 122 def lval_for_equality @_lval_for_equality ||= begin if left_to_right? && substitution substitution else lval end end end |
#right_to_left? ⇒ Boolean
132 133 134 |
# File 'lib/json-deep-compare/node_comparison.rb', line 132 def right_to_left? @options[:direction] == :right end |
#rval_for_equality ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/json-deep-compare/node_comparison.rb', line 136 def rval_for_equality @_rval_for_equality ||= begin if right_to_left? && substitution substitution else rval end end end |
#selector_excluded? ⇒ Boolean
146 147 148 149 150 151 152 153 154 |
# File 'lib/json-deep-compare/node_comparison.rb', line 146 def selector_excluded? @options[:exclusions].any? { |exclusion| if exclusion.is_a?(String) exclusion == @selector else @selector =~ exclusion end } end |
#substitution ⇒ Object
156 157 158 |
# File 'lib/json-deep-compare/node_comparison.rb', line 156 def substitution @options[:substitutions] && @options[:substitutions][selector] end |
#value_inspect(value) ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'lib/json-deep-compare/node_comparison.rb', line 160 def value_inspect(value) str = value.inspect if str.length >= 40 "#{value.class.name} #{str[0..37]}..." else str end end |