Module: OvirtSDK4::Type
Overview
This module is a mixin that contains the methods common to struct and list types.
Instance Method Summary collapse
-
#dig(*keys) ⇒ Object
Returns the attribute corresponding to the given set of keys, but will not generate a
NoMethodError
when the value corresponding to the key isnil
, it will returnnil
instead. -
#href ⇒ String
Returns the value of the
href
attribute. -
#href=(value) ⇒ Object
Sets the value of the
href
attribute.
Instance Method Details
#dig(*keys) ⇒ Object
Returns the attribute corresponding to the given set of keys, but will not generate a NoMethodError
when
the value corresponding to the key is nil
, it will return nil
instead. It is intended to simplify access
to deeply nested attributes within an structure, and mostly copied from the Ruby 2.3 dig
method available
for hashes.
For example, to access the alias of the first disk attached to a virtual machine that is part of an event without
having to check for nil
several times:
event = ...
first_disk_id = event.dig(:vm, :disk_attachments, 0, :disk, :alias)
Which is equivalent to this:
event = ...
first_disk_id = nil
vm = event.vm
if !vm.nil?
= vm.
if !.nil?
= [0]
if !.nil?
disk = .disk
if !disk.nil?
first_disk_id = disk.id
end
end
end
end
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ovirtsdk4/type.rb', line 79 def dig(*keys) current = self keys.each do |key| if key.is_a?(Symbol) current = current.send(key) elsif key.is_a?(Integer) current = current[key] else raise TypeError, "The key '#{key}' isn't a symbol or integer" end break if current.nil? end current end |
#href ⇒ String
Returns the value of the href
attribute.
27 28 29 |
# File 'lib/ovirtsdk4/type.rb', line 27 def href @href end |
#href=(value) ⇒ Object
Sets the value of the href
attribute.
36 37 38 |
# File 'lib/ovirtsdk4/type.rb', line 36 def href=(value) @href = value end |