This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
Amiel Martin (author)
Mon Dec 14 12:01:10 -0800 2009
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Fri Oct 09 11:09:55 -0700 2009 | |
| |
README.rdoc | Fri Oct 16 15:28:16 -0700 2009 | |
| |
Rakefile | Fri Oct 09 10:14:14 -0700 2009 | |
| |
init.rb | Fri Dec 11 16:37:05 -0800 2009 | |
| |
lib/ | Mon Dec 14 12:01:10 -0800 2009 | |
| |
test/ | Fri Dec 11 16:57:53 -0800 2009 |
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







