Class: TypesafeEnum::Base

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/typesafe_enum/base.rb

Overview

Base class for typesafe enum classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject (readonly)

The symbol key for the enum instance



99
100
101
# File 'lib/typesafe_enum/base.rb', line 99

def key
  @key
end

#ordObject (readonly)

The ordinal of the enum instance, in declaration order



103
104
105
# File 'lib/typesafe_enum/base.rb', line 103

def ord
  @ord
end

#valueObject (readonly)

The value encapsulated by the enum instance



101
102
103
# File 'lib/typesafe_enum/base.rb', line 101

def value
  @value
end

Class Method Details

.each(&block) ⇒ Object

Iterates over the set of enum instances



21
22
23
# File 'lib/typesafe_enum/base.rb', line 21

def each(&block)
  to_a.each(&block)
end

.each_with_index(&block) ⇒ Object

Iterates over the set of enum instances



26
27
28
# File 'lib/typesafe_enum/base.rb', line 26

def each_with_index(&block)
  to_a.each_with_index(&block)
end

.find_by_key(key) ⇒ Object

Looks up an enum instance based on its key



36
37
38
# File 'lib/typesafe_enum/base.rb', line 36

def find_by_key(key)
  by_key[key]
end

.find_by_ord(ord) ⇒ Object

Looks up an enum instance based on its ordinal



55
56
57
58
# File 'lib/typesafe_enum/base.rb', line 55

def find_by_ord(ord)
  return nil if ord < 0 || ord > size
  as_array[ord]
end

.find_by_value(value) ⇒ Object

Looks up an enum instance based on its value



41
42
43
# File 'lib/typesafe_enum/base.rb', line 41

def find_by_value(value)
  by_value[value]
end

.find_by_value_str(value_str) ⇒ Object

Looks up an enum instance based on the string representation of its value



46
47
48
49
50
51
52
# File 'lib/typesafe_enum/base.rb', line 46

def find_by_value_str(value_str)
  value_str = value_str.to_s
  by_value.each do |value, instance|
    return instance if value_str == value.to_s
  end
  nil
end

.map(&block) ⇒ Object

Iterates over the set of enum instances



31
32
33
# File 'lib/typesafe_enum/base.rb', line 31

def map(&block)
  to_a.map(&block)
end

.sizeObject

Returns the number of enum instances



16
17
18
# File 'lib/typesafe_enum/base.rb', line 16

def size
  as_array ? as_array.length : 0
end

.to_aObject

Returns an array of the enum instances in declaration order



11
12
13
# File 'lib/typesafe_enum/base.rb', line 11

def to_a
  as_array.dup
end

Instance Method Details

#<=>(other) ⇒ Object

Compares two instances of the same enum class based on their declaration order



106
107
108
# File 'lib/typesafe_enum/base.rb', line 106

def <=>(other)
  ord <=> other.ord if self.class == other.class
end

#hashObject

Generates a Fixnum hash value for this enum instance



111
112
113
114
115
116
117
118
# File 'lib/typesafe_enum/base.rb', line 111

def hash
  @hash ||= begin
    result = 17
    result = 31 * result + self.class.hash
    result = 31 * result + ord
    result.is_a?(Fixnum) ? result : result.hash
  end
end