Use an ActiveRecord integer field as a bitfield, so that flags from a pre-defined list can be set, queried and unset. Useful for example for an authorization field that defines varies capabilities. A 32-bit integer field can contain up to 31 flags, and only flags from the pre-defined list can be set or unset.
Assuming that you have an ActiveRecord model with an integer field named “authz”, which should be defined with :default => 0, :null => false
class Member < ActiveRecord::Base include BitfieldList bitfield :authz, %w{member_moderator content_moderator admin} end
Given member = Member.create You can set the field to a single flag with =(String) member.authz = “member_moderator” Querying the field always returns an array member.authz #=> [“member_moderator”] Set the field to multiple flags with an array member.authz = [“member_moderator”, “content_moderator”] Add flags to the field with fieldname_add, which takes a String or Array argument member.authz_add “admin” member.authz #=> [“member_moderator”, “content_moderator”, “admin”] Remove flags from the field with fieldname_remove, which takes a String or Array argument member.authz_remove [“member_moderator”, “content_moderator”] member.authz #=> [“admin”] Query whether a given flag is set member.authz_set? “admin” #=> true member.authz_set? “content_moderator” #=> false
Get a list of available flags for the field in question member.authz_flaglist #=> [“member_moderator”, “content_moderator”, “admin”]
git clone git://github.com/victorgrey/bitfield_list.git cd bitfield_list gem build bitfield_list.gemspec (sudo) gem install bitfield_list-0.1.0.gem
…or, in Rails 3 you can just add this to your Gemfile gem ‘bitfield_list’, :git => ‘git://github.com/victorgrey/bitfield_list.git’
Copyright © 2010 Victor Grey. See LICENSE for details.