-
# frozen_string_literal: true
-
-
1
module Ruby
-
1
module Enum
-
1
attr_reader :key, :value
-
-
1
def initialize(key, value)
-
8
@key = key
-
8
@value = value
-
end
-
-
1
def self.included(base)
-
4
base.extend Enumerable
-
4
base.extend ClassMethods
-
-
4
base.private_class_method(:new)
-
end
-
-
1
module ClassMethods
-
# Define an enumerated value.
-
#
-
# === Parameters
-
# [key] Enumerator key.
-
# [value] Enumerator value.
-
1
def define(key, value = key)
-
10
@_enum_hash ||= {}
-
10
@_enums_by_value ||= {}
-
-
10
validate_key!(key)
-
9
validate_value!(value)
-
-
8
store_new_instance(key, value)
-
-
8
if upper?(key.to_s)
-
5
const_set key, value
-
else
-
6
define_singleton_method(key) { value }
-
end
-
end
-
-
1
def store_new_instance(key, value)
-
8
new_instance = new(key, value)
-
8
@_enum_hash[key] = new_instance
-
8
@_enums_by_value[value] = new_instance
-
end
-
-
1
def const_missing(key)
-
3
raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key
-
end
-
-
# Iterate over all enumerated values.
-
# Required for Enumerable mixin
-
1
def each(&block)
-
8
@_enum_hash.each(&block)
-
end
-
-
# Attempt to parse an enum key and return the
-
# corresponding value.
-
#
-
# === Parameters
-
# [k] The key string to parse.
-
#
-
# Returns the corresponding value or nil.
-
1
def parse(k)
-
4
k = k.to_s.upcase
-
4
each do |key, enum|
-
6
return enum.value if key.to_s.upcase == k
-
end
-
nil
-
end
-
-
# Whether the specified key exists in this enum.
-
#
-
# === Parameters
-
# [k] The string key to check.
-
#
-
# Returns true if the key exists, false otherwise.
-
1
def key?(k)
-
7
@_enum_hash.key?(k)
-
end
-
-
# 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.
-
1
def value(k)
-
5
enum = @_enum_hash[k]
-
5
enum&.value
-
end
-
-
# Whether the specified value exists in this enum.
-
#
-
# === Parameters
-
# [k] The string value to check.
-
#
-
# Returns true if the value exists, false otherwise.
-
1
def value?(v)
-
7
@_enums_by_value.key?(v)
-
end
-
-
# Gets the key symbol for the specified value.
-
#
-
# === Parameters
-
# [v] The string value to parse.
-
#
-
# Returns the corresponding key symbol or nil.
-
1
def key(v)
-
5
enum = @_enums_by_value[v]
-
5
enum&.key
-
end
-
-
# Returns all enum keys.
-
1
def keys
-
2
@_enum_hash.values.map(&:key)
-
end
-
-
# Returns all enum values.
-
1
def values
-
7
result = @_enum_hash.values.map(&:value)
-
-
7
if superclass < Ruby::Enum
-
3
superclass.values + result
-
else
-
4
result
-
end
-
end
-
-
# Iterate over all enumerated values.
-
# Required for Enumerable mixin
-
1
def each_value(&_block)
-
1
@_enum_hash.each_value do |v|
-
2
yield v.value
-
end
-
end
-
-
# Iterate over all enumerated keys.
-
# Required for Enumerable mixin
-
1
def each_key(&_block)
-
1
@_enum_hash.each_value do |v|
-
2
yield v.key
-
end
-
end
-
-
1
def to_h
-
1
Hash[@_enum_hash.map do |key, enum|
-
2
[key, enum.value]
-
end]
-
end
-
-
1
private
-
-
1
def upper?(s)
-
8
!/[[:upper:]]/.match(s).nil?
-
end
-
-
1
def validate_key!(key)
-
10
return unless @_enum_hash.key?(key)
-
-
1
raise Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key
-
end
-
-
1
def validate_value!(value)
-
9
return unless @_enums_by_value.key?(value)
-
-
1
raise Ruby::Enum::Errors::DuplicateValueError, name: name, value: value
-
end
-
end
-
end
-
end