Class: TypesafeEnum::Base
- Inherits:
-
Object
- Object
- TypesafeEnum::Base
- 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| ... } ⇒ Array<self>
Iterates over the set of enum instances.
-
.each_with_index {|self, Integer| ... } ⇒ Array<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.
-
.map {|self| ... } ⇒ Array
Iterates over the set of enum instances.
-
.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
125 126 127 |
# File 'lib/typesafe_enum/base.rb', line 125 def key @key end |
#ord ⇒ Integer (readonly)
The ordinal of the enum instance, in declaration order
133 134 135 |
# File 'lib/typesafe_enum/base.rb', line 133 def ord @ord end |
#value ⇒ Object (readonly)
The value encapsulated by the enum instance
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
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
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
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
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
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
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
43 44 45 |
# File 'lib/typesafe_enum/base.rb', line 43 def map(&block) to_a.map(&block) end |
.size ⇒ Integer
Returns the number of enum instances
20 21 22 |
# File 'lib/typesafe_enum/base.rb', line 20 def size as_array ? as_array.length : 0 end |
.to_a ⇒ Array<self>
Returns an array of the enum instances 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
140 141 142 |
# File 'lib/typesafe_enum/base.rb', line 140 def <=>(other) ord <=> other.ord if self.class == other.class end |
#hash ⇒ Fixnum
Generates a Fixnum hash value for this enum instance
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_s ⇒ Object
155 156 157 |
# File 'lib/typesafe_enum/base.rb', line 155 def to_s "#{self.class}::#{key} [#{ord}] -> #{value}" end |