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

#keySymbol (readonly)

The symbol key for the enum instance

Returns:

  • (Symbol)

    the key



125
126
127
# File 'lib/typesafe_enum/base.rb', line 125

def key
  @key
end

#ordInteger (readonly)

The ordinal of the enum instance, in declaration order

Returns:

  • (Integer)

    the ordinal



133
134
135
# File 'lib/typesafe_enum/base.rb', line 133

def ord
  @ord
end

#valueObject (readonly)

The value encapsulated by the enum instance

Returns:

  • (Object)

    the value



129
130
131
# File 'lib/typesafe_enum/base.rb', line 129

def value
  @value
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



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

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



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

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



50
51
52
# File 'lib/typesafe_enum/base.rb', line 50

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



72
73
74
75
76
# File 'lib/typesafe_enum/base.rb', line 72

def find_by_ord(ord)
  return nil if ord > size || ord.negative?

  as_array[ord]
end

.find_by_value(value) ⇒ self?

Looks up an enum instance based on its value

Parameters:

  • value (Object)

    the value to look up

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



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

def find_by_value(value)
  by_value[value]
end

.find_by_value_str(value_str) ⇒ self?

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

Parameters:

  • value_str (String)

    the string form of the value

Returns:

  • (self, nil)

    the corresponding enum instance, or nil



64
65
66
67
# File 'lib/typesafe_enum/base.rb', line 64

def find_by_value_str(value_str)
  value_str = value_str.to_s
  by_value_str[value_str]
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



43
44
45
# File 'lib/typesafe_enum/base.rb', line 43

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

.sizeInteger

Returns the number of enum instances

Returns:

  • (Integer)

    the number of instances



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

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



14
15
16
# File 'lib/typesafe_enum/base.rb', line 14

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



140
141
142
# File 'lib/typesafe_enum/base.rb', line 140

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



146
147
148
149
150
151
152
153
# File 'lib/typesafe_enum/base.rb', line 146

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

#to_sObject



155
156
157
# File 'lib/typesafe_enum/base.rb', line 155

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