Class: TypeIsEnum::Enum

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/type_is_enum/enum.rb

Overview

Base class for typesafe enum classes.

Direct Known Subclasses

ValueEnum

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keySymbol (readonly)

The symbol key for the enum instance

Returns:

  • (Symbol)

    the key



103
104
105
# File 'lib/type_is_enum/enum.rb', line 103

def key
  @key
end

#ordInteger (readonly)

The ordinal of the enum instance, in declaration order

Returns:

  • (Integer)

    the ordinal



107
108
109
# File 'lib/type_is_enum/enum.rb', line 107

def ord
  @ord
end

Class Method Details

.each {|self| ... } ⇒ Array<self>

Iterates over the set of enum instances

Yields:

  • (self)

    Each instance of this enum, in declaration order

Returns:

  • (Array<self>)

    All instances of this enum, in declaration order



25
26
27
# File 'lib/type_is_enum/enum.rb', line 25

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

.each_with_index {|self, Integer| ... } ⇒ Array<self>

Iterates over the set of enum instances

Yields:

  • (self, Integer)

    Each instance of this enum, in declaration order, with its ordinal index

Returns:

  • (Array<self>)

    All instances of this enum, in declaration order



33
34
35
# File 'lib/type_is_enum/enum.rb', line 33

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

.find_by_key(key) ⇒ self?

Looks up an enum instance based on its key

Parameters:

  • key (Symbol)

    the key to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



48
49
50
# File 'lib/type_is_enum/enum.rb', line 48

def find_by_key(key)
  by_key[key]
end

.find_by_ord(ord) ⇒ self?

Looks up an enum instance based on its ordinal

Parameters:

  • ord (Integer)

    the ordinal to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



55
56
57
58
# File 'lib/type_is_enum/enum.rb', line 55

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

.map {|self| ... } ⇒ Array

Iterates over the set of enum instances

Yields:

  • (self)

    Each instance of this enum, in declaration order

Returns:

  • (Array)

    An array containing the result of applying &block to each instance of this enum, in instance declaration order



41
42
43
# File 'lib/type_is_enum/enum.rb', line 41

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

.sizeInteger

Returns the number of enum instances

Returns:

  • (Integer)

    the number of instances



18
19
20
# File 'lib/type_is_enum/enum.rb', line 18

def size
  as_array ? as_array.length : 0
end

.to_aArray<self>

Returns an array of the enum instances in declaration order

Returns:

  • (Array<self>)

    All instances of this enum, in declaration order



12
13
14
# File 'lib/type_is_enum/enum.rb', line 12

def to_a
  as_array.dup
end

Instance Method Details

#<=>(other) ⇒ Integer?

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

Parameters:

  • other (self)

    the enum instance to compare

Returns:

  • (Integer, nil)

    -1 if this value precedes other; 0 if the two are the same enum instance; 1 if this value follows other; nil if other is not an instance of this enum class



114
115
116
# File 'lib/type_is_enum/enum.rb', line 114

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

#hashFixnum

Generates a Fixnum hash value for this enum instance

Returns:

  • (Fixnum)

    the hash value



120
121
122
123
124
125
126
127
# File 'lib/type_is_enum/enum.rb', line 120

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

#nameObject



129
130
131
# File 'lib/type_is_enum/enum.rb', line 129

def name
  key.to_s
end

#to_sObject



133
134
135
# File 'lib/type_is_enum/enum.rb', line 133

def to_s
  "#{self.class}::#{key} [#{ord}]"
end