Class: Leptris::XML::Pull::Parser
- Inherits:
-
Object
- Object
- Leptris::XML::Pull::Parser
- Defined in:
- lib/leptris/xml/pull.rb
Class Method Summary collapse
Instance Method Summary collapse
- #each ⇒ Object
-
#free ⇒ Object
Releases the C parser.
-
#initialize(handle) ⇒ Parser
constructor
A new instance of Parser.
-
#next_event ⇒ Object
Advances the cursor and returns the next Event, or nil after the end of the document.
Constructor Details
#initialize(handle) ⇒ Parser
Returns a new instance of Parser.
40 41 42 43 |
# File 'lib/leptris/xml/pull.rb', line 40 def initialize(handle) raise Leptris::XML::ParseError, "leptris_pull_new failed" if handle.null? @handle = handle end |
Class Method Details
Instance Method Details
#each ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/leptris/xml/pull.rb', line 52 def each return enum_for(:each) unless block_given? loop do event = next_event break if event.nil? yield event break if event.type == :end_document || event.type == :error end self end |
#free ⇒ Object
Releases the C parser. Safe to call twice.
46 47 48 49 50 |
# File 'lib/leptris/xml/pull.rb', line 46 def free return if @handle.nil? Leptris::XML::FFI.leptris_pull_free(@handle) @handle = nil end |
#next_event ⇒ Object
Advances the cursor and returns the next Event, or nil after the end of the document. Attribute values are captured during start_element because the C accessors are valid only for the most recently returned event.
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/leptris/xml/pull.rb', line 67 def next_event raw = Leptris::XML::FFI.leptris_pull_next(@handle) return nil if raw.null? event = Leptris::XML::FFI::PullEventStruct.new(raw) type = TYPES[event[:type]] name = read_owned(event[:name]) text = read_owned(event[:text]) attrs = type == :start_element ? capture_attrs : nil Event.new(type: type, name: name, text: text, attrs: attrs) end |