module PowerEnum::Enumerated::EnumInstanceMethods

These are instance methods for objects which are enums.

Public Instance Methods

===(arg) click to toggle source

Behavior depends on the type of arg.

  • If arg is nil, returns false.

  • If arg is an instance of Symbol, Integer or String, returns the result of +BookingStatus == BookingStatus+.

  • If arg is an Array, returns true if any member of the array returns true 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].

Calls superclass method
# 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
Also aliased as: like?
active?() click to toggle source

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
in?(*list) click to toggle source

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
inactive?() click to toggle source

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
like?(arg)
Alias for: ===
name_sym() click to toggle source

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
Also aliased as: to_sym
to_s() click to toggle source

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
to_sym()
Alias for: name_sym

Private Instance Methods

enumeration_model_update() click to toggle source

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
validate_enumeration_model_updates_permitted() click to toggle source

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