Module: Ruby::Enum::ClassMethods
- Defined in:
- lib/ruby-enum/enum.rb
Instance Method Summary collapse
- #const_missing(key) ⇒ Object
-
#define(key, value) ⇒ Object
Define an enumerated value.
-
#each(&block) ⇒ Object
Iterate over all enumerated values.
-
#key(v) ⇒ Object
Gets the key symbol for the specified value.
-
#key?(k) ⇒ Boolean
Whether the specified key exists in this enum.
-
#keys ⇒ Object
Returns all enum keys.
-
#parse(k) ⇒ Object
Attempt to parse an enum key and return the corresponding value.
- #to_h ⇒ Object
- #validate_key!(key) ⇒ Object
- #validate_value!(value) ⇒ Object
-
#value(k) ⇒ Object
Gets the string value for the specified key.
-
#value?(v) ⇒ Boolean
Whether the specified value exists in this enum.
-
#values ⇒ Object
Returns all enum values.
Instance Method Details
#const_missing(key) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby-enum/enum.rb', line 47 def const_missing(key) enum_instance = @_enum_hash && @_enum_hash[key] if enum_instance @_enum_hash[key].value elsif superclass.instance_variable_get(:@_enum_hash) superclass.send(:const_missing, key) else raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key end end |
#define(key, value) ⇒ Object
Define an enumerated value.
Parameters
- key
-
Enumerator key.
- value
-
Enumerator value.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ruby-enum/enum.rb', line 23 def define(key, value) @_enum_hash ||= {} @_enums_by_value ||= {} validate_key!(key) validate_value!(value) new_instance = new(key, value) @_enum_hash[key] = new_instance @_enums_by_value[value] = new_instance end |
#each(&block) ⇒ Object
Iterate over all enumerated values. Required for Enumerable mixin
60 61 62 |
# File 'lib/ruby-enum/enum.rb', line 60 def each(&block) @_enum_hash.each(&block) end |
#key(v) ⇒ Object
Gets the key symbol for the specified value.
Parameters
- v
-
The string value to parse.
Returns the corresponding key symbol or nil.
116 117 118 119 |
# File 'lib/ruby-enum/enum.rb', line 116 def key(v) enum = @_enums_by_value[v] enum.key if enum end |
#key?(k) ⇒ Boolean
Whether the specified key exists in this enum.
Parameters
- k
-
The string key to check.
Returns true if the key exists, false otherwise.
85 86 87 |
# File 'lib/ruby-enum/enum.rb', line 85 def key?(k) @_enum_hash.key?(k) end |
#keys ⇒ Object
Returns all enum keys.
122 123 124 |
# File 'lib/ruby-enum/enum.rb', line 122 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.
71 72 73 74 75 76 77 |
# File 'lib/ruby-enum/enum.rb', line 71 def parse(k) k = k.to_s.upcase each do |key, enum| return enum.value if key.to_s.upcase == k end nil end |
#to_h ⇒ Object
131 132 133 134 135 |
# File 'lib/ruby-enum/enum.rb', line 131 def to_h Hash[@_enum_hash.map do |key, enum| [key, enum.value] end] end |
#validate_key!(key) ⇒ Object
35 36 37 38 39 |
# File 'lib/ruby-enum/enum.rb', line 35 def validate_key!(key) return unless @_enum_hash.key?(key) raise Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key end |
#validate_value!(value) ⇒ Object
41 42 43 44 45 |
# File 'lib/ruby-enum/enum.rb', line 41 def validate_value!(value) return unless @_enums_by_value.key?(value) raise Ruby::Enum::Errors::DuplicateValueError, name: name, value: value end |
#value(k) ⇒ Object
Gets the string value for the specified key.
Parameters
- k
-
The key symbol to get the value for.
Returns the corresponding enum instance or nil.
95 96 97 98 |
# File 'lib/ruby-enum/enum.rb', line 95 def value(k) enum = @_enum_hash[k] enum.value if enum end |
#value?(v) ⇒ Boolean
Whether the specified value exists in this enum.
Parameters
- k
-
The string value to check.
Returns true if the value exists, false otherwise.
106 107 108 |
# File 'lib/ruby-enum/enum.rb', line 106 def value?(v) @_enums_by_value.key?(v) end |
#values ⇒ Object
Returns all enum values.
127 128 129 |
# File 'lib/ruby-enum/enum.rb', line 127 def values @_enum_hash.values.map(&:value) end |