Class: JsonDeepCompare::NodeComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/json-deep-compare/node_comparison.rb

Defined Under Namespace

Classes: Difference

Constant Summary collapse

ExcerptPadding =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @lval, @rval, @selector, @options = lval, rval, selector, options
  @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}", 
          options
        )
      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})",
          options
        )
      end
    end
  end
end

Instance Attribute Details

#lvalObject (readonly)

Returns the value of attribute lval.



4
5
6
# File 'lib/json-deep-compare/node_comparison.rb', line 4

def lval
  @lval
end

#rvalObject (readonly)

Returns the value of attribute rval.



4
5
6
# File 'lib/json-deep-compare/node_comparison.rb', line 4

def rval
  @rval
end

#selectorObject (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

Returns:

  • (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

Returns:

  • (Boolean)


38
39
40
# File 'lib/json-deep-compare/node_comparison.rb', line 38

def blank_equality?
  @options[:blank_equality]
end

#differencesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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_for_equality), 
          rval: value_inspect(rval_for_equality)
        )]
      end
    else
      @children.map(&:differences).compact.flatten
    end
  end
end

#equal?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/json-deep-compare/node_comparison.rb', line 62

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_procObject



78
79
80
# File 'lib/json-deep-compare/node_comparison.rb', line 78

def equality_proc
  @options[:equality]
end

#excerptable_difference?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/json-deep-compare/node_comparison.rb', line 82

def excerptable_difference?
  @lval.is_a?(String) and @rval.is_a?(String) && (
    @lval.size > ExcerptPadding * 2 || @rval.size > ExcerptPadding * 2
  )
end

#excerpted_differenceObject



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
113
# File 'lib/json-deep-compare/node_comparison.rb', line 88

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

#has_substitution?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/json-deep-compare/node_comparison.rb', line 115

def has_substitution?
  @options[:substitutions] && @options[:substitutions].has_key?(selector)
end

#leaf?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/json-deep-compare/node_comparison.rb', line 119

def leaf?
  @children.empty?
end

#left_to_right?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/json-deep-compare/node_comparison.rb', line 123

def left_to_right?
  @options[:direction] == :left
end

#lval_for_equalityObject



127
128
129
130
131
132
133
134
135
# File 'lib/json-deep-compare/node_comparison.rb', line 127

def lval_for_equality
  @_lval_for_equality ||= begin
    if left_to_right? && has_substitution?
      substitution
    else
      lval
    end
  end
end

#right_to_left?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/json-deep-compare/node_comparison.rb', line 137

def right_to_left?
  @options[:direction] == :right
end

#rval_for_equalityObject



141
142
143
144
145
146
147
148
149
# File 'lib/json-deep-compare/node_comparison.rb', line 141

def rval_for_equality
  @_rval_for_equality ||= begin
    if right_to_left? && has_substitution?
      substitution
    else
      rval
    end
  end
end

#selector_excluded?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
# File 'lib/json-deep-compare/node_comparison.rb', line 151

def selector_excluded?
  @options[:exclusions].any? { |exclusion|
    if exclusion.is_a?(String)
      exclusion == @selector
    else
      @selector =~ exclusion
    end
  }
end

#substitutionObject



161
162
163
# File 'lib/json-deep-compare/node_comparison.rb', line 161

def substitution
  @options[:substitutions][selector]
end

#value_inspect(value) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/json-deep-compare/node_comparison.rb', line 165

def value_inspect(value)
  str = value.inspect
  if str.length >= 40
    "#{value.class.name} #{str[0..37]}..."
  else
    str
  end
end