Module: Shale::Adapter::Ox

Defined in:
lib/shale/adapter/ox.rb,
lib/shale/adapter/ox/node.rb,
lib/shale/adapter/ox/document.rb

Overview

Ox adapter

Defined Under Namespace

Classes: Document, Node

Class Method Summary collapse

Class Method Details

.create_document(_version = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create Shale::Adapter::Ox::Document instance



64
65
66
# File 'lib/shale/adapter/ox.rb', line 64

def self.create_document(_version = nil)
  Document.new
end

.dump(doc, pretty: false, declaration: false, encoding: false) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Serialize Ox document into XML

Parameters:

  • doc (::Ox::Document, ::Ox::Element)

    Ox document

  • pretty (true, false) (defaults to: false)
  • declaration (true, false, String) (defaults to: false)
  • encoding (true, false, String) (defaults to: false)

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shale/adapter/ox.rb', line 41

def self.dump(doc, pretty: false, declaration: false, encoding: false)
  opts = { indent: -1, with_xml: false }

  if pretty
    opts[:indent] = 2
  end

  if declaration
    doc[:version] = declaration == true ? '1.0' : declaration

    if encoding
      doc[:encoding] = encoding == true ? 'UTF-8' : encoding
    end

    opts[:with_xml] = true
  end

  ::Ox.dump(doc, opts).sub(/\A\n/, '')
end

.load(xml) ⇒ Shale::Adapter::Ox::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse XML into Ox document

Parameters:

  • xml (String)

    XML document

Returns:

Raises:



24
25
26
27
28
29
# File 'lib/shale/adapter/ox.rb', line 24

def self.load(xml)
  element = ::Ox.parse(xml)
  Node.new(element.respond_to?(:root) ? element.root : element)
rescue ::Ox::ParseError => e
  raise ParseError, "Document is invalid: #{e.message}"
end