Garbage Burrito!

An internet web blog by Ben Kittrell

Bitchin: Easy Key/Value Fields for Rails Using Bit Fields or Hashes

Bitchin: Easy Key/Value Fields for Rails Using Bit Fields or Hashes
Ben Kittrell - 06/06/2010 02:38:00
Comments: 0
Document databases are definitely hot right now, and for good reason.  I've thought relational databases were wasted on most web apps for a long time and I'm seriously considering switching Doodlekit to something like MongoDB.

In the meantime (ie. while I get my ass caught up) I wrote a simple plugin to make it super easy to add properties to your ActiveRecord models without needing table columns for all of them.

Nothing earth-shattering, just a cool little plugin I wanted to share.

I call it Bitchin.

There are two parts to it, Bitchin and Hashed.  The only difference is that Bitchin uses a Bit Field so it can only store boolean fields which means it's much faster.  As long as you have an Integer called bit_flag in your table you just need to do this...

class User < ActiveRecord::Base
 
  bitchin :active, :banned, :stupid


And the accessors are automatically created for you...

user.stupid = true

user.stupid? #=> true
user.stupid #=> true
Hashed works the same way but it relies on ActiveRecord's built-in hash serialization so you can basically store any scalar value.

More about configuration options and how to use in the README.

This plugin is under the Do Whatever The Fuck You Want With It License (WTFYWL)

Comments: 0

Post a Comment


Please enter the word below.


powered by Doodlekit™ Free Website Builder by Doodlebit™ Website Company