This is the base class for all the XML readers used by the SDK. It contains the utility methods used by all of them.
@api private
Converts the given text to a boolean value.
@param text [String] @return [Boolean]
# File lib/ovirtsdk4/reader.rb, line 52 def self.parse_boolean(text) return nil if text.nil? case text.downcase when 'false', '0' return false when 'true', '1' return true else raise Error, "The text '#{text}' isn't a valid boolean value." end end
Converts the given text to a date value.
@param text [String] @return [DateTime]
# File lib/ovirtsdk4/reader.rb, line 162 def self.parse_date(text) return nil if text.nil? begin return DateTime.xmlschema(text) rescue raise Error, "The text '#{text}' isn't a valid date." end end
Converts the given text to a decimal value.
@return [Fixnum]
# File lib/ovirtsdk4/reader.rb, line 126 def self.parse_decimal(text) return nil if text.nil? begin return Float(text) rescue raise Error, "The text '#{text}' isn't a valid decimal value." end end
Converts the given text to an integer value.
@param text [String] @return [Integer]
# File lib/ovirtsdk4/reader.rb, line 91 def self.parse_integer(text) return nil if text.nil? begin return Integer(text, 10) rescue raise Error, "The text '#{text}' isn't a valid integer value." end end
Reads one object, determining the reader method to use based on the tag name of the first element. For example, if the first tag name is `vm` then it will create a `Vm` object, if it the tag is `vms` it will create an array of `Vm` objects, so on.
@param source [String, XmlReader] The string, IO or XML reader where the input will be taken from.
# File lib/ovirtsdk4/reader.rb, line 216 def self.read(source) # If the source is a string or IO object then create a XML reader from it: cursor = nil if source.is_a?(String) || source.is_a?(IO) cursor = XmlReader.new(source) elsif source.is_a?(XmlReader) cursor = source else raise ArgumentError, "Expected a 'String' or 'XmlReader', but got '#{source.class}'" end # Do the actual read, and make sure to always close the XML reader if we created it: begin # Do nothing if there aren't more tags: return nil unless cursor.forward # Select the specific reader according to the tag: tag = cursor.node_name reader = @readers[tag] raise Error, "Can't find a reader for tag '#{tag}'" if reader.nil? # Read the object using the specific reader: reader.call(cursor) ensure cursor.close if !cursor.nil? && !cursor.equal?(source) end end
Reads a boolean value, assuming that the cursor is positioned at the start element that contains the value.
@param reader [XmlReader] @return [Boolean]
# File lib/ovirtsdk4/reader.rb, line 70 def self.read_boolean(reader) Reader.parse_boolean(reader.read_element) end
Reads a list of boolean values, assuming that the cursor is positioned at the start element that contains the values.
@param reader [XmlReader] @return [Array<Boolean>]
# File lib/ovirtsdk4/reader.rb, line 81 def self.read_booleans(reader) reader.read_elements.map { |text| Reader.parse_boolean(text) } end
Reads a date value, assuming that the cursor is positioned at the start element that contains the value.
@param reader [XmlReader] @return [DateTime]
# File lib/ovirtsdk4/reader.rb, line 177 def self.read_date(reader) Reader.parse_date(reader.read_element) end
Reads a list of dates values, assuming that the cursor is positioned at the start element that contains the values.
@param reader [XmlReader] @return [Array<DateTime>]
# File lib/ovirtsdk4/reader.rb, line 188 def self.read_dates(reader) reader.read_elements.map { |text| Reader.parse_date(text) } end
Reads a decimal value, assuming that the cursor is positioned at the start element that contains the value.
@param reader [XmlReader] @return [Fixnum]
# File lib/ovirtsdk4/reader.rb, line 141 def self.read_decimal(reader) Reader.parse_decimal(reader.read_element) end
Reads a list of decimal values, assuming that the cursor is positioned at the start element that contains the values.
@param reader [XmlReader] @return [Array<Fixnum>]
# File lib/ovirtsdk4/reader.rb, line 152 def self.read_decimals(reader) reader.read_elements.map { |text| Reader.parse_decimal(text) } end
Reads an integer value, assuming that the cursor is positioned at the start element that contains the value.
@param reader [XmlReader] @return [Integer]
# File lib/ovirtsdk4/reader.rb, line 106 def self.read_integer(reader) Reader.parse_integer(reader.read_element) end
Reads a list of integer values, assuming that the cursor is positioned at the start element that contains the values.
@param reader [XmlReader] @return [Array<Integer>]
# File lib/ovirtsdk4/reader.rb, line 117 def self.read_integers(reader) reader.read_elements.map { |text| Reader.parse_integer(text) } end
Reads a string value, assuming that the cursor is positioned at the start element that contains the value.
@param reader [XmlReader] @return [String]
# File lib/ovirtsdk4/reader.rb, line 31 def self.read_string(reader) reader.read_element end
Reads a list of string values, assuming that the cursor is positioned at the start of the element that contains the first value.
@param reader [XmlReader] @return [Array<String>]
# File lib/ovirtsdk4/reader.rb, line 42 def self.read_strings(reader) reader.read_elements end
Registers a read method.
@param tag [String] The tag name. @param reader [Method] The reference to the method that reads the object corresponding to the `tag`.
# File lib/ovirtsdk4/reader.rb, line 205 def self.register(tag, reader) @readers[tag] = reader end