Posted by admin Tue, 24 Apr 2007 02:58:00 GMT

We needed a convenient way to store a series of values in a single field. A bitfield would not do because it would only allow a value of 1 or 0 for each field, and we 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. ActsAsBytefield is the result. It allows storage of 256 values in each field, or 255 discrete values ranging from 0-255 (unsigned char or byte) for each value in a MySQL varchar(255) field.

NOTE: Gabriel’s site has been down for some time. My own repository has also been down but is now back up. Sorry for the inconvenience.

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

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

 # The field that's storing the value:
 obj.bfield        #=> "\000A"

Posted by admin Mon, 16 Apr 2007 23:09:00 GMT

Managing a distributed Rails application without Capistrano is painful at best. If you have ever had to do it you know what I am talking about.

Capistrano makes things much easier. Case in point is updating your gems across multiple servers. Here is the Capistrano recipe that we use to keep gems updated:

desc "Update ruby gems"
task :update_gems, :roles => [:web, :admin, :app] do
  cmd = "/usr/local/bin/gem update --include-dependencies"
  sudo cmd do |channel, stream, data|
    puts "#{channel[:host]}: #{data}"
    if data =~ /([12])\.[^\(]+\(ruby\)/
      channel.send_data "#{$1}\n"
    end
  end
end

Now, you’ll probably need to customize it to your own needs. For instance, change the roles to suit your own setup. Also, you may want to modify the path to your ‘gem’ executable, and configure ’sudo’ to allow your user to run that command without a password. You’ll have to do that on each server as follows:

sudo visudo
Then add a line like this:
myuser ALL=(ALL) NOPASSWD: /usr/local/bin/gem update --include-dependencies

If you don’t have access to ‘visudo’ ask your administrator.

Also, Win32 users will need to change the regular expression from this:

if data =~ /([12])\.[^\(]+\(ruby\)/

to this:

if data =~ /([12])\.[^\(]+\(mswin32\)/

… so Capistrano will choose the correct version of the gem to use.

Basically this recipe will run the ’gem update --include-dependencies’ command and analyze the output choosing the correct latest version of gem to install.

Older posts: 1 2 3 4