module Ruby::Enum::ClassMethods

Public Instance Methods

const_missing(key) click to toggle source
# File lib/ruby-enum/enum.rb, line 45
def const_missing(key)
  raise Ruby::Enum::Errors::UninitializedConstantError, name: name, key: key
end
define(key, value) click to toggle source

Define an enumerated value.

Parameters

key

Enumerator key.

value

Enumerator value.

# File lib/ruby-enum/enum.rb, line 23
def define(key, value)
  @_enum_hash ||= {}
  @_enums_by_value ||= {}

  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) click to toggle source

Iterate over all enumerated values. Required for Enumerable mixin

# File lib/ruby-enum/enum.rb, line 51
def each(&block)
  @_enum_hash.each(&block)
end
key(v) click to toggle source

Gets the key symbol for the specified value.

Parameters

v

The string value to parse.

Returns the corresponding key symbol or nil.

# File lib/ruby-enum/enum.rb, line 107
def key(v)
  enum = @_enums_by_value[v]
  enum.key if enum
end
key?(k) click to toggle source

Whether the specified key exists in this enum.

Parameters

k

The string key to check.

Returns true if the key exists, false otherwise.

# File lib/ruby-enum/enum.rb, line 76
def key?(k)
  @_enum_hash.key?(k)
end
keys() click to toggle source

Returns all enum keys.

# File lib/ruby-enum/enum.rb, line 113
def keys
  @_enum_hash.values.map(&:key)
end
parse(k) click to toggle source

Attempt to parse an enum key and return the corresponding value.

Parameters

k

The key string to parse.

Returns the corresponding value or nil.

# File lib/ruby-enum/enum.rb, line 62
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) click to toggle source
# File lib/ruby-enum/enum.rb, line 39
def store_new_instance(key, value)
  new_instance = new(key, value)
  @_enum_hash[key] = new_instance
  @_enums_by_value[value] = new_instance
end
to_h() click to toggle source
# File lib/ruby-enum/enum.rb, line 122
def to_h
  Hash[@_enum_hash.map do |key, enum|
    [key, enum.value]
  end]
end
value(k) click to toggle source

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.

# File lib/ruby-enum/enum.rb, line 86
def value(k)
  enum = @_enum_hash[k]
  enum.value if enum
end
value?(v) click to toggle source

Whether the specified value exists in this enum.

Parameters

k

The string value to check.

Returns true if the value exists, false otherwise.

# File lib/ruby-enum/enum.rb, line 97
def value?(v)
  @_enums_by_value.key?(v)
end
values() click to toggle source

Returns all enum values.

# File lib/ruby-enum/enum.rb, line 118
def values
  @_enum_hash.values.map(&:value)
end

Private Instance Methods

upper?(s) click to toggle source
# File lib/ruby-enum/enum.rb, line 130
def upper?(s)
  !/[[:upper:]]/.match(s).nil?
end
validate_key!(key) click to toggle source
# File lib/ruby-enum/enum.rb, line 134
def validate_key!(key)
  return unless @_enum_hash.key?(key)

  raise Ruby::Enum::Errors::DuplicateKeyError, name: name, key: key
end
validate_value!(value) click to toggle source
# File lib/ruby-enum/enum.rb, line 140
def validate_value!(value)
  return unless @_enums_by_value.key?(value)

  raise Ruby::Enum::Errors::DuplicateValueError, name: name, value: value
end