class Libvirt::Xml::Generic
Constants
- FALSE_VALUES
- TRUE_VALUES
Public Class Methods
attribute(name, options = {})
click to toggle source
# File lib/libvirt/xml/generic.rb, line 29 def self.attribute(name, options = {}) _attributes_opts.merge!(name.to_sym => options.dup) attr_accessor name end
attributes(*names)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 21 def self.attributes(*names) options = names.extract_options! names.each do |name| _attributes_opts.merge!(name.to_sym => options.dup) end attr_accessor(*names) end
build(attrs = {})
click to toggle source
Build xml object with attributes. @param attrs [Hash] @return [Xml::Base]
# File lib/libvirt/xml/generic.rb, line 44 def self.build(attrs = {}) xml_node = Nokogiri::XML(nil) obj = new(xml_node) attrs.each { |key, val| obj.public_send("#{key}=", val) } obj end
inherited(subclass)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 12 def self.inherited(subclass) subclass._root_path = '.' subclass._attributes_opts = _attributes_opts.dup end
load(xml)
click to toggle source
@param xml [String] @return [Class<LibvirtXml::Generic>]
# File lib/libvirt/xml/generic.rb, line 36 def self.load(xml) xml_node = Nokogiri::XML(xml).xpath(_root_path).first new(xml_node) end
new(xml_node)
click to toggle source
@param xml_node [Nokogiri::XML::Element]
# File lib/libvirt/xml/generic.rb, line 52 def initialize(xml_node) @xml_node = xml_node parse_xml_node end
root_path(path)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 17 def self.root_path(path) self._root_path = path end
Public Instance Methods
[](attr)
click to toggle source
@param attr [Symbol,String] @return [Object,nil]
# File lib/libvirt/xml/generic.rb, line 59 def [](attr) read_attribute(attr) end
[]=(attr, value)
click to toggle source
@param attr [Symbol,String] @param value [Object,nil] @return [Object,nil]
# File lib/libvirt/xml/generic.rb, line 66 def []=(attr, value) write_attribute(attr, value) end
to_h()
click to toggle source
@return [Hash{Symbol=>(Object,nil)}]
# File lib/libvirt/xml/generic.rb, line 71 def to_h _attributes_opts.map do |name, _opts| value = public_send(name) [name, serialize_for_hash(value)] end.to_h end
to_xml()
click to toggle source
@return [String]
# File lib/libvirt/xml/generic.rb, line 79 def to_xml @xml_node.to_xml end
Private Instance Methods
decode(value, opts)
click to toggle source
Cast value using “decode_#{type}” method. @param value [String] @param opts [Hash{Symbol=>Object}] @return [Object, nil]
# File lib/libvirt/xml/generic.rb, line 116 def decode(value, opts) return opts[:default] if value.nil? cast = opts[:cast] return value if cast.nil? meth = "decode_#{cast}" if opts[:array] value.map do |val| if cast.is_a?(Proc) cast.call(val, opts) elsif respond_to?(meth, true) send(meth, val, opts) else raise ArgumentError, "invalid :cast option #{cast.inspect}" end end end if cast.is_a?(Proc) cast.call(value, opts) elsif respond_to?(meth, true) send(meth, value, opts) else raise ArgumentError, "invalid :cast option #{cast.inspect}" end end
decode_bool(value, _opts)
click to toggle source
@param value [String, Boolean] @return [Boolean]
# File lib/libvirt/xml/generic.rb, line 147 def decode_bool(value, _opts) return value if value.is_a?(TrueClass) || value.is_a?(FalseClass) return true if TRUE_VALUES.include?(value) return false if FALSE_VALUES.include?(value) nil end
decode_int(value, _opts)
click to toggle source
@param value [String, Integer] @return [Integer] @raise [ArgumentError]
# File lib/libvirt/xml/generic.rb, line 160 def decode_int(value, _opts) Integer(value) end
find_nodes(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 164 def find_nodes(name, opts) value_name = opts[:name]&.to_sym || name path = opts[:path] || "./#{value_name}" path == :root ? [@xml_node] : @xml_node.xpath(path) end
parse_node(name, opts)
click to toggle source
Parse node value using “parse_node_#{type}” method. @param name [Symbol] @param opts [Hash{Symbol=>Object}] @return [Object, nil]
# File lib/libvirt/xml/generic.rb, line 99 def parse_node(name, opts) type = opts[:type] || :text meth = "parse_node_#{type}" if opts[:apply] opts[:apply].call(@xml_node, opts) elsif respond_to?(meth, true) send(meth, name, opts) else raise ArgumentError, "Invalid :type option #{type.inspect} for attribute #{name}" end end
parse_node_attr(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 181 def parse_node_attr(name, opts) nodes = find_nodes name, { path: :root }.merge(opts) value_name = opts[:name]&.to_sym || name return nodes.map { |node| node[value_name.to_s] } if opts[:array] node = nodes.first return if node.nil? node[value_name.to_s] end
parse_node_memory(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 218 def parse_node_memory(name, opts) nodes = find_nodes(name, opts) if opts[:array] return [] if nodes.empty? return nodes.map { |node| Util.parse_memory node.text, node['unit'] } end node = nodes.first return if node.nil? Util.parse_memory node.text, node['unit'] end
parse_node_raw(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 207 def parse_node_raw(name, opts) nodes = find_nodes(name, opts) return nodes.map(&:to_xml) if opts[:array] node = nodes.first return if node.nil? node.to_xml end
parse_node_struct(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 193 def parse_node_struct(name, opts) klass = opts[:class] raise ArgumentError, "Invalid :class option nil for attribute #{name}" if klass.nil? nodes = find_nodes(name, opts) return nodes.map { |node| klass.new(node) } if opts[:array] node = nodes.first return if node.nil? klass.new(node) end
parse_node_text(name, opts)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 170 def parse_node_text(name, opts) nodes = find_nodes(name, opts) return nodes.map(&:text) if opts[:array] node = nodes.first return if node.nil? node.text end
parse_xml_node()
click to toggle source
# File lib/libvirt/xml/generic.rb, line 87 def parse_xml_node _attributes_opts.each do |name, opts| value = parse_node(name, opts) value = decode(value, opts) write_attribute name, value end end
read_attribute(attr)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 233 def read_attribute(attr) attr = attr.to_sym raise ArgumentError, "can't find attribute #{attr}" unless _attributes_opts.key?(attr) instance_variable_get :"@#{attr}" end
serialize_for_hash(value)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 247 def serialize_for_hash(value) return value.to_h if value.is_a?(Generic) return value.map { |val| serialize_for_hash(val) } if value.is_a?(Array) value end
write_attribute(attr, value)
click to toggle source
# File lib/libvirt/xml/generic.rb, line 240 def write_attribute(attr, value) attr = attr.to_sym raise ArgumentError, "can't find attribute #{attr}" unless _attributes_opts.key?(attr) instance_variable_set :"@#{attr}", value end