Marshalize¶ ↑
This Rails plugin provides serialization using Marshal[http://ruby-doc.org/core/classes/Marshal.html] 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.
Example¶ ↑
$ 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
Installation¶ ↑
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:¶ ↑
If necessary:
gem sources -a http://gems.github.com
Then:
sudo gem install chikamichi-marshalize
Testing¶ ↑
A very simple test case is provided as test/marshalize_test.rb
. Run rake test
.
TODO¶ ↑
-
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?)
License¶ ↑
Released under the WTFPL (see COPYING).