module PowerEnum::Enumerated::EnumClassMethods
These are class level methods which are patched into classes that act as enumerated
Attributes
Public Instance Methods
Enum lookup by Symbol, String, or id. Returns <tt>arg<tt> if arg is an enum instance. Passing in a list of arguments returns a list of enums. When called with no arguments, returns nil.
# File lib/power_enum/enumerated.rb, line 219 def [](*args) case args.size when 0 nil when 1 arg = args.first Array === arg ? self[*arg] : (lookup_enum_by_type(arg) || handle_lookup_failure(arg)) else args.map{ |item| self[item] }.uniq end end
Returns all the active enum values. See the ‘active?’ instance method.
# File lib/power_enum/enumerated.rb, line 195 def active return @all_active if @all_active @all_active = all.find_all{ |enum| enum.active? }.freeze end
Returns true for ActiveRecord
models that act as enumerated.
# File lib/power_enum/enumerated.rb, line 172 def acts_as_enumerated? true end
Returns all the enum values. Caches results after the first time this method is run.
# File lib/power_enum/enumerated.rb, line 177 def all return @all if @all freeze_handler = if (handler = self.acts_enumerated_freeze_members).nil? -> { Rails.env.production? } else case handler when Proc handler else -> { handler } end end @all = load_all.collect{ |val| !!freeze_handler.call ? val.freeze : val }.freeze end
Returns all except for the given list
# File lib/power_enum/enumerated.rb, line 212 def all_except(*excluded) all.find_all { |item| !(item === excluded) } end
Returns true
if the given Symbol, String or id has a member instance in the enumeration, false
otherwise. Returns true
if the argument is an enum instance, returns false
if the argument is nil or any other value.
# File lib/power_enum/enumerated.rb, line 235 def contains?(arg) case arg when Symbol !!lookup_name(arg.id2name) when String !!lookup_name(arg) when Integer !!lookup_id(arg) when self true else false end end
Returns true if the enumerations model is in the middle of an update_enumerations_model
block, false otherwise.
# File lib/power_enum/enumerated.rb, line 320 def enumerations_model_updating? !!@enumerations_model_updating end
Returns all the inactive enum values. See the ‘inactive?’ instance method.
# File lib/power_enum/enumerated.rb, line 201 def inactive return @all_inactive if @all_inactive @all_inactive = all.find_all{ |enum| !enum.active? }.freeze end
Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
# File lib/power_enum/enumerated.rb, line 261 def include?(arg) case arg when Symbol !lookup_name(arg.id2name).nil? when String !lookup_name(arg).nil? when Integer !lookup_id(arg).nil? when self possible_match = lookup_id(arg.id) !possible_match.nil? && possible_match == arg else false end end
Enum lookup by id
# File lib/power_enum/enumerated.rb, line 251 def lookup_id(arg) all_by_id[arg] end
Enum lookup by String
# File lib/power_enum/enumerated.rb, line 256 def lookup_name(arg) all_by_name[arg] end
Returns the name of the column this enum uses as the basic underlying value.
# File lib/power_enum/enumerated.rb, line 325 def name_column @name_column ||= self.acts_enumerated_name_column end
Returns the names of all the enum values as an array of symbols.
# File lib/power_enum/enumerated.rb, line 207 def names all.map { |item| item.name_sym } end
NOTE: purging the cache is sort of pointless because of the per-process rails model. By default this blows up noisily just in case you try to be more clever than rails allows. For those times (like in Migrations) when you really do want to alter the records you can silence the carping by setting enumeration_model_updates_permitted
to true.
# File lib/power_enum/enumerated.rb, line 284 def purge_enumerations_cache unless self.enumeration_model_updates_permitted raise "#{self.name}: cache purging disabled for your protection" end @all = @all_by_name = @all_by_id = @all_active = nil end
The preferred method to update an enumerations model. The same warnings as ‘purge_enumerations_cache’ and ‘enumerations_model_update_permitted’ apply. Pass a block to this method where you perform your updates. Cache will be flushed automatically. If your block takes an argument, will pass in the model class. The argument is optional.
# File lib/power_enum/enumerated.rb, line 297 def update_enumerations_model(&block) if block_given? begin self.enumeration_model_updates_permitted = true purge_enumerations_cache @all = load_all @enumerations_model_updating = true case block.arity when 0 yield else yield self end ensure purge_enumerations_cache @enumerations_model_updating = false self.enumeration_model_updates_permitted = false end end end
Private Instance Methods
Returns a hash of all enumeration members keyed by their ids.
# File lib/power_enum/enumerated.rb, line 371 def all_by_id @all_by_id ||= all_by_attribute( primary_key ) end
Returns a hash of all the enumeration members keyed by their names.
# File lib/power_enum/enumerated.rb, line 376 def all_by_name begin @all_by_name ||= all_by_attribute( :__enum_name__ ) rescue NoMethodError => err if err.name == name_column raise TypeError, "#{self.name}: you need to define a '#{name_column}' column in the table '#{table_name}'" end raise end end
Deals with a lookup failure for the given argument.
# File lib/power_enum/enumerated.rb, line 357 def handle_lookup_failure(arg) if (lookup_failure_handler = self.acts_enumerated_on_lookup_failure) case lookup_failure_handler when Proc lookup_failure_handler.call(arg) else self.send(lookup_failure_handler, arg) end else self.send(:enforce_none, arg) end end
—Private methods—
# File lib/power_enum/enumerated.rb, line 331 def load_all conditions = self.acts_enumerated_conditions order = self.acts_enumerated_order unscoped.where(conditions).order(order) end
Looks up the enum based on the type of the argument.
# File lib/power_enum/enumerated.rb, line 338 def lookup_enum_by_type(arg) case arg when Symbol lookup_name(arg.id2name) when String lookup_name(arg) when Integer lookup_id(arg) when self arg when nil nil else raise TypeError, "#{self.name}[]: argument should"\ " be a String, Symbol or Integer but got a: #{arg.class.name}" end end
raise the {ActiveRecord::RecordNotFound} error. @private
# File lib/power_enum/enumerated.rb, line 421 def raise_record_not_found(arg) raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})" end