Module: PersistentEnum::ActsAsEnum
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/persistent_enum/acts_as_enum.rb
Defined Under Namespace
Modules: ClassMethods Classes: State
Constant Summary collapse
- LOCK =
Monitor.new
- @@known_enumerations =
{}
Class Method Summary collapse
- .register_acts_as_enum(clazz) ⇒ Object
-
.reinitialize_enumerations ⇒ Object
Reload enumerations from the database: useful if the database contents may have changed (e.g. fixture loading).
-
.rerequire_known_enumerations ⇒ Object
Ensure that all known_enumerations are loaded by resolving each name constant and reregistering the resulting class.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Is this enum member still present in the enum declaration?.
- #enum_constant ⇒ Object
- #ordinal ⇒ Object
-
#readonly? ⇒ Boolean
Enum values should not be mutable: allow creation and modification only before the state has been initialized.
- #to_sym ⇒ Object
Class Method Details
.register_acts_as_enum(clazz) ⇒ Object
197 198 199 200 201 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 197 def register_acts_as_enum(clazz) LOCK.synchronize do @@known_enumerations[clazz.name] = clazz end end |
.reinitialize_enumerations ⇒ Object
Reload enumerations from the database: useful if the database contents may have changed (e.g. fixture loading).
205 206 207 208 209 210 211 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 205 def reinitialize_enumerations LOCK.synchronize do @@known_enumerations.each_value do |clazz| clazz.reinitialize_acts_as_enum end end end |
.rerequire_known_enumerations ⇒ Object
Ensure that all known_enumerations are loaded by resolving each name constant and reregistering the resulting class. Raises NameError if a previously-encountered type cannot be resolved.
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 216 def rerequire_known_enumerations LOCK.synchronize do @@known_enumerations.keys.each do |name| new_clazz = name.safe_constantize unless new_clazz.is_a?(Class) raise NameError.new("Could not resolve ActsAsEnum type '#{name}' after reload") end register_acts_as_enum(new_clazz) end end end |
Instance Method Details
#active? ⇒ Boolean
Is this enum member still present in the enum declaration?
189 190 191 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 189 def active? self.class.active?(self) end |
#enum_constant ⇒ Object
176 177 178 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 176 def enum_constant read_attribute(self.class.name_attr) end |
#ordinal ⇒ Object
184 185 186 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 184 def ordinal read_attribute(:id) end |
#readonly? ⇒ Boolean
Enum values should not be mutable: allow creation and modification only before the state has been initialized.
172 173 174 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 172 def readonly? !self.class._acts_as_enum_state.nil? end |
#to_sym ⇒ Object
180 181 182 |
# File 'lib/persistent_enum/acts_as_enum.rb', line 180 def to_sym enum_constant.to_sym end |