Module: PowerEnum::Enumerated::EnumInstanceMethods

Defined in:
lib/power_enum/enumerated.rb

Overview

These are instance methods for objects which are enums.

Instance Method Summary collapse

Instance Method Details

#===(arg) ⇒ Object Also known as: like?

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[:foo] == BookingStatus[arg].
  • 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].



448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/power_enum/enumerated.rb', line 448

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

#active?Boolean

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.

Returns:

  • (Boolean)


486
487
488
# File 'lib/power_enum/enumerated.rb', line 486

def active?
  @_active_status ||= ( attributes.include?('active') ? !!self.active : true )
end

#in?(*list) ⇒ Boolean

Returns true if any element in the list returns true for ===(arg), false otherwise.

Returns:

  • (Boolean)


464
465
466
467
468
469
# File 'lib/power_enum/enumerated.rb', line 464

def in?(*list)
  for item in list
    self === item and return true
  end
  false
end

#inactive?Boolean

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.

Returns:

  • (Boolean)


492
493
494
# File 'lib/power_enum/enumerated.rb', line 492

def inactive?
  !active?
end

#name_symObject Also known as: to_sym

Returns the symbol representation of the name of the enum. BookingStatus.name_sym returns :foo.



472
473
474
# File 'lib/power_enum/enumerated.rb', line 472

def name_sym
  self.__enum_name__.to_sym
end

#to_sObject

By default enumeration #to_s should return stringified name of the enum. BookingStatus.to_s returns "foo"



479
480
481
# File 'lib/power_enum/enumerated.rb', line 479

def to_s
  self.__enum_name__
end