131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/smart_enum.rb', line 131
def self.register_value(enum_type: self, detect_sti_types: false, **attrs)
fail EnumLocked.new(enum_type) if enum_locked?
type_attr_val = attrs[DEFAULT_TYPE_ATTR_STR] || attrs[DEFAULT_TYPE_ATTR_SYM]
klass = if type_attr_val && detect_sti_types
_constantize_cache[type_attr_val] ||= SmartEnum::Utilities.constantize(type_attr_val)
else
enum_type
end
unless (_descends_from_cache[klass] ||= (klass <= self))
raise RegistrationError.new("Specified class must derive from #{self}", type: klass, attributes: attrs)
end
if klass.abstract_class
raise RegistrationError, "#{klass} is marked as abstract and may not be registered"
end
instance = klass.new(attrs)
id = instance.id
unless id
raise MissingIDError.new(type: klass, attributes: attrs)
end
if _enum_storage.has_key?(id)
raise DuplicateIDError.new(type: klass, attributes: attrs, id: id, existing: _enum_storage[id])
end
instance.freeze_attributes
_enum_storage[id] = instance
if klass != self
klass._enum_storage[id] = instance
end
end
|