Sha256: 8022ce1a15f9fe1e0421416a27d5d3d80cede7727c28c5814ff5c12b52fafed5

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

module ClassyEnum

  class Base

    # 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
    #
    def self.enum_classes(*options)
      self.const_set("OPTIONS", options) unless self.const_defined? "OPTIONS"

      self.extend ClassyEnum::ClassMethods
      self.send(:include, ClassyEnum::InstanceMethods)
      self.send(:include, Comparable)

      options.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?)
          options.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
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
classy_enum-1.0.0 lib/classy_enum/base.rb
classy_enum-0.9.1 lib/classy_enum/base.rb
classy_enum-0.8.1 lib/classy_enum/base.rb
classy_enum-0.8.0 lib/classy_enum/base.rb
classy_enum-0.7.1 lib/classy_enum/base.rb