This Rails plugin provides serialization using Marshal in the same way Rails provides builtin serialization using YAML. In a model, just do
class YourModel < ActiveRecord::Base marshalize :attribute end
and your app will automagically handle the attribute using the binary converter Marshal: dumping when saving to the database, loading when fetching from it.
You can store any kind object (not just arrays and hashes…). Be aware that Marshal defines a binary format which may change in incoming Ruby releases and is currently not portable outside the Ruby world. For portable yet slower alternatives, you may try JSON or XML serializers.
$ script/generate scaffold Robot name:string parameters:text features:text status:text class Robot < ActiveRecord::Base marshalize :parameters # The "parameters" attribute will be saved as binary data marshalize :features, RobotFeature # You can define what kind of object is to be marshalized # An error will be raised if another class is provided for # this attribute serialize :status, Array # Marshalization plays well with YAML classic serialization end
recommended – as a local gem dependency:
In your environment.rb, inside the Rails::Initializer, add the following line:
config.gem "chikamichi-marshalize", :lib => 'marshalize', :source => 'http://gems.github.com'
Then run rake gems:install to fetch the plugin in.
as a plugin:
In your Rails application root directory:
script/plugin install git://github.com/chikamichi/marshalize.git
as an standalone gem:
gem sources -a http://gems.github.com
sudo gem install chikamichi-marshalize
A very simple test case is provided as test/marshalize_test.rb. Run rake test.
make base64 encoding optionnal for binary attributes (blob), mandatory for text attributes
some super stuff to avoid harsh overriding of ActiveRecord internals
a method to fetch raw binary and/or encoded data from the db, bypassing unmarshalizing?
rake tasks to manage marshalized data recovering in case Marshal went astray (say, a new version breaking things down?)
Released under the WTFPL (see COPYING).