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].



482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/power_enum/enumerated.rb', line 482

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)


520
521
522
# File 'lib/power_enum/enumerated.rb', line 520

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)


498
499
500
501
502
503
# File 'lib/power_enum/enumerated.rb', line 498

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)


526
527
528
# File 'lib/power_enum/enumerated.rb', line 526

def inactive?
  !active?
end

#name_sym ⇒ Object Also known as: to_sym

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



506
507
508
# File 'lib/power_enum/enumerated.rb', line 506

def name_sym
  self.__enum_name__.to_sym
end

#to_s ⇒ Object

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



513
514
515
# File 'lib/power_enum/enumerated.rb', line 513

def to_s
  self.__enum_name__
end