Class: Oga::XML::XmlDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/oga/xml/xml_declaration.rb

Overview

Class containing information about an XML declaration tag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ XmlDeclaration

Returns a new instance of XmlDeclaration.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :version (String)
  • :encoding (String)
  • :standalone (String)


24
25
26
27
28
# File 'lib/oga/xml/xml_declaration.rb', line 24

def initialize(options = {})
  @version    = options[:version] || '1.0'
  @encoding   = options[:encoding] || 'UTF-8'
  @standalone = options[:standalone]
end

Instance Attribute Details

#encodingString

Returns:

  • (String)


11
12
13
# File 'lib/oga/xml/xml_declaration.rb', line 11

def encoding
  @encoding
end

#standaloneString

Whether or not the document is a standalone document.

Returns:

  • (String)


15
16
17
# File 'lib/oga/xml/xml_declaration.rb', line 15

def standalone
  @standalone
end

#versionString

Returns:

  • (String)


8
9
10
# File 'lib/oga/xml/xml_declaration.rb', line 8

def version
  @version
end

Instance Method Details

#inspectString

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/oga/xml/xml_declaration.rb', line 50

def inspect
  segments = []

  [:version, :encoding, :standalone].each do |attr|
    value = send(attr)

    if value and !value.empty?
      segments << "#{attr}: #{value.inspect}"
    end
  end

  "XmlDeclaration(#{segments.join(' ')})"
end

#to_xmlString

Converts the declaration tag to XML.

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oga/xml/xml_declaration.rb', line 35

def to_xml
  pairs = []

  [:version, :encoding, :standalone].each do |getter|
    value = send(getter)

    pairs << %Q{#{getter}="#{value}"} if value
  end

  "<?xml #{pairs.join(' ')} ?>"
end