Class: VirtualBox::COM::AbstractEnum
- Inherits:
-
AbstractModel
- Object
- AbstractModel
- VirtualBox::COM::AbstractEnum
- Extended by:
- Enumerable
- Defined in:
- lib/virtualbox/com/abstract_enum.rb
Overview
Represents a C enum type. Provides functionality to easily convert an int value to its proper symbol within the enum.
Class Method Summary collapse
-
.[](index) ⇒ Object
Returns the symbol associated with the given key.
-
.each ⇒ Object
Iterate over the enum, yielding each item to a block.
-
.index(key) ⇒ Object
Returns the index associated with a value.
-
.map(value = nil) ⇒ Object
Defines the mapping of int => symbol for the given Enum.
Methods inherited from AbstractModel
Class Method Details
.[](index) ⇒ Object
Returns the symbol associated with the given key
30 31 32 33 34 35 |
# File 'lib/virtualbox/com/abstract_enum.rb', line 30 def self.[](index) case index when Symbol then @map[index] else @reverse_map[index] end end |
.each ⇒ Object
Iterate over the enum, yielding each item to a block.
43 44 45 46 47 |
# File 'lib/virtualbox/com/abstract_enum.rb', line 43 def self.each @map.each do |key, value| yield key end end |
.index(key) ⇒ Object
Returns the index associated with a value
38 39 40 |
# File 'lib/virtualbox/com/abstract_enum.rb', line 38 def self.index(key) @map[key] end |
.map(value = nil) ⇒ Object
Defines the mapping of int => symbol for the given Enum. The parameter to this can be an Array or Hash or anything which respond to ‘each` and yield a key/value pair. If a value appear more than once, only the first is kept If value is left nil, it will return the current mapping
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/virtualbox/com/abstract_enum.rb', line 16 def self.map(value = nil) if value m, r = {}, {} if Array === value then value.each_index {|i| m[value[i]] = i; r[i] = value[i] } else value.each {|k,v| m[k] = v; r[v] ||= k } end @map, @reverse_map = m, r end @map end |