Class: Canon::Xml::ElementMatcher::MatchResult

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/xml/element_matcher.rb

Overview

Represents the result of matching an element across two DOM trees

A MatchResult indicates whether an element was found in both trees (matched), only in the first tree (deleted), or only in the second tree (inserted).

Attributes

  • status: Symbol indicating match type (:matched, :deleted, :inserted)

  • elem1: Element from first tree (nil if inserted)

  • elem2: Element from second tree (nil if deleted)

  • path: Array of element names showing location in tree

  • pos1: Integer index of elem1 in its parent’s children (nil if inserted)

  • pos2: Integer index of elem2 in its parent’s children (nil if deleted)

Position Change Detection

When status is :matched and pos1 ≠ pos2, the element has moved positions. This is tracked as a semantic difference via the :element_position dimension.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, elem1:, elem2:, path:, pos1: nil, pos2: nil) ⇒ MatchResult

Returns a new instance of MatchResult.

Parameters:

  • status (Symbol)

    Match status (:matched, :deleted, :inserted)

  • elem1 (Object, nil)

    Element from first tree

  • elem2 (Object, nil)

    Element from second tree

  • path (Array<String>)

    Element path in tree

  • pos1 (Integer, nil) (defaults to: nil)

    Position index in first tree

  • pos2 (Integer, nil) (defaults to: nil)

    Position index in second tree



87
88
89
90
91
92
93
94
# File 'lib/canon/xml/element_matcher.rb', line 87

def initialize(status:, elem1:, elem2:, path:, pos1: nil, pos2: nil)
  @status = status
  @elem1 = elem1
  @elem2 = elem2
  @path = path
  @pos1 = pos1
  @pos2 = pos2
end

Instance Attribute Details

#elem1Object (readonly)

Returns the value of attribute elem1.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def elem1
  @elem1
end

#elem2Object (readonly)

Returns the value of attribute elem2.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def elem2
  @elem2
end

#pathObject (readonly)

Returns the value of attribute path.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def path
  @path
end

#pos1Object (readonly)

Returns the value of attribute pos1.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def pos1
  @pos1
end

#pos2Object (readonly)

Returns the value of attribute pos2.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def pos2
  @pos2
end

#statusObject (readonly)

Returns the value of attribute status.



79
80
81
# File 'lib/canon/xml/element_matcher.rb', line 79

def status
  @status
end

Instance Method Details

#deleted?Boolean

Returns true if element only in first tree.

Returns:

  • (Boolean)

    true if element only in first tree



107
108
109
# File 'lib/canon/xml/element_matcher.rb', line 107

def deleted?
  status == :deleted
end

#inserted?Boolean

Returns true if element only in second tree.

Returns:

  • (Boolean)

    true if element only in second tree



102
103
104
# File 'lib/canon/xml/element_matcher.rb', line 102

def inserted?
  status == :inserted
end

#matched?Boolean

Returns true if element found in both trees.

Returns:

  • (Boolean)

    true if element found in both trees



97
98
99
# File 'lib/canon/xml/element_matcher.rb', line 97

def matched?
  status == :matched
end

#position_changed?Boolean

Returns true if element moved to different position.

Returns:

  • (Boolean)

    true if element moved to different position



112
113
114
# File 'lib/canon/xml/element_matcher.rb', line 112

def position_changed?
  matched? && pos1 && pos2 && pos1 != pos2
end