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.
-
#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 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) = 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) 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.
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 |
#save_interface_attribute(key, interface) ⇒ Object
Saves a single interface attribute
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) = 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.
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, | 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 |