Class: TypeIsEnum::Enum
- Inherits:
-
Object
- Object
- TypeIsEnum::Enum
- Includes:
- Comparable
- Defined in:
- lib/type_is_enum/enum.rb
Overview
Base class for typesafe enum classes.
Direct Known Subclasses
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.
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.
-
.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.
- #name ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#key ⇒ Symbol (readonly)
The symbol key for the enum instance
103 104 105 |
# File 'lib/type_is_enum/enum.rb', line 103 def key @key end |
#ord ⇒ Integer (readonly)
The ordinal of the enum instance, in declaration order
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
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
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
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
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
41 42 43 |
# File 'lib/type_is_enum/enum.rb', line 41 def map(&block) to_a.map(&block) end |
.size ⇒ Integer
Returns the number of enum instances
18 19 20 |
# File 'lib/type_is_enum/enum.rb', line 18 def size as_array ? as_array.length : 0 end |
.to_a ⇒ Array<self>
Returns an array of the enum instances 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
114 115 116 |
# File 'lib/type_is_enum/enum.rb', line 114 def <=>(other) ord <=> other.ord if self.class == other.class end |
#hash ⇒ Fixnum
Generates a Fixnum hash value for this enum instance
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 |
#name ⇒ Object
129 130 131 |
# File 'lib/type_is_enum/enum.rb', line 129 def name key.to_s end |
#to_s ⇒ Object
133 134 135 |
# File 'lib/type_is_enum/enum.rb', line 133 def to_s "#{self.class}::#{key} [#{ord}]" end |