Class: Enum
- Inherits:
-
Object
- Object
- Enum
- Defined in:
- enum/enum.rb
Overview
The Enum class allows for better handling of the states within a given scenario. This is often inherited when introducing different enums.
Direct Known Subclasses
Class Method Summary collapse
-
.enum_attr(name, num) ⇒ NilClass
Create an enum attribute and create methods for each attribute.
Instance Method Summary collapse
-
#initialize(attrs = 0) ⇒ NilClass
constructor
Initialize the given attribute.
-
#to_i ⇒ Integer
Convert the value of the attribute on integer.
Constructor Details
#initialize(attrs = 0) ⇒ NilClass
Initialize the given attribute. 0 is assigned if nil args.
43 44 45 |
# File 'enum/enum.rb', line 43 def initialize(attrs = 0) @attrs = attrs end |
Class Method Details
.enum_attr(name, num) ⇒ NilClass
Create an enum attribute and create methods for each attribute. Each attribute can be checked whether it is true for that instance of the attribute. Additionally they can be changed to another type of attribute.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'enum/enum.rb', line 19 def self.enum_attr(name, num) name = name.to_s define_method(name + '?') do @attrs & num != 0 end define_method(name + '=') do |set| if set @attrs |= num else @attrs &= ~num end end end |
Instance Method Details
#to_i ⇒ Integer
Convert the value of the attribute on integer.
52 53 54 |
# File 'enum/enum.rb', line 52 def to_i @attrs end |