Module: VirtualBox::AbstractModel::InterfaceAttributes

Included in:
VirtualBox::AbstractModel
Defined in:
lib/virtualbox/abstract_model/interface_attributes.rb

Overview

Module which can be included which defines helper methods to DRY out the code which handles attributes with COM interfaces. This module works alongside the Attributable module, so both are required.

Instance Method Summary collapse

Instance Method Details

#load_interface_attribute(key, interface) ⇒ Object

Loads a single interface attribute.

Parameters:



21
22
23
24
25
26
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 21

def load_interface_attribute(key, interface)
  value = read_interface_attribute(key, interface)
  return if !value

  write_attribute(key, value)
end

#load_interface_attributes(interface) ⇒ Object

Loads the attributes which have an interface getter and writes their values.

Parameters:



11
12
13
14
15
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 11

def load_interface_attributes(interface)
  self.class.attributes.each do |key, options|
    load_interface_attribute(key, interface)
  end
end

#read_interface_attribute(key, interface) ⇒ Object

Reads a single interface attribute without loading it into the given key.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 30

def read_interface_attribute(key, interface)
  # Return unless we have a valid interface attribute with a getter
  return unless has_attribute?(key)
  options = self.class.attributes[key.to_sym]
  return if options.has_key?(:property) && !options[:property]
  return if options.has_key?(:version) && !version_match?(options[:version], VirtualBox.version)
  getter = options[:property] || options[:property_getter] || key.to_sym
  return unless getter

  # Convert the getter to a proc and call it
  getter = spec_to_proc(getter)
  getter.call(self, interface, key)
end

#save_interface_attribute(key, interface) ⇒ Object

Saves a single interface attribute

Parameters:

  • key (Symbol)

    The attribute to write

  • interface (VirtualBox::COM::Interface)

    The interface

  • value (Object)

    The value to write



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 56

def save_interface_attribute(key, interface)
  # Return unless we have a valid interface attribute with a setter
  return unless has_attribute?(key)
  options = self.class.attributes[key.to_sym]
  return if options[:readonly]
  return if options.has_key?(:property) && !options[:property]
  return if options.has_key?(:version) && !version_match?(options[:version], VirtualBox.version)

  setter = options[:property] || options[:property_setter] || "#{key}=".to_sym
  return unless setter

  # Convert the setter to a proc and call it
  setter = spec_to_proc(setter)
  setter.call(self, interface, key, read_attribute(key))
end

#save_interface_attributes(interface) ⇒ Object

Saves all the attributes which have an interface setter.



45
46
47
48
49
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 45

def save_interface_attributes(interface)
  self.class.attributes.each do |key, options|
    save_interface_attribute(key, interface)
  end
end

#spec_to_proc(spec) ⇒ Object

Converts a getter/setter specification to a Proc which can be called to obtain or set a value. There are multiple ways to specify the getter and/or setter of an interface attribute:

Symbol

A symbol represents a method to call on the interface. An example of the declaration and resulting method call are shown below:

attribute :foo, :property_getter => :get_foo

Converts to:

interface.get_foo

Proc

A proc is called with the interface and it is expected to return the value for a getter. For a setter, the interface and the value is sent in as parameters to the Proc.

attribute :foo, :property_getter => Proc.new { |i| i.get_foo }


95
96
97
98
99
100
101
102
103
104
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 95

def spec_to_proc(spec)
  # Return the spec as-is if its a proc
  return spec if spec.is_a?(Proc)

  if spec.is_a?(Symbol)
    # For symbols, wrap up a method send in a Proc and return
    # that
    return Proc.new { |this, m, key, *args| m.send(spec, *args) }
  end
end