Project

enum_set

0.0
No commit activity in last 3 years
No release in over 3 years
Allows using a single integer column as a set of booleans, with bitfield-style storage.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0
>= 0
 Project Readme

Build Status

EnumSet

enum_set is a gem for treating integer columns as a compact set of boolean fields. It defines boolean getter methods similar to the standard enum method as well as an array setter.

Installation

  • With bundler:

      $ gem 'enum_set'
    
  • Without bundler:

      $ gem install enum_set
    

Usage

Array Specification

# user.rb
class User < ActiveRecord::Base
  include EnumSet
  enum_set roles: [:admin, :super_user, :kaiser]
end

Hash Specification

# user.rb
class User < ActiveRecord::Base
  include EnumSet
  # N.B.: values must be powers of 2
  enum_set roles: { admin: 1, super_user: 4, kaiser: 256 }
end

Elsewhere

user = User.create(roles: [:super_user])

user.super_user? # => true
user.admin? # => false

super_users = User.super_user.all # will include `user`

user.roles <<= :kaiser # Adds `:kaiser` role to `user`
user.roles # => [:super_user, :kaiser]
user.roles <<= :gender # raises `EnumSet::EnumError`, since `:gender` isn't a role

Notes

The gem currently requires ActiveRecord due to scope creation.