module PowerEnum::Enumerated::EnumInstanceMethods
These are instance methods for objects which are enums.
Public Instance Methods
Behavior depends on the type of arg
.
-
If
arg
isnil
, returnsfalse
. -
If
arg
is an instance ofSymbol
,Integer
orString
, returns the result of +BookingStatus == BookingStatus+. -
If
arg
is anArray
, returnstrue
if any member of the array returnstrue
for +===(arg)+,false
otherwise. -
In all other cases, delegates to +===(arg)+ of the superclass.
Examples:
BookingStatus[:foo] === :foo #Returns true BookingStatus[:foo] === 'foo' #Returns true BookingStatus[:foo] === :bar #Returns false BookingStatus[:foo] === [:foo, :bar, :baz] #Returns true BookingStatus[:foo] === nil #Returns false
You should note that defining an :on_lookup_failure
method that raises an exception will cause +===+ to also raise an exception for any lookup failure of BookingStatus[arg]
.
# File lib/power_enum/enumerated.rb, line 446 def ===(arg) case arg when nil false when Symbol, String, Integer return self == self.class[arg] when Array return self.in?(*arg) else super end end
Returns true if the instance is active, false otherwise. If it has an attribute ‘active’, returns the attribute cast to a boolean, otherwise returns true. This method is used by the ‘active’ class method to select active enums.
# File lib/power_enum/enumerated.rb, line 484 def active? @_active_status ||= ( attributes.include?('active') ? !!self.active : true ) end
Returns true if any element in the list returns true for ===(arg), false otherwise.
# File lib/power_enum/enumerated.rb, line 462 def in?(*list) for item in list self === item and return true end false end
Returns true if the instance is inactive, false otherwise. Default implementations returns !active? This method is used by the ‘inactive’ class method to select inactive enums.
# File lib/power_enum/enumerated.rb, line 490 def inactive? !active? end
Returns the symbol representation of the name of the enum. BookingStatus.name_sym returns :foo.
# File lib/power_enum/enumerated.rb, line 470 def name_sym self.__enum_name__.to_sym end
By default enumeration to_s
should return stringified name of the enum. BookingStatus.to_s returns “foo”
# File lib/power_enum/enumerated.rb, line 477 def to_s self.__enum_name__ end
Private Instance Methods
NOTE: updating the models that back an acts_as_enumerated is rather dangerous because of rails’ per-process model. The cached values could get out of synch between processes and rather than completely disallow changes I make you jump through an extra hoop just in case you’re defining your enumeration values in Migrations. I.e. set enumeration_model_updates_permitted = true
# File lib/power_enum/enumerated.rb, line 500 def enumeration_model_update if self.class.enumeration_model_updates_permitted self.class.purge_enumerations_cache true else # Ugh. This just seems hack-ish. I wonder if there's a better way. if Rails.version =~ /^4\.2\.*/ false else throw(:abort) end end end
Validates that model updates are enabled.
# File lib/power_enum/enumerated.rb, line 515 def validate_enumeration_model_updates_permitted unless self.class.enumeration_model_updates_permitted self.errors.add(self.class.name_column, "changes to acts_as_enumeration model instances are not permitted") end end