Class: NdrImport::Helpers::File::XmlStreaming::Cursor
- Inherits:
-
Object
- Object
- NdrImport::Helpers::File::XmlStreaming::Cursor
- Defined in:
- lib/ndr_import/helpers/file/xml_streaming.rb
Overview
Object to track state as the XML is iterated over, and detect when an element of interest is entered.
Defined Under Namespace
Classes: StackItem
Instance Method Summary collapse
- #enter(node) ⇒ Object
-
#in?(node) ⇒ Boolean
Has this cursor already passed inside a similar node?.
-
#initialize(xpath) ⇒ Cursor
constructor
A new instance of Cursor.
- #leave(_node) ⇒ Object
-
#matches? ⇒ Boolean
Does the element that the cursor is currently on match what is being looked for?.
Constructor Details
#initialize(xpath) ⇒ Cursor
Returns a new instance of Cursor.
36 37 38 39 40 |
# File 'lib/ndr_import/helpers/file/xml_streaming.rb', line 36 def initialize(xpath) @xpath = xpath @stack = [] @match_depth = nil end |
Instance Method Details
#enter(node) ⇒ Object
47 48 49 |
# File 'lib/ndr_import/helpers/file/xml_streaming.rb', line 47 def enter(node) @stack.push StackItem.new(node.name, node.attributes, node.empty_element?) end |
#in?(node) ⇒ Boolean
Has this cursor already passed inside a similar node?
43 44 45 |
# File 'lib/ndr_import/helpers/file/xml_streaming.rb', line 43 def in?(node) @stack.detect { |item| item.name == node.name } end |
#leave(_node) ⇒ Object
51 52 53 54 |
# File 'lib/ndr_import/helpers/file/xml_streaming.rb', line 51 def leave(_node) @stack.pop @match_depth = nil if @match_depth && @stack.length < @match_depth end |
#matches? ⇒ Boolean
Does the element that the cursor is currently on match what is being looked for?
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ndr_import/helpers/file/xml_streaming.rb', line 58 def matches? # Can't match again if we're inside a match already: return false if @matched_depth match = current_stack_match? # "empty element" matches are yielded immediately, without # tagging the stack as having matched, because there won't # be an equivalent closing tag to end the match with later. if in_empty_element? @stack.pop elsif match @match_depth = @stack.length end match end |