Class: SmartEnum
- Inherits:
-
Object
- Object
- SmartEnum
- Extended by:
- Associations, YamlStore
- Includes:
- ActiveRecordCompatibility, Attributes
- Defined in:
- lib/smart_enum.rb,
lib/smart_enum/version.rb,
lib/smart_enum/utilities.rb,
lib/smart_enum/attributes.rb,
lib/smart_enum/yaml_store.rb,
lib/smart_enum/associations.rb,
lib/smart_enum/monetize_interop.rb,
lib/smart_enum/active_record_compatibility.rb
Overview
Methods to make SmartEnum models work in contexts like views where rails expects ActiveRecord instances.
Defined Under Namespace
Modules: ActiveRecordCompatibility, Associations, Attributes, MonetizeInterop, Utilities, YamlStore Classes: DuplicateIDError, EnumLocked, EnumUnlocked, MissingIDError, RegistrationError
Constant Summary collapse
- DEFAULT_TYPE_ATTR_STR =
TODO: allow a SmartEnum to define its own type discriminator attr?
"type"
- DEFAULT_TYPE_ATTR_SYM =
:type
- VERSION =
"2.3.0"
- INTEGER =
[Integer].freeze
Constants included from Attributes
Class Attribute Summary collapse
-
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
Class Method Summary collapse
- .[](id) ⇒ Object
- .enum_locked? ⇒ Boolean
- .lock_enum! ⇒ Object
- .register_value(enum_type: self, detect_sti_types: false, **attrs) ⇒ Object
- .register_values(values, enum_type = self, detect_sti_types: false) ⇒ Object
- .values ⇒ Object
Methods included from Associations
__assert_enum, belongs_to_enum, enum_associations, has_many_enums, has_many_enums_through, has_one_enum, has_one_enum_through
Methods included from YamlStore
data_root, data_root=, register_values_from_file!
Methods included from ActiveRecordCompatibility
#_read_attribute, #destroyed?, included, #marked_for_destruction?, #new_record?, #persisted?, #to_key
Methods included from Attributes
#attributes, #freeze_attributes, included, #initialize, #inspect
Class Attribute Details
.abstract_class ⇒ Object
Returns the value of attribute abstract_class.
53 54 55 |
# File 'lib/smart_enum.rb', line 53 def abstract_class @abstract_class end |
Class Method Details
.[](id) ⇒ Object
38 39 40 41 |
# File 'lib/smart_enum.rb', line 38 def self.[](id) ensure_ready_for_reads! _enum_storage[id] end |
.enum_locked? ⇒ Boolean
48 49 50 |
# File 'lib/smart_enum.rb', line 48 def self.enum_locked? @enum_locked end |
.lock_enum! ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/smart_enum.rb', line 108 def self.lock_enum! return if @enum_locked @enum_locked = true @_constantize_cache = nil @_descends_from_cache = nil _enum_storage.freeze class_descendants(self).each do |klass| klass.lock_enum! end end |
.register_value(enum_type: self, detect_sti_types: false, **attrs) ⇒ Object
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 |
.register_values(values, enum_type = self, detect_sti_types: false) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/smart_enum.rb', line 120 def self.register_values(values, enum_type=self, detect_sti_types: false) values.each do |raw_attrs| _deferred_attr_hashes << SmartEnum::Utilities.symbolize_hash_keys(raw_attrs).merge(enum_type: enum_type, detect_sti_types: detect_sti_types) end @_deferred_values_present = true end |
.values ⇒ Object
43 44 45 46 |
# File 'lib/smart_enum.rb', line 43 def self.values ensure_ready_for_reads! _enum_storage.values end |