sutonnie/enumancer

Easy to use typed enums for Ruby.

★ 0Forks 0RubyGitHub ↗Compare

README

Enumancer

Gem Version   License: BSD-3-Clause

Enumancer provides a declarative, type-safe registry for named values in Ruby.
Each entry is unique by both name and value. Optional type constraints and strict mode are supported.
Designed for predictable access, JSON serialization, and clean integration into Ruby applications.


Installation

Add this line to your Gemfile:

gem 'enumancer'

Then install:

bundle install

Or install it directly:

gem install enumancer

Usage

class Status < Enumancer::Enum
  type Integer, strict: true

  entry :draft, 0
  entry :published, 1
  entry :archived, 2
end

Status[:draft].value      # => 0
Status.published.to_sym   # => :published
Status.from_json('{"name":"archived"}') # => Status.archived

Serialization

Status.draft.to_json
# => '{"name":"draft","value":0}'

Deserialization

Status.from_json('{"name":"published"}')
# => Status.published

Contributors

sutonnie

Issues