public
Description: Bitmask Attributes allows you to store many boolean values as one integer field in the database.
Homepage:
Clone URL: git://github.com/amiel/bitmask_attributes.git
name age message
file MIT-LICENSE Fri Oct 09 11:09:55 -0700 2009 I got tests to pass [amiel]
file README.rdoc Fri Oct 16 15:28:16 -0700 2009 fix reload bug [amiel]
file Rakefile Fri Oct 09 10:14:14 -0700 2009 initial import, still need to fix tests [amiel]
file init.rb Fri Dec 11 16:37:05 -0800 2009 add support for array assignment and formtastic [Amiel Martin]
directory lib/ Mon Dec 14 12:01:10 -0800 2009 provide options for all bitmask attributes as a... [Amiel Martin]
directory test/ Fri Dec 11 16:57:53 -0800 2009 fix failure when array assignment was passed em... [Amiel Martin]
README.rdoc

BitmaskAttributes

Bitmask Attributes allows you to store many boolean values as one integer field in the database.

Synopsis

        class User < ActiveRecord::Base
          has_bitmask_attributes :notifications do |config|
            config.attribute :weekly_newsletter,    0b0001
            config.attribute :monthly_newsletter,   0b0010, true
          end
        end

RDOC

Can be found at rdoc.info: rdoc.info/projects/amiel/bitmask_attributes

Examples

  # in migration
  t.integer :notifications_mask

  # in app/models/user.rb
  class User < ActiveRecord::Base
      has_bitmask_attributes :notifications do |config|
          config.method_format 'send_%s'
          config.attribute :weekly_newsletter,    0b0001
          config.attribute :monthly_newsletter,   0b0010, true
          config.accessible
          # config.field_name :notifications_mask # <- default functionality
      end
  end

this will define 8 functions:

  • User#notifications — returns a Bitmask object representing all values (see rdocs for more information on the Bitmask object)
  • User#send_weekly_newsletter? — predicate
  • User#send_weekly_newsletter — works just like the predicate, makes it easy to use actionview form helpers
  • User#send_weekly_newsletter=(value) — just give it a boolean value (also takes "0" and "1" or "t" and "f" just like activerecord does for boolean fields)
  • User#send_monthly_newsletter?
  • User#send_monthly_newsletter
  • User#send_monthly_newsletter=(value)
  • User.notifications_mask — a hash of the masks. ie: { :weekly_newsletter => 1, :monthly_newsletter => 2 }

the call to config.accessible calls attr_accessible :send_weekly_newsletter, :send_monthly_newsletter in your model

View Example

        # in your view
        <% form_for @user do |f| %>
                Monthly Newsletter: <%= f.check_box :send_monthly_newsletter? %>
                or
                Monthly Newsletter
                Yes: <%= f.radio_button :send_monthly_newsletter, 'true' %>
                No: <%= f.radio_button :send_monthly_newsletter, 'false' %>
        <% end %>

Config Options

config.attribute(name, mask, default = false)

Sets up a binary attribute. Defines three functions: name, name?, and name=(true|false)

  • name a symbol, BitmaskAttributes will define
  • mask example: 0b0000001, must be a power of 2
  • default set to true if you want the attribute to default to true

config.method_format(format)

Useful for making method calls more natural.

  • format a printf style format string to create function names

config.accessible

if you are using attr_accessible in your model and you want to mass-assign your bitmask attributes, you will want to call this

config.field_name(name)

  • name name of the field name in the database where all this info is stored, should be an integer

Copyright © 2009 Amiel Martin, released under the MIT license