Class: ClassyEnum::Base
- Inherits:
-
Object
- Object
- ClassyEnum::Base
- Defined in:
- lib/classy_enum/base.rb
Class Method Summary collapse
-
.enum_classes(*options) ⇒ Object
Macro for defining enum members within a ClassyEnum class.
Class Method Details
.enum_classes(*options) ⇒ Object
Macro for defining enum members within a ClassyEnum class. Accepts an array of symbols or strings which are converted to ClassyEnum members as descents of their parent class.
Example
# Define an enum called Priority with three child classes
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
end
The child classes will be defined with the following constants:
PriorityLow, PriorityMedium, and PriorityHigh
These child classes can be instantiated with either:
Priority.build(:low) or PriorityLow.new
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/classy_enum/base.rb', line 21 def self.enum_classes(*) self.const_set("OPTIONS", ) unless self.const_defined? "OPTIONS" self.extend ClassyEnum::ClassMethods self.send(:include, ClassyEnum::InstanceMethods) self.send(:include, Comparable) .each_with_index do |option, index| klass = Class.new(self) do @index = index + 1 @option = option def initialize @to_s = self.class.instance_variable_get('@option').to_s @index = self.class.instance_variable_get('@index') end # Define methods to test member type (ie member.option?) .each do |o| self.send(:define_method, "#{o}?", lambda { o == option }) end end klass_name = "#{self}#{option.to_s.camelize}" Object.const_set(klass_name, klass) unless Object.const_defined? klass_name end end |