Module: Ruby::Enum::ClassMethods

Defined in:
lib/ruby-enum/enum.rb

Instance Method Summary collapse

Instance Method Details

#const_missing(key) ⇒ Object



52
53
54
# File 'lib/ruby-enum/enum.rb', line 52

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-enum/enum.rb', line 30

def define(key, value = key)
  @_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) ⇒ Object

Iterate over all enumerated values. Required for Enumerable mixin



58
59
60
# File 'lib/ruby-enum/enum.rb', line 58

def each(&block)
  @_enum_hash.each(&block)
end

#each_key(&_block) ⇒ Object

Iterate over all enumerated keys. Required for Enumerable mixin



145
146
147
148
149
# File 'lib/ruby-enum/enum.rb', line 145

def each_key(&_block)
  @_enum_hash.each_value do |v|
    yield v.key
  end
end

#each_value(&_block) ⇒ Object

Iterate over all enumerated values. Required for Enumerable mixin



137
138
139
140
141
# File 'lib/ruby-enum/enum.rb', line 137

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.

Parameters

v

The string value to parse.

Returns the corresponding key symbol or nil.



114
115
116
117
# File 'lib/ruby-enum/enum.rb', line 114

def key(v)
  enum = @_enums_by_value[v]
  enum&.key
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.

Returns:

  • (Boolean)


83
84
85
# File 'lib/ruby-enum/enum.rb', line 83

def key?(k)
  @_enum_hash.key?(k)
end

#keysObject

Returns all enum keys.



120
121
122
# File 'lib/ruby-enum/enum.rb', line 120

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.



69
70
71
72
73
74
75
# File 'lib/ruby-enum/enum.rb', line 69

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
# File 'lib/ruby-enum/enum.rb', line 46

def store_new_instance(key, value)
  new_instance = new(key, value)
  @_enum_hash[key] = new_instance
  @_enums_by_value[value] = new_instance
end

#to_hObject



151
152
153
# File 'lib/ruby-enum/enum.rb', line 151

def to_h
  @_enum_hash.transform_values(&: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.



93
94
95
96
# File 'lib/ruby-enum/enum.rb', line 93

def value(k)
  enum = @_enum_hash[k]
  enum&.value
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.

Returns:

  • (Boolean)


104
105
106
# File 'lib/ruby-enum/enum.rb', line 104

def value?(v)
  @_enums_by_value.key?(v)
end

#valuesObject

Returns all enum values.



125
126
127
128
129
130
131
132
133
# File 'lib/ruby-enum/enum.rb', line 125

def values
  result = @_enum_hash.values.map(&:value)

  if superclass < Ruby::Enum
    superclass.values + result
  else
    result
  end
end