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 54 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.new("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 164 def self.parse_date(text) return nil if text.nil? begin return DateTime.xmlschema(text) rescue raise Error.new("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 128 def self.parse_decimal(text) return nil if text.nil? begin return Float(text) rescue raise Error.new("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 93 def self.parse_integer(text) return nil if text.nil? begin return Integer(text, 10) rescue raise Error.new("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 218 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.new("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] if reader.nil? raise Error.new("Can't find a reader for tag '#{tag}'") end # Read the object using the specific reader: return reader.call(cursor) ensure if !cursor.nil? && !cursor.equal?(source) cursor.close end 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 72 def self.read_boolean(reader) return 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 83 def self.read_booleans(reader) return 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 179 def self.read_date(reader) return 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 190 def self.read_dates(reader) return 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 143 def self.read_decimal(reader) return 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 154 def self.read_decimals(reader) return 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 108 def self.read_integer(reader) return 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 119 def self.read_integers(reader) return 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 33 def self.read_string(reader) return 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 44 def self.read_strings(reader) return 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 207 def self.register(tag, reader) @@readers[tag] = reader end