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

Instance Method Summary collapse

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_enumerationsObject

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_enumerationsObject

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_constantObject



176
177
178
# File 'lib/persistent_enum/acts_as_enum.rb', line 176

def enum_constant
  read_attribute(self.class.name_attr)
end

#ordinalObject



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_symObject



180
181
182
# File 'lib/persistent_enum/acts_as_enum.rb', line 180

def to_sym
  enum_constant.to_sym
end