class Decay::EnumeratedType
Attributes
key[R]
value[R]
Public Class Methods
[](key)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
each() { |key, value| ... }
click to toggle source
# 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
freeze()
click to toggle source
Calls superclass method
# File lib/decay/enumerated_type.rb, line 71 def freeze registry.freeze super end
key_for(value)
click to toggle source
# 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
keys()
click to toggle source
# File lib/decay/enumerated_type.rb, line 48 def keys registry.keys end
members()
click to toggle source
# File lib/decay/enumerated_type.rb, line 67 def members registry.values end
new(key, value)
click to toggle source
# File lib/decay/enumerated_type.rb, line 101 def initialize(key, value) @key = key @value = value end
Private Class Methods
normalized(value)
click to toggle source
# File lib/decay/enumerated_type.rb, line 92 def normalized(value) if value.respond_to?(:to_sym) value.to_sym else value end end
registry()
click to toggle source
# File lib/decay/enumerated_type.rb, line 88 def registry @registry ||= {} end
Public Instance Methods
case()
click to toggle source
# File lib/decay/enumerated_type.rb, line 109 def case Case.new(self.class, self) end