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
-
#===(arg) ⇒ Object
(also: #like?)
Behavior depends on the type of
arg. -
#active? ⇒ Boolean
Returns true if the instance is active, false otherwise.
-
#in?(*list) ⇒ Boolean
Returns true if any element in the list returns true for ===(arg), false otherwise.
-
#inactive? ⇒ Boolean
Returns true if the instance is inactive, false otherwise.
-
#name_sym ⇒ Object
(also: #to_sym)
Returns the symbol representation of the name of the enum.
-
#to_s ⇒ Object
By default enumeration #to_s should return stringified name of the enum.
Instance Method Details
#===(arg) ⇒ Object Also known as: like?
Behavior depends on the type of arg.
- If
argisnil, returnsfalse. - If
argis an instance ofSymbol,IntegerorString, returns the result ofBookingStatus[:foo] == BookingStatus[arg]. - If
argis anArray, returnstrueif any member of the array returnstruefor===(arg),falseotherwise. - 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.
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.
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.
492 493 494 |
# File 'lib/power_enum/enumerated.rb', line 492 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.
472 473 474 |
# File 'lib/power_enum/enumerated.rb', line 472 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"
479 480 481 |
# File 'lib/power_enum/enumerated.rb', line 479 def to_s self.__enum_name__ end |