Class: AMT::Utility::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/amt/utility/enum.rb

Overview

A simple class that behaves like an enumeration. I.e. you can associate an arbitrary value with a name.

The class allows you to get to a specific enumeration instance by using either the name or the value.

It isn’t useful to create objects directly from this class but one should create a subclass and use the provided class methods.

Here is an example: You have a Processor class and this processor class should have a field which contains the current status of the processor. The status is given by an integer with a predefined range. You could model the status field as enumeration by creating a new enumeration class:

class Status < AMT::Utility::Enum
  multi_add(0 => 'Unknown', 1 => 'Enabled', 2 => 'Disabled by user via BIOS setup',
            3 => 'Disabled by BIOS (POST Error)', 4 => 'Idle, waiting to be enabled',
            7 => 'Other')
end

Then you can use this new Status class in the constructor of the Processor class:

class Processor
  attr_reader :status

  def initialize(status)
    @status = Status.for(status)
  end

end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject (readonly)

The enumeration description.



46
47
48
# File 'lib/amt/utility/enum.rb', line 46

def description
  @description
end

#nameObject (readonly)

The enumeration name.



43
44
45
# File 'lib/amt/utility/enum.rb', line 43

def name
  @name
end

#valueObject (readonly)

The enumeration value.



40
41
42
# File 'lib/amt/utility/enum.rb', line 40

def value
  @value
end

Class Method Details

.add(value, name = nil, description = nil) ⇒ Object

Add a new enumeration value to the enumeration.

value

The value of the enumeration (normally an Integer or String)

name

The name of the value (a Symbol or a String)

description

A textual description of the value.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/amt/utility/enum.rb', line 95

def self.add(value, name = nil, description = nil)
  @value_map ||= {}
  @name_map ||= {}
  name ||= value.to_s
  if @value_map.has_key?(value) || @name_map.has_key?(name)
    raise ArgumentError, "value #{value} or name '#{name}' are already taken"
  end
  inst = new(value, name, description)
  @value_map[value] = inst
  @name_map[name] = inst
  inst
end

.for(value_or_name) ⇒ Object

Return an enumeration value for the given parameter which can either be the value or the name of an enumeration instance.



86
87
88
# File 'lib/amt/utility/enum.rb', line 86

def self.for(value_or_name)
  (value_or_name.kind_of?(self) ? value_or_name : @value_map[value_or_name] || @name_map[value_or_name])
end

.multi_add(values) ⇒ Object

Add all values of the provided collection as enumeration values. The values of the collection should either have the form [value, [name, description]] or the collection should be a hash mapping values to [name, description] arrays.



111
112
113
# File 'lib/amt/utility/enum.rb', line 111

def self.multi_add(values)
  values.each {|v, (n,d)| add(v,n,d)}
end

.valuesObject

Return an array with all defined enumeration values.



80
81
82
# File 'lib/amt/utility/enum.rb', line 80

def self.values
  @value_map.values
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
# File 'lib/amt/utility/enum.rb', line 66

def ==(other) #:nodoc:
  if other.kind_of?(self.class)
    self.value == other.value
  else
    self.value == other || self.name == other
  end
end

#inspectObject

:nodoc:



74
75
76
# File 'lib/amt/utility/enum.rb', line 74

def inspect #:nodoc:
  "#<#{self.class.name} #{@name}>"
end

#to_strObject Also known as: to_s

An enumeration instance can be treated like a String. It’s string value is its description.



61
62
63
# File 'lib/amt/utility/enum.rb', line 61

def to_str
  description
end