Class: TypesafeEnum::Base
- Inherits:
-
Object
- Object
- TypesafeEnum::Base
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/typesafe_enum/base.rb
Overview
Base class for typesafe enum classes.
Instance Attribute Summary collapse
-
#key ⇒ Symbol
readonly
The symbol key for the enum instance.
-
#ord ⇒ Integer
readonly
The ordinal of the enum instance, in declaration order.
-
#value ⇒ Object
readonly
The value encapsulated by the enum instance.
Class Method Summary collapse
-
.each {|self| ... } ⇒ Enumerator<self>
Iterates over the set of enum instances.
-
.find_by_key(key) ⇒ self?
Looks up an enum instance based on its key.
-
.find_by_ord(ord) ⇒ self?
Looks up an enum instance based on its ordinal.
-
.find_by_value(value) ⇒ self?
Looks up an enum instance based on its value.
-
.find_by_value_str(value_str) ⇒ self?
Looks up an enum instance based on the string representation of its value.
-
.size ⇒ Integer
Returns the number of enum instances.
-
.to_a ⇒ Array<self>
Returns an array of the enum instances in declaration order.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compares two instances of the same enum class based on their declaration order.
-
#hash ⇒ Fixnum
Generates a Fixnum hash value for this enum instance.
- #to_s ⇒ Object
Instance Attribute Details
#key ⇒ Symbol (readonly)
The symbol key for the enum instance
110 111 112 |
# File 'lib/typesafe_enum/base.rb', line 110 def key @key end |
#ord ⇒ Integer (readonly)
The ordinal of the enum instance, in declaration order
118 119 120 |
# File 'lib/typesafe_enum/base.rb', line 118 def ord @ord end |
#value ⇒ Object (readonly)
The value encapsulated by the enum instance
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
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
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
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
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
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 |
.size ⇒ Integer
Returns the number of enum instances
21 22 23 |
# File 'lib/typesafe_enum/base.rb', line 21 def size as_array.size end |
.to_a ⇒ Array<self>
Returns an array of the enum instances 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
125 126 127 |
# File 'lib/typesafe_enum/base.rb', line 125 def <=>(other) ord <=> other.ord if self.class == other.class end |
#hash ⇒ Fixnum
Generates a Fixnum hash value for this enum instance
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_s ⇒ Object
140 141 142 |
# File 'lib/typesafe_enum/base.rb', line 140 def to_s "#{self.class}::#{key} [#{ord}] -> #{value}" end |