Module: OvirtSDK4::Type

Included in:
List, Struct
Defined in:
lib/ovirtsdk4/type.rb

Overview

This module is a mixin that contains the methods common to struct and list types.

Instance Method Summary collapse

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?
  disk_attachments = vm.disk_attachments
  if !disk_attachments.nil?
    first_disk_attachment = disk_attachments[0]
    if !first_disk_attachment.nil?
      disk = first_disk_attachment.disk
      if !disk.nil?
        first_disk_id = disk.id
      end
    end
  end
end

Parameters:

  • keys (Array<Symbol, Integer>)

    An array of symbols corresponding to attribute names of structs, or integers corresponding to list indexes.


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

#hrefString

Returns the value of the href attribute.

Returns:

  • (String)

27
28
29
# File 'lib/ovirtsdk4/type.rb', line 27

def href
  @href
end

#href=(value) ⇒ Object

Sets the value of the href attribute.

Parameters:

  • value (String)

36
37
38
# File 'lib/ovirtsdk4/type.rb', line 36

def href=(value)
  @href = value
end