Dy-na-bix, tasty serialization attribute accessors for ActiveRecord
Data serialization is a technique that can be used to persist data to the database without changing the schema when adding or removing attributes. A single text field can contain multiple attributes. Serialization is useful for one-off situations like voting polls or frequently changing on-line questionnaires. Dynabix uses ActiveRecord's serialize method under the hood.
ActiveRecord as of 3.2.1, as pointed out in the comments, has a very similar native method store. Dynabix differs from store by providing a declarative DSL for defining multiple stores (Ruby 1.9+), has separate read/write accessors, and stores to the database as HashWithIndifferentAccess. Unless you need one of these specific features, using the native 'store' method is recommended.
Add a text column "metadata" to your model migration. This column will store all the attribute values defined by Dynabix.
class AddMetadataToThings < ActiveRecord::Migration def change add_column :things, :metadata, :text end end
Add accessors to your model using the default column name ":metadata", specify the attributes in a separate step.
class Thing < ActiveRecord::Base has_metadata # full accessors metadata_accessor :breakfast_food, :wheat_products, :needs_milk # read-only accessor metadata_reader :friends_with_spoons end
Specifying attributes for full attribute accessors in one step
class Thing < ActiveRecord::Base has_metadata :metadata, :breakfast_food, :wheat_products, :needs_milk end
Using the new accessors
thing = Thing.new thing.breakfast_food = 'a wheat like cereal" # same thing, but using the metadata hash directly thing.metadata[:breakfast_food] = 'a wheat like cereal"
Dynabix under Ruby 1.9+ enables specifying multiple metadata columns on a model. You are not limited to using the static "metadata" column.
Add text columns "cows" and "chickens" to your "thing" model migration
class AddMetadataToThings < ActiveRecord::Migration def change add_column :things, :cows, :text add_column :things, :chickens, :text end end
Specifying multiple metadata serializers to segregate like data into separate database columns (Ruby 1.9 only)
class Thing < ActiveRecord::Base has_metadata :cows has_metadata :chickens, :tasty, :feather_count # read-only cows_reader :likes_milk, :hates_eggs # write-only cows_writer :no_wheat_products # extra full accessors for chickens chickens_accessor :color, :likes_eggs, :egg_count end
Using the new accessors
thing = Thing.new # cow stuff thing.no_wheat_products = true # chicken stuff thing.likes_eggs = true thing.egg_count = 12 # using the metadata hash directly to read the data since # we only created a write accessor thing.cows[:no_wheat_products].should be_true
Add Dynabix to your Gemfile
Install the gem with Bundler
Get the source
cd workspace git clone https://github.com/robertwahler/dynabix.git cd dynabix
Install the dependencies
Run the specs
bundle exec rake spec
Autotest with Guard
article comments powered by Disqus
bundle exec guard