Class: Shale::Adapter::Nokogiri::Document Private

Inherits:
Object
  • Object
show all
Defined in:
lib/shale/adapter/nokogiri/document.rb

Overview

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

Wrapper around Nokogiri API

Instance Method Summary collapse

Constructor Details

#initialize(version = nil) ⇒ Document

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.

Initialize object

Parameters:

  • version (String, nil) (defaults to: nil)


15
16
17
18
19
20
21
# File 'lib/shale/adapter/nokogiri/document.rb', line 15

def initialize(version = nil)
  ver = nil
  ver = version if version.is_a?(String)

  @doc = ::Nokogiri::XML::Document.new(ver)
  @namespaces = {}
end

Instance Method Details

#add_attribute(element, name, value) ⇒ 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.

Add attribute to Nokogiri element

Parameters:

  • element (::Nokogiri::XML::Element)

    Nokogiri element

  • name (String)

    Name of the XML attribute

  • value (String)

    Value of the XML attribute



76
77
78
# File 'lib/shale/adapter/nokogiri/document.rb', line 76

def add_attribute(element, name, value)
  element[name] = value
end

#add_element(element, child) ⇒ 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.

Add child element to Nokogiri element

Parameters:

  • element (::Nokogiri::XML::Element)

    Nokogiri parent element

  • child (::Nokogiri::XML::Element)

    Nokogiri child element



86
87
88
# File 'lib/shale/adapter/nokogiri/document.rb', line 86

def add_element(element, child)
  element.add_child(child)
end

#add_namespace(prefix, namespace) ⇒ 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.

Add XML namespace to document

Parameters:

  • prefix (String)
  • namespace (String)


65
66
67
# File 'lib/shale/adapter/nokogiri/document.rb', line 65

def add_namespace(prefix, namespace)
  @namespaces[prefix] = namespace if prefix && namespace
end

#add_text(element, text) ⇒ 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.

Add text node to Nokogiri element

Parameters:

  • element (::Nokogiri::XML::Element)

    Nokogiri element

  • text (String)

    Text to add



96
97
98
# File 'lib/shale/adapter/nokogiri/document.rb', line 96

def add_text(element, text)
  element.content = text
end

#create_cdata(text, parent) ⇒ 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 CDATA node and add it to parent

Parameters:

  • text (String)
  • parent (::Nokogiri::XML::Element)


55
56
57
# File 'lib/shale/adapter/nokogiri/document.rb', line 55

def create_cdata(text, parent)
  parent.add_child(::Nokogiri::XML::CDATA.new(@doc, text))
end

#create_element(name) ⇒ ::Nokogiri::XML::Element

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 Nokogiri element

Parameters:

  • name (String)

    Name of the XML element

Returns:

  • (::Nokogiri::XML::Element)


45
46
47
# File 'lib/shale/adapter/nokogiri/document.rb', line 45

def create_element(name)
  ::Nokogiri::XML::Element.new(name, @doc)
end

#doc::Nokogiri::XML::Document

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.

Return Nokogiri document

Returns:

  • (::Nokogiri::XML::Document)


28
29
30
31
32
33
34
35
36
# File 'lib/shale/adapter/nokogiri/document.rb', line 28

def doc
  if @doc.root
    @namespaces.each do |prefix, namespace|
      @doc.root.add_namespace(prefix, namespace)
    end
  end

  @doc
end