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



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

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.



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

def elem1
  @elem1
end

#elem2Object (readonly)

Returns the value of attribute elem2.



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

def elem2
  @elem2
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#pos1Object (readonly)

Returns the value of attribute pos1.



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

def pos1
  @pos1
end

#pos2Object (readonly)

Returns the value of attribute pos2.



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

def pos2
  @pos2
end

#statusObject (readonly)

Returns the value of attribute status.



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

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



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

def deleted?
  status == :deleted
end

#inserted?Boolean

Returns true if element only in second tree.

Returns:

  • (Boolean)

    true if element only in second tree



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

def inserted?
  status == :inserted
end

#matched?Boolean

Returns true if element found in both trees.

Returns:

  • (Boolean)

    true if element found in both trees



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

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



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

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