Class: Libvirt::Xml::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/libvirt/xml/generic.rb

Constant Summary collapse

TRUE_VALUES =
%w[yes on].freeze
FALSE_VALUES =
%w[no off].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Generic

Returns a new instance of Generic.

Parameters:

  • xml_node (Nokogiri::XML::Element)


52
53
54
55
# File 'lib/libvirt/xml/generic.rb', line 52

def initialize(xml_node)
  @xml_node = xml_node
  parse_xml_node
end

Class Method Details

.attribute(name, **options) ⇒ Object



29
30
31
32
# 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) ⇒ Object



21
22
23
24
25
26
27
# 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) ⇒ Xml::Base

Build xml object with attributes.

Parameters:

  • attrs (Hash)

Returns:

  • (Xml::Base)


44
45
46
47
48
49
# 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) ⇒ Object



12
13
14
15
# File 'lib/libvirt/xml/generic.rb', line 12

def self.inherited(subclass)
  subclass._root_path = '.'
  subclass._attributes_opts = _attributes_opts.dup
end

.load(xml) ⇒ Class<LibvirtXml::Generic>

Parameters:

  • xml (String)

Returns:

  • (Class<LibvirtXml::Generic>)


36
37
38
39
# 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

.root_path(path) ⇒ Object



17
18
19
# File 'lib/libvirt/xml/generic.rb', line 17

def self.root_path(path)
  self._root_path = path
end

Instance Method Details

#[](attr) ⇒ Object?

Parameters:

  • attr (Symbol, String)

Returns:

  • (Object, nil)


59
60
61
# File 'lib/libvirt/xml/generic.rb', line 59

def [](attr)
  read_attribute(attr)
end

#[]=(attr, value) ⇒ Object?

Parameters:

  • attr (Symbol, String)
  • value (Object, nil)

Returns:

  • (Object, nil)


66
67
68
# File 'lib/libvirt/xml/generic.rb', line 66

def []=(attr, value)
  write_attribute(attr, value)
end

#to_hHash{Symbol=>(Object,nil)}

Returns:

  • (Hash{Symbol=>(Object,nil)})


71
72
73
74
75
76
# 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_xmlString

Returns:

  • (String)


79
80
81
# File 'lib/libvirt/xml/generic.rb', line 79

def to_xml
  @xml_node.to_xml
end