Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Acts As Bytefield

Acts As Bytefield

= ActsAsBytefield

A Rails plugin to turn a string column into a bytefield. Inspired by Gabriel Gironda’s acts_as_bitfield: http://gabriel.gironda.org/articles/2005/12/08/bitfields-in-activerecord

= Overview

We needed a convenient way to store a series of values in a single field but a bitfield would not do because that would only allow a value of 1 or 0 for each field, and I wanted to be able to store at least 3 values for each field.

So I found Gabriel Gironda’s acts_as_bitfield plugin and made a few tweaks. This plugin, acts_as_bytefield, is the result. It allows storage of 256 values in each field, or 256 discrete values ranging from 0-255 (unsigned char or byte) for each value in a MySQL varchar(255) field.

Values greater than 255, or less than 0 wrap around. For example, setting a bytefield column to -1 will actually set it to 255, and setting it to -2 will actually set it to 254, etc.

= Installation

  • script/plugin install acts_as_bytefield OR
  • script/plugin install https://svn.cbciweb.com/svn/plugins/acts_as_bytefield
Documentation (RDoc)
  • rake rdoc
Testing The tests require rspec[http://rspec.rubyforge.org/]
  • rake test

= Usage

  • Create a string column in your table – varchar(255)
  • Add this directive to your model:

    acts_as_bytefield :bytefield_column_name, :fields => [:field_name_one, :field_name_two]

You will then be able to use the model in the following manner, for example:

class SomeModel < ActiveRecord::Base
  acts_as_bytefield :bfield, :fields => [:test, :production]
end
obj = SomeModel.new(:test => 1)
obj.test          #=> 1
obj.test?         #=> 1
obj.production?   #=> 0
obj.test = 0
obj.production = 65
obj.save
obj.test?         #=> 0
obj.production?   #=> 65
  1. The field that’s storing the value: obj.bfield #=> ”\000A”

= Contact

Get in touch, with any bugs or feedback at mailto:plugins@cbciweb.com. http://blog.cbciweb.com/

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all details


Membership

+ Join this railsplugin

Record Maintainer

'None'