Module: TypesafeEnum::ClassMethods
Overview
Class methods for {TypesafeEnum{TypesafeEnum::Base}.
Instance Method Summary collapse
-
#each {|self| ... } ⇒ Enumerator<self>
Iterates over the set of enum instances.
-
#each_key {|Enumerator<Symbol>| ... } ⇒ Enumerator<Symbol>
Iterates over the set of keys of all instances (#keys).
-
#each_value {|Enumerator<Object>| ... } ⇒ Enumerator<Object>
Iterates over the set of values of all instances (#values).
-
#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!(value) ⇒ self, EnumValidationError
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.
-
#find_by_value_str!(value_str) ⇒ self, EnumValidationError
Looks up an enum instance based on the string representation of its value.
-
#keys ⇒ Enumerator<Symbol>
The set of all keys of all enum instances.
-
#size ⇒ Integer
Returns the number of enum instances.
-
#to_a ⇒ Array<self>
Returns an array of the enum instances in declaration order.
-
#values ⇒ Enumerator<Object>
The set of all values of all enum instances.
Instance Method Details
#each {|self| ... } ⇒ Enumerator<self>
Iterates over the set of enum instances
21 22 23 |
# File 'lib/typesafe_enum/class_methods.rb', line 21 def each(&block) to_a.each(&block) end |
#each_key {|Enumerator<Symbol>| ... } ⇒ Enumerator<Symbol>
Iterates over the set of keys of all instances (#keys)
40 41 42 |
# File 'lib/typesafe_enum/class_methods.rb', line 40 def each_key(&block) keys.each(&block) end |
#each_value {|Enumerator<Object>| ... } ⇒ Enumerator<Object>
Iterates over the set of values of all instances (#values)
47 48 49 |
# File 'lib/typesafe_enum/class_methods.rb', line 47 def each_value(&block) values.each(&block) end |
#find_by_key(key) ⇒ self?
Looks up an enum instance based on its key
54 55 56 |
# File 'lib/typesafe_enum/class_methods.rb', line 54 def find_by_key(key) by_key[key] end |
#find_by_ord(ord) ⇒ self?
Looks up an enum instance based on its ordinal
96 97 98 99 100 |
# File 'lib/typesafe_enum/class_methods.rb', line 96 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
61 62 63 |
# File 'lib/typesafe_enum/class_methods.rb', line 61 def find_by_value(value) by_value[value] end |
#find_by_value!(value) ⇒ self, EnumValidationError
Looks up an enum instance based on its value
68 69 70 71 72 73 |
# File 'lib/typesafe_enum/class_methods.rb', line 68 def find_by_value!(value) valid = find_by_value(value) return valid unless valid.nil? raise Exceptions::EnumValidationError, "#{class_name}: #{value} is absurd" end |
#find_by_value_str(value_str) ⇒ self?
Looks up an enum instance based on the string representation of its value
78 79 80 81 |
# File 'lib/typesafe_enum/class_methods.rb', line 78 def find_by_value_str(value_str) value_str = value_str.to_s by_value_str[value_str] end |
#find_by_value_str!(value_str) ⇒ self, EnumValidationError
Looks up an enum instance based on the string representation of its value
86 87 88 89 90 91 |
# File 'lib/typesafe_enum/class_methods.rb', line 86 def find_by_value_str!(value_str) valid = find_by_value_str(value_str) return valid unless valid.nil? raise Exceptions::EnumValidationError, "#{class_name}: #{value_str} is absurd" end |
#keys ⇒ Enumerator<Symbol>
The set of all keys of all enum instances
27 28 29 |
# File 'lib/typesafe_enum/class_methods.rb', line 27 def keys to_a.map(&:key) end |
#size ⇒ Integer
Returns the number of enum instances
14 15 16 |
# File 'lib/typesafe_enum/class_methods.rb', line 14 def size as_array.size end |
#to_a ⇒ Array<self>
Returns an array of the enum instances in declaration order
8 9 10 |
# File 'lib/typesafe_enum/class_methods.rb', line 8 def to_a as_array.dup end |
#values ⇒ Enumerator<Object>
The set of all values of all enum instances
33 34 35 |
# File 'lib/typesafe_enum/class_methods.rb', line 33 def values to_a.map(&:value) end |