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
27
28
29
30
31
32
33
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 21

def load_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)
  write_attribute(key, getter.call(self, interface, key))
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

#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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 47

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.



36
37
38
39
40
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 36

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 }


86
87
88
89
90
91
92
93
94
95
# File 'lib/virtualbox/abstract_model/interface_attributes.rb', line 86

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