Module: Libvirt::Spec::Util
- Included in:
- Device::Disk, Device::Graphics, Device::Input, Device::Interface, Device::Sound, Device::Video, Device::VideoModel, Domain, Domain::Clock, Domain::OSBooting
- Defined in:
- lib/libvirt/spec/util.rb
Overview
Utility methods for the spec classes. This module is typically included for each class.
Instance Method Summary collapse
-
#raise_if_unparseables(search_result) ⇒ Object
This will raise an Exception::UnparseableSpec exception if there are any search results given.
-
#try(search_result, options = nil) {|search_result| ... } ⇒ Object
Tries the given XML search, running the block if there are any results.
Instance Method Details
#raise_if_unparseables(search_result) ⇒ Object
This will raise an Exception::UnparseableSpec exception if there are any search results given. This is meant as a helper to reduce the duplicity of this feature across specs.
38 39 40 |
# File 'lib/libvirt/spec/util.rb', line 38 def raise_if_unparseables(search_result) raise Exception::UnparseableSpec, search_result if !search_result.empty? end |
#try(search_result, options = nil) {|search_result| ... } ⇒ Object
Tries the given XML search, running the block if there are any results. This allows a concise syntax for loading data from XML which may or may not exist.
Warning: By default, the result of the search given will be removed from the XML tree. See the options below for information on how to avoid this.
An additional parameter supports options given as a hash. This allows for the following to be set:
multi
- If true, then all results will be returned, not just the first one.preserve
- If true, then the node will not be deleted after yielding to the block.
25 26 27 28 29 30 31 |
# File 'lib/libvirt/spec/util.rb', line 25 def try(search_result, =nil) ||= {} return if search_result.empty? search_result = search_result.first if ![:multi] yield search_result search_result.remove if ![:preserve] end |