EnumClass

This gem is an attempt to provide a lightweight solution for object-oriented enumerations in Ruby. It consists in a single class (Enum), that can be subclassed to create enumerations and comes with no dependencies. It's similar in design to some other gems available, but it stemmed out from the necessity of handling reliably missing features like inheritance and instance methods of the enumerated object.

Installation

Add this line to your application's Gemfile:

gem 'enum_class'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enum_class

Usage

Basic usage

class Foo < Enum
  value :A
  value :B
end

Foo.inspect  # => Foo(A, B)

# Instances are accessible as constants
Foo::A.class # => Foo
Foo::B.class # => Foo
Foo::C       # => raises NameError

# Enumerated values are implicitly ordered, depending on order of definition
Foo::A == Foo::B # => false
Foo::A < Foo::B  # => true

# Instances can be retrieved by key
Foo[:a]      # => Foo::A
Foo['a']     # => Foo::A
Foo.for('A') # => Foo::A
Foo.for('X') # => raises KeyError

Foo.new      # => raises an error

# Helper methods
Foo.a        # => Foo::A
Foo.b        # => Foo::B

Foo::A.a?    # => true
Foo::A.b?    # => false

Creating enums with a shortcut

Bar = Enum.create(:a, :b, :c) # => Bar(A, B, C)

Custom initializer

class Foobar < Enum
  def initialize(name)
    @name = name
  end

  attr_reader :name

  value :A, 'foo'
  value :B, 'bar'
end

Foobar::A.name # => 'foo'
Foobar::B.name # => 'bar'

Inheritance

class Base < Enum
  value :A
  value :B
end

class Child < Base
  value :C
end

Base.inspect  # => Base(A, B)
Child.inspect # => Child(A, B, C)

:warning: If you reopen the base class later, the changes won't be reflected in the child. The values to inherit are evaluated at the time of the class definition.

Advanced inheritance with custom initializer

class Base < Enum
  def initialize(name)
    @name = name
  end

  attr_reader :name

  value :A, 'foo'
  value :B, 'bar'
end

class Child < Base
  def initialize(name, number)
    super(name)
    @number = number
  end

  def initialize_from_superclass(superclass_enum_value)
    super # default behavior is to copy all the instance variables
    @number = superclass_enum_value.ord + 1
  end

  attr_reader :number

  value :C, 'baz', 3
end

Child::C.name   # => 'baz'
Child::C.number # => 3

Child::B.name   # => 'bar'
Child::B.number # => 2

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/LIQIDTechnology/enum_class. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.