has_bitfield
A rails gem for easy bitfield support.
Has support form multiple bitfield columns. See github.com/aka47/has_flags for a gem with a different feature set.
Install
gem install has_bitfield
Usage
class MyClass << ActiveRecord::Base
has_bitfield :column_name,
1 => :flag1,
2 => :flag2,
3 => :flag3
end
Attribute Accessors
readers: name, name?
writer: name=(v)
where 'name' is the name of the bit field. the (v) parameter can be true/false, "true"/"false", 0/1, 'yes'/'no',
or :yes/:no. specifically, the following (v) inputs evaluate to true:
[ true, 'true', 'yes', :yes, 'ok', :ok, 1, '1' ]
all others, including nil evaluate false.
groups_name = [Array of flags] , not the plural
groups_name << name
groups_name
group_ids
group_ids = [Array of flag-ids]
The database table must include the column ‘column_name’ as an integer.
Defaults are set in per column ‘after_initialized’ callbacks. If your model needs to use this callback, define it in the class and invoke: initialize_column_name.
class MyClass << ActiveRecord::Base
has_bitfield :my_flags,
1 => {:flag1 => { :default => false }},
2 => :flag2,
3 => :flag3
after_initialize :initialize_my_flags
end
Note on Patches/Pull Requests
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright
Copyright © 2009 Hugo Duncan. See LICENSE for details.