typed-enum

A TypedEnum offers an enumeration facility allowing for the expression of a constrained set of (enumerated) values of a user-defined class, akin to Java’s concept of enum. It supports listing the available enumerated values and serializing/deserializing instances to/from string representations. It is not thread safe (though it could be made so relatively easily).

Basic Usage

A TypedEnum can be used to represent a typed, enumerated set of values. A TypedEnum offers the following features:

  • Instances are restricted (in the absence of class-opening) to the set of instances defined in the class definition

  • Available values can be enumerated in a consistent order via a call to the #values class method

  • Instances can be retrieved via class constants

  • Instances can be retrieved by parsing a string/symbol

  • Instances can be serialized to a string

Basic usage example:

class Color
  @enum = lambda {[
    RED   = self.new(:red),
    GREEN = self.new(:green),
    BLUE  = self.new(:blue)
  ]}
  include TypedEnum
end

Color::RED => #<Color:0x163d59f8 @name=:red>
Color.from_name(:red) => #<Color:0x163d59f8 @name=:red>
Color.from_name('red') => #<Color:0x163d59f8 @name=:red>
Color::RED.name => :red
Color.values => [#<Color:0x163d59f8 @name=:red>, #<Color:0x163d59a8 @name=:green>, #<Color:0x163d5980 @name=:blue>]

Contributing to typed-enum

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Chris Tucker. See LICENSE.txt for further details.