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
45 46 47 48 49 50 51 |
# File 'lib/ruby-enum/enum.rb', line 45 def const_missing(key) if @_enum_hash[key] @_enum_hash[key].value else fail 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.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby-enum/enum.rb', line 21 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
55 56 57 |
# File 'lib/ruby-enum/enum.rb', line 55 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.
111 112 113 114 |
# File 'lib/ruby-enum/enum.rb', line 111 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.
80 81 82 |
# File 'lib/ruby-enum/enum.rb', line 80 def key?(k) @_enum_hash.key?(k) end |
#keys ⇒ Object
Returns all enum keys.
117 118 119 |
# File 'lib/ruby-enum/enum.rb', line 117 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.
66 67 68 69 70 71 72 |
# File 'lib/ruby-enum/enum.rb', line 66 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
126 127 128 129 130 |
# File 'lib/ruby-enum/enum.rb', line 126 def to_h Hash[@_enum_hash.map do |key, enum| [key, enum.value] end] end |
#validate_key!(key) ⇒ Object
33 34 35 36 37 |
# File 'lib/ruby-enum/enum.rb', line 33 def validate_key!(key) return unless @_enum_hash.key?(key) fail Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key end |
#validate_value!(value) ⇒ Object
39 40 41 42 43 |
# File 'lib/ruby-enum/enum.rb', line 39 def validate_value!(value) return unless @_enums_by_value.key?(value) fail 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.
90 91 92 93 |
# File 'lib/ruby-enum/enum.rb', line 90 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.
101 102 103 |
# File 'lib/ruby-enum/enum.rb', line 101 def value?(v) @_enums_by_value.key?(v) end |
#values ⇒ Object
Returns all enum values.
122 123 124 |
# File 'lib/ruby-enum/enum.rb', line 122 def values @_enum_hash.values.map(&:value) end |