Class: TypesafeEnum::Base

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
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



110
111
112
# File 'lib/typesafe_enum/base.rb', line 110

def key
  @key
end

#ordInteger (readonly)

The ordinal of the enum instance, in declaration order

Returns:

  • (Integer)

    the ordinal



118
119
120
# File 'lib/typesafe_enum/base.rb', line 118

def ord
  @ord
end

#valueObject (readonly)

The value encapsulated by the enum instance

Returns:

  • (Object)

    the value



114
115
116
# File 'lib/typesafe_enum/base.rb', line 114

def value
  @value
end

Class Method Details

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

Iterates over the set of enum instances

Yields:

  • (self)

    Each instance of this enum, in declaration order

Returns:

  • (Enumerator<self>)

    All instances of this enum, in declaration order



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

def each(&block)
  to_a.each(&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



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

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



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

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



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

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



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

def find_by_value_str(value_str)
  value_str = value_str.to_s
  by_value_str[value_str]
end

.sizeInteger

Returns the number of enum instances

Returns:

  • (Integer)

    the number of instances



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

def size
  as_array.size
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



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

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



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

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



131
132
133
134
135
136
137
138
# File 'lib/typesafe_enum/base.rb', line 131

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



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

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