Module: PgXML

Defined in:
lib/pg-xml/pg_xml.rb

Overview

note: none of this is tested against Hpricot the goal was to allow either hpricot or nokogiri, but support for “using either” is half-assed at best

Constant Summary collapse

XML_PARSER_TYPE =

complex line of code determining whether we have access to nokogiri or hpricot.

( require('nokogiri') || true rescue          # evaluates to true if we have nokogiri
  require('hpricot') && false rescue          # evaluates to valse if we have hpricot
  raise LoadError, "Either nokogiri or hpricot is required" # raises an error if we have neither
) ? :nokogiri : :hpricot

Class Method Summary collapse

Class Method Details

.defaultObject

Valid xml requires a root node.



27
28
29
# File 'lib/pg-xml/pg_xml.rb', line 27

def self.default
  parse '<xml></xml>'
end

.dump(xml_document) ⇒ Object



22
23
24
# File 'lib/pg-xml/pg_xml.rb', line 22

def self.dump xml_document
  xml_document.to_s
end

.load(xml_str) ⇒ Object



18
19
20
# File 'lib/pg-xml/pg_xml.rb', line 18

def self.load xml_str
  parse xml_str
end

.parse(xml_str) ⇒ Object

use our parser to create an XML document warning: nokogiri is friendly – if you feed it invalid XML it will fail silently Nokogiri::XML.parse(‘<xml><xml>’).to_s ~> <xml><xml/></xml>



34
35
36
# File 'lib/pg-xml/pg_xml.rb', line 34

def self.parse xml_str
  XML_PARSER_TYPE == :nokogiri ? Nokogiri::XML.parse(xml_str) : Hpricot(xml_str)
end

.parser_typeObject

get which XML parser we are using



14
15
16
# File 'lib/pg-xml/pg_xml.rb', line 14

def self.parser_type
  XML_PARSER_TYPE
end