module PowerEnum::Enumerated::EnumClassMethods
These are class level methods which are patched into classes that act as enumerated
Attributes
Public Instance Methods
Source
# File lib/power_enum/enumerated.rb, line 221 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
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.
Source
# File lib/power_enum/enumerated.rb, line 197 def active return @all_active if @all_active @all_active = all.find_all{ |enum| enum.active? }.freeze end
Returns all the active enum values. See the ‘active?’ instance method.
Source
# File lib/power_enum/enumerated.rb, line 174 def acts_as_enumerated? true end
Returns true for ActiveRecord
models that act as enumerated.
Source
# File lib/power_enum/enumerated.rb, line 179 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 the enum values. Caches results after the first time this method is run.
Source
# File lib/power_enum/enumerated.rb, line 214 def all_except(*excluded) all.find_all { |item| !(item === excluded) } end
Returns all except for the given list
Source
# File lib/power_enum/enumerated.rb, line 237 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 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.
Source
# File lib/power_enum/enumerated.rb, line 322 def enumerations_model_updating? !!@enumerations_model_updating end
Returns true if the enumerations model is in the middle of an update_enumerations_model
block, false otherwise.
Source
# File lib/power_enum/enumerated.rb, line 203 def inactive return @all_inactive if @all_inactive @all_inactive = all.find_all{ |enum| !enum.active? }.freeze end
Returns all the inactive enum values. See the ‘inactive?’ instance method.
Source
# File lib/power_enum/enumerated.rb, line 263 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
Returns true if the enum lookup by the given Symbol, String or id would have returned a value, false otherwise.
Source
# File lib/power_enum/enumerated.rb, line 253 def lookup_id(arg) all_by_id[arg] end
Enum lookup by id
Source
# File lib/power_enum/enumerated.rb, line 258 def lookup_name(arg) all_by_name[arg] end
Enum lookup by String
Source
# File lib/power_enum/enumerated.rb, line 327 def name_column @name_column ||= self.acts_enumerated_name_column end
Returns the name of the column this enum uses as the basic underlying value.
Source
# File lib/power_enum/enumerated.rb, line 209 def names all.map { |item| item.name_sym } end
Returns the names of all the enum values as an array of symbols.
Source
# File lib/power_enum/enumerated.rb, line 286 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
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.
Source
# File lib/power_enum/enumerated.rb, line 299 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
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.
Private Instance Methods
Source
# File lib/power_enum/enumerated.rb, line 373 def all_by_id @all_by_id ||= all_by_attribute( primary_key ) end
Returns a hash of all enumeration members keyed by their ids.
Source
# File lib/power_enum/enumerated.rb, line 378 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
Returns a hash of all the enumeration members keyed by their names.
Source
# File lib/power_enum/enumerated.rb, line 359 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
Deals with a lookup failure for the given argument.
Source
# File lib/power_enum/enumerated.rb, line 333 def load_all conditions = self.acts_enumerated_conditions order = self.acts_enumerated_order unscoped.where(conditions).order(order) end
—Private methods—
Source
# File lib/power_enum/enumerated.rb, line 340 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
Looks up the enum based on the type of the argument.
Source
# File lib/power_enum/enumerated.rb, line 423 def raise_record_not_found(arg) raise ActiveRecord::RecordNotFound, "Couldn't find a #{self.name} identified by (#{arg.inspect})" end
raise the {ActiveRecord::RecordNotFound} error. @private