Class: Decay::EnumeratedType

Inherits:
Object
  • Object
show all
Defined in:
lib/decay/enumerated_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ EnumeratedType

Returns a new instance of EnumeratedType.



101
102
103
104
# File 'lib/decay/enumerated_type.rb', line 101

def initialize(key, value)
  @key = key
  @value = value
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



106
107
108
# File 'lib/decay/enumerated_type.rb', line 106

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



107
108
109
# File 'lib/decay/enumerated_type.rb', line 107

def value
  @value
end

Class Method Details

.[](key) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/decay/enumerated_type.rb', line 37

def [](key)
  if registry.key?(key)
    registry[key]
  else
    valid_keys = registry.keys.map(&:inspect).map { |str| "`#{str}'" }
    raise Error::UnknownKey,
      "Attempted to search for unknown key `#{key.inspect}'. " \
      "Valid options are #{valid_keys.join(", ")}"
  end
end

.[]=(key, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/decay/enumerated_type.rb', line 26

def []=(key, value)
  if registry.frozen?
    raise Error::CantDefineEnumAtRuntime, \
      "New values can only be created on Enum creation"
  elsif registry.key?(key)
    raise Error::DuplicateEnum, "Attempted to re-define `#{key.inspect}'"
  else
    registry[key] = new(key, value)
  end
end

.create(*definitions) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/decay/enumerated_type.rb', line 4

def create(*definitions)
  type = Class.new(self)

  definitions.each do |definition|
    if definition.respond_to?(:keys)
      definition.each do |key, value|
        type[normalized(key)] = value
      end
    elsif definition.respond_to?(:each)
      definition.each do |key|
        type[normalized(key)] = normalized(key)
      end
    else
      type[normalized(definition)] = normalized(definition)
    end
  end

  type.freeze

  type
end

.eachObject



76
77
78
79
80
81
82
83
84
# File 'lib/decay/enumerated_type.rb', line 76

def each
  if !block_given?
    return enum_for(:each)
  end

  registry.each do |key, value|
    yield key, value
  end
end

.freezeObject



71
72
73
74
# File 'lib/decay/enumerated_type.rb', line 71

def freeze
  registry.freeze
  super
end

.key_for(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/decay/enumerated_type.rb', line 52

def key_for(value)
  if value.is_a?(EnumeratedType)
    registry.invert[value]
  else
    raw_value = registry.map { |k, v| [k, v.value] }.to_h
    value_key = raw_value.invert

    if value_key.key?(value)
      value_key[value]
    else
      value_key[normalized(value)]
    end
  end
end

.keysObject



48
49
50
# File 'lib/decay/enumerated_type.rb', line 48

def keys
  registry.keys
end

.membersObject



67
68
69
# File 'lib/decay/enumerated_type.rb', line 67

def members
  registry.values
end

Instance Method Details

#caseObject



109
110
111
# File 'lib/decay/enumerated_type.rb', line 109

def case
  Case.new(self.class, self)
end