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
-
#load_interface_attribute(key, interface) ⇒ Object
Loads a single interface attribute.
-
#load_interface_attributes(interface) ⇒ Object
Loads the attributes which have an interface getter and writes their values.
-
#read_interface_attribute(key, interface) ⇒ Object
Reads a single interface attribute without loading it into the given key.
-
#save_interface_attribute(key, interface) ⇒ Object
Saves a single interface attribute.
-
#save_interface_attributes(interface) ⇒ Object
Saves all the attributes which have an interface setter.
-
#spec_to_proc(spec) ⇒ Object
Converts a getter/setter specification to a Proc which can be called to obtain or set a value.
Instance Method Details
#load_interface_attribute(key, interface) ⇒ Object
Loads a single interface attribute.
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.
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, | 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) = self.class.attributes[key.to_sym] return if .has_key?(:property) && ![:property] return if .has_key?(:version) && !version_match?([:version], VirtualBox.version) getter = [:property] || [: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
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) = self.class.attributes[key.to_sym] return if [:readonly] return if .has_key?(:property) && ![:property] return if .has_key?(:version) && !version_match?([:version], VirtualBox.version) setter = [:property] || [: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, | 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 |