Class: Serialbench::Serializers::Xml::LeptrisSerializer

Inherits:
BaseXmlSerializer show all
Defined in:
lib/serialbench/serializers/xml/leptris_serializer.rb

Defined Under Namespace

Classes: StreamingHandler

Instance Method Summary collapse

Methods inherited from BaseXmlSerializer

#features, format, #generate_xml, #parse_dom, #parse_sax

Methods inherited from BaseSerializer

#get_version, #initialize, #require_library, #stream_parse, #supports?

Constructor Details

This class inherits a constructor from Serialbench::Serializers::BaseSerializer

Instance Method Details

#available?Boolean

The gem's require succeeds even when the libtaurus shared library is missing -- the FFI load happens lazily at first use. Probe with a real parse so "available" means "actually usable".

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 70

def available?
  return @available if defined?(@available)

  @available = begin
    require 'leptris'
  require 'leptris/xml'
    Leptris::XML.parse('<probe/>')
    true
  rescue StandardError, LoadError
    false
  end
end

#capabilitiesObject



39
40
41
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 39

def capabilities
  super | Set.new(%i[xpath sax stax])
end

#generate(data) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 19

def generate(data)
  require 'leptris'
  require 'leptris/xml'
  if data.is_a?(Leptris::XML::Document) || data.is_a?(Leptris::XML::Node)
    data.to_xml(indent: 2)
  else
    build_document_from_data(data).to_xml(indent: 2)
  end
end

#library_require_nameObject



63
64
65
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 63

def library_require_name
  'leptris'
end

#nameObject



9
10
11
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 9

def name
  'leptris'
end

#parse(xml_string) ⇒ Object



13
14
15
16
17
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 13

def parse(xml_string)
  require 'leptris'
  require 'leptris/xml'
  Leptris::XML.parse(xml_string)
end

#parse_streaming(xml_string, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 29

def parse_streaming(xml_string, &block)
  require 'leptris'
  require 'leptris/xml'

  handler = StreamingHandler.new(&block)
  parser = Leptris::XML::SAX::Parser.new(handler)
  parser.parse_memory(xml_string)
  handler.elements_processed
end

#versionObject



55
56
57
58
59
60
61
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 55

def version
  return 'unknown' unless available?

  require 'leptris'
  require 'leptris/xml'
  Leptris::VERSION
end

#xpath_query(document, expression) ⇒ Object



43
44
45
46
47
48
# File 'lib/serialbench/serializers/xml/leptris_serializer.rb', line 43

def xpath_query(document, expression)
  require 'leptris'
  require 'leptris/xml'
  result = document.xpath(expression)
  result.size
end