Class: TypesafeEnum::Base
- Inherits:
-
Object
- Object
- TypesafeEnum::Base
- Extended by:
- ClassMethods
- 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.
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 ⇒ String
Default implementation includes the enum class,
key
,ord
andvalue
.
Methods included from ClassMethods
each, each_key, each_value, find_by_key, find_by_ord, find_by_value, find_by_value!, find_by_value_str, find_by_value_str!, keys, size, to_a, values
Instance Attribute Details
#key ⇒ Symbol (readonly)
The symbol key for the enum instance
20 21 22 |
# File 'lib/typesafe_enum/base.rb', line 20 def key @key end |
#ord ⇒ Integer (readonly)
The ordinal of the enum instance, in declaration order
28 29 30 |
# File 'lib/typesafe_enum/base.rb', line 28 def ord @ord end |
#value ⇒ Object (readonly)
The value encapsulated by the enum instance
24 25 26 |
# File 'lib/typesafe_enum/base.rb', line 24 def value @value end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compares two instances of the same enum class based on their declaration order
35 36 37 38 39 40 |
# File 'lib/typesafe_enum/base.rb', line 35 def <=>(other) # in the case where the enum being compared is actually the parent # class, only `==` will work correctly & we cannot use #is_a? or # #instance_of? ord <=> other.ord if self.class == other.class end |
#hash ⇒ Fixnum
Generates a Fixnum hash value for this enum instance
44 45 46 47 48 49 50 51 |
# File 'lib/typesafe_enum/base.rb', line 44 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 ⇒ String
Default implementation includes the enum class, key
,
ord
and value
.
56 57 58 |
# File 'lib/typesafe_enum/base.rb', line 56 def to_s "#{self.class}::#{key} [#{ord}] -> #{value.inspect}" end |