Module: Ruby::Enum::ClassMethods
- Defined in:
- lib/ruby-enum/enum.rb
Instance Method Summary collapse
- #const_missing(key) ⇒ Object
-
#define(key, value = key) ⇒ Object
Define an enumerated value.
-
#each(&block) ⇒ Object
Iterate over all enumerated values, including those defined in a superclass.
-
#each_key(&_block) ⇒ Object
Iterate over all enumerated keys, including those defined in a superclass.
-
#each_value(&_block) ⇒ Object
Iterate over all enumerated values, including those defined in a superclass.
-
#key(v) ⇒ Object
Gets the key symbol for the specified value, including those defined in a superclass.
-
#key?(k) ⇒ Boolean
Whether the specified key exists in this enum, including those defined in a superclass.
-
#keys ⇒ Object
Returns all enum keys, including those defined in a superclass.
-
#parse(k) ⇒ Object
Attempt to parse an enum key and return the corresponding value.
- #store_new_instance(key, value) ⇒ Object
-
#to_h ⇒ Object
Returns a hash of key:values, including those defined in a superclass.
-
#value(k) ⇒ Object
Gets the string value for the specified key, including those defined in a superclass.
-
#value?(v) ⇒ Boolean
Whether the specified value exists in this enum, including those defined in a superclass.
-
#values ⇒ Object
Returns all enum values.
Instance Method Details
#const_missing(key) ⇒ Object
55 56 57 |
# File 'lib/ruby-enum/enum.rb', line 55 def const_missing(key) raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key end |
#define(key, value = key) ⇒ Object
Define an enumerated value.
Parameters
[key] Enumerator key. [value] Enumerator value.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby-enum/enum.rb', line 33 def define(key, value = key) validate_key!(key) validate_value!(value) store_new_instance(key, value) if upper?(key.to_s) const_set key, value else define_singleton_method(key) { value } end end |
#each(&block) ⇒ Object
Iterate over all enumerated values, including those defined in a superclass. Required for Enumerable mixin
61 62 63 |
# File 'lib/ruby-enum/enum.rb', line 61 def each(&block) _enum_hash.each(&block) end |
#each_key(&_block) ⇒ Object
Iterate over all enumerated keys, including those defined in a superclass. Required for Enumerable mixin
148 149 150 151 152 |
# File 'lib/ruby-enum/enum.rb', line 148 def each_key(&_block) _enum_hash.each_value do |v| yield v.key end end |
#each_value(&_block) ⇒ Object
Iterate over all enumerated values, including those defined in a superclass. Required for Enumerable mixin
140 141 142 143 144 |
# File 'lib/ruby-enum/enum.rb', line 140 def each_value(&_block) _enum_hash.each_value do |v| yield v.value end end |
#key(v) ⇒ Object
Gets the key symbol for the specified value, including those defined in a superclass.
Parameters
[v] The string value to parse.
Returns the corresponding key symbol or nil.
117 118 119 120 |
# File 'lib/ruby-enum/enum.rb', line 117 def key(v) enum = _enums_by_value[v] enum&.key end |
#key?(k) ⇒ Boolean
Whether the specified key exists in this enum, including those defined in a superclass.
Parameters
[k] The string key to check.
Returns true if the key exists, false otherwise.
86 87 88 |
# File 'lib/ruby-enum/enum.rb', line 86 def key?(k) _enum_hash.key?(k) end |
#keys ⇒ Object
Returns all enum keys, including those defined in a superclass.
123 124 125 |
# File 'lib/ruby-enum/enum.rb', line 123 def keys _enum_hash.values.map(&:key) end |
#parse(k) ⇒ Object
Attempt to parse an enum key and return the corresponding value.
Parameters
[k] The key string to parse.
Returns the corresponding value or nil.
72 73 74 75 76 77 78 |
# File 'lib/ruby-enum/enum.rb', line 72 def parse(k) k = k.to_s.upcase each do |key, enum| return enum.value if key.to_s.upcase == k end nil end |
#store_new_instance(key, value) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/ruby-enum/enum.rb', line 46 def store_new_instance(key, value) new_instance = new(key, value) _own_enum_hash[key] = new_instance _own_enums_by_value[value] = new_instance # Invalidate memoized, merged hashes since this class' own enums changed. @_enum_hash = @_enums_by_value = nil end |
#to_h ⇒ Object
Returns a hash of key:values, including those defined in a superclass.
155 156 157 |
# File 'lib/ruby-enum/enum.rb', line 155 def to_h _enum_hash.transform_values(&:value) end |
#value(k) ⇒ Object
Gets the string value for the specified key, including those defined in a superclass.
Parameters
[k] The key symbol to get the value for.
Returns the corresponding enum instance or nil.
96 97 98 99 |
# File 'lib/ruby-enum/enum.rb', line 96 def value(k) enum = _enum_hash[k] enum&.value end |
#value?(v) ⇒ Boolean
Whether the specified value exists in this enum, including those defined in a superclass.
Parameters
[k] The string value to check.
Returns true if the value exists, false otherwise.
107 108 109 |
# File 'lib/ruby-enum/enum.rb', line 107 def value?(v) _enums_by_value.key?(v) end |