Yeah how are you getting it into the db? Are you using serialize :foo for one of your columns in the db? That might be the easiest way. When you use a serialize statement in one of your models the object you want to store in the column gets serialized to yaml before going in and transparently gets dehydrated back into an object when it comes out.
The main limitation of this method of doing things is that your object needs to be YAML dumpable. This means no procs or lamdas. You could try to create on of your runt objects in irb or something and then dump it to yaml and reload it to see if it survives the translation. Like this:
Code:
require 'yaml'
class Foo
attr_accessor :bar
attr_accessor :qux
def initialize(bar, qux)
@bar, @qux = bar, qux
end
end
a = Foo.new("hello", "world")
p a # => #<Foo:0x219f00 @qux="world", @bar="hello">
b = YAML.load(YAML.dump a)
p b # => #<Foo:0x444408 @qux="world", @bar="hello">
But if you try to add a proc or lamda in the mix it will not work:
Code:
require 'yaml'
class Foo
attr_accessor :bar
attr_accessor :qux
def initialize(bar, qux)
@bar, @qux = bar, lambda { qux.reverse.upcase }
end
end
a = Foo.new("hello", "world")
p a # #<Foo:0x4160bc @qux=#<Proc:0x0041962c@(irb):38>, @bar="hello">
a.qux.call # => "DLROW"
b = YAML.load(YAML.dump a)
TypeError: allocator undefined for Proc
from /usr/lib/ruby/1.8/yaml.rb:347:in `allocate'
from /usr/lib/ruby/1.8/yaml.rb:347:in `object_maker'
from /usr/lib/ruby/1.8/yaml/rubytypes.rb:36
from /usr/lib/ruby/1.8/yaml/rubytypes.rb:34:in `call'
from (irb):46:in `transfer'
from /usr/lib/ruby/1.8/yaml.rb:119:in `load'
from /usr/lib/ruby/1.8/yaml.rb:119:in `load'
from (irb):46
Hope that helps some.
-Ezra
Bookmarks