class OvirtSDK4::Writer

This is the base class for all the XML writers used by the SDK. It contains the utility methods used by all of them.

@api private

Public Class Methods

register(type, writer) click to toggle source

Registers a write method.

@param type [Class] The type. @param writer [Method] The reference to the method that writes the XML document corresponding to the type.

# File lib/ovirtsdk4/writer.rb, line 132
def self.register(type, writer)
  @writers[type] = writer
end
render_boolean(value) click to toggle source

Converts the given boolean value to an string.

@param value [Boolean] @return [String]

# File lib/ovirtsdk4/writer.rb, line 42
def self.render_boolean(value)
  value ? 'true' : 'false'
end
render_date(value) click to toggle source

Converts the given date value to an string.

@param value [DateTime] @return [String]

# File lib/ovirtsdk4/writer.rb, line 105
def self.render_date(value)
  value.xmlschema
end
render_decimal(value) click to toggle source

Converts the given decimal value to an string.

@param value [Fixnum] @return [String]

# File lib/ovirtsdk4/writer.rb, line 84
def self.render_decimal(value)
  value.to_s
end
render_integer(value) click to toggle source

Converts the given integer value to an string.

@param value [Integer] @return [String]

# File lib/ovirtsdk4/writer.rb, line 63
def self.render_integer(value)
  value.to_s
end
write(object, opts = {}) click to toggle source

Writes one object, determining the writer method to use based on the type. For example if the type of the object is `Vm` then it will create write the `vm` tag, with its contents.

@param object [Struct] The object to write.

@param opts [Hash] Options to alter the behaviour of the method.

@option opts [XmlWriter] :target The XML writer where the output will be written. If it this option

isn't given, or if the value is `nil` the method will return a string contain the XML document.

@option opts [String] :root The name of the root tag of the generated XML document. This isn't needed

when writing single objects, as the tag is calculated from the type of the object, for example, if
the object is a virtual machine then the tag will be `vm`. But when writing arrays of objects the tag
is needed, because the list may be empty, or have different types of objects. In this case, for arrays,
if the name isn't provided an exception will be raised.
# File lib/ovirtsdk4/writer.rb, line 153
def self.write(object, opts = {})
  # Get the options:
  target = opts[:target]
  root = opts[:root]

  # If the target is `nil` then create a temporary XML writer to write the output:
  cursor = nil
  if target.nil?
    cursor = XmlWriter.new
  elsif target.is_a?(XmlWriter)
    cursor = target
  else
    raise ArgumentError, "Expected an 'XmlWriter', but got '#{target.class}'"
  end

  # Do the actual write, and make sure to always close the XML writer if we created it:
  begin
    if object.is_a?(Array)
      # For arrays we can't decide which tag to use, so the 'root' parameter is mandatory in this case:
      if root.nil?
        raise Error, "The 'root' option is mandatory when writing arrays"
      end

      # Write the root tag, and then recursively call the method to write each of the items of the array:
      cursor.write_start(root)
      object.each do |item|
        write(item, target: cursor)
      end
      cursor.write_end
    else
      # Select the specific writer according to the type:
      type = object.class
      writer = @writers[type]
      raise Error, "Can't find a writer for type '#{type}'" if writer.nil?

      # Write the object using the specific method:
      writer.call(object, cursor, root)
    end

    # If no XML cursor was explicitly given, and we created it, then we need to return the generated XML text:
    cursor.string if target.nil?
  ensure
    cursor.close if !cursor.nil? && !cursor.equal?(target)
  end
end
write_boolean(writer, name, value) click to toggle source

Writes an element with the given name and boolean value.

@param writer [XmlWriter] @param name [String] @param value [Boolean]

# File lib/ovirtsdk4/writer.rb, line 53
def self.write_boolean(writer, name, value)
  writer.write_element(name, Writer.render_boolean(value))
end
write_date(writer, name, value) click to toggle source

Writes an element with the given name and date value.

@param writer [XmlWriter] @param name [String] @param value [DateTime]

# File lib/ovirtsdk4/writer.rb, line 116
def self.write_date(writer, name, value)
  writer.write_element(name, Writer.render_date(value))
end
write_decimal(writer, name, value) click to toggle source

Writes an element with the given name and decimal value.

@param writer [XmlWriter] @param name [String] @param value [Fixnum]

# File lib/ovirtsdk4/writer.rb, line 95
def self.write_decimal(writer, name, value)
  writer.write_element(name, Writer.render_decimal(value))
end
write_integer(writer, name, value) click to toggle source

Writes an element with the given name and integer value.

@param writer [XmlWriter] @param name [String] @param value [Integer]

# File lib/ovirtsdk4/writer.rb, line 74
def self.write_integer(writer, name, value)
  writer.write_element(name, Writer.render_integer(value))
end
write_string(writer, name, value) click to toggle source

Writes an element with the given name and string value.

@param writer [XmlWriter] @param name [String] @param text [String]

# File lib/ovirtsdk4/writer.rb, line 32
def self.write_string(writer, name, value)
  writer.write_element(name, value)
end