-
-
Save ahoward/5066528 to your computer and use it in GitHub Desktop.
it's easy to make your models marshal mo-betta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# some models have whack shit that can't survive a marshal round-trip | |
# | |
class Model | |
include Mongoid::Document | |
def initialize(*args, &block) | |
super | |
ensure | |
@fail = Class.new, open(__FILE__) | |
end | |
end | |
# so this'll fail | |
# | |
begin | |
p Marshal.load(Marshal.dump(Model.create)) | |
rescue Object => o | |
warn "#{ o.message } (#{ o.class })" | |
end | |
# but mongoid models simply need a hash of information from the mongo driver | |
# to fully vivify themselves... ergo this is all we need persist when | |
# marshaled. this makes loading from marshaled data *just like* loading from | |
# the db. | |
# | |
# if you ask me this should be the default behavior! | |
# | |
# hrm - i am on mongoid core... @durran, what do you think? | |
# | |
# btw - this works just fine with active_record too... | |
# | |
class Model | |
def _dump(*args, &block) | |
Marshal.dump(raw_attributes, *args, &block) | |
end | |
def Model._load(string, *args, &block) | |
raw_attributes = Marshal.load(string, *args, &block) | |
instantiate(raw_attributes) | |
end | |
end | |
# so now it just werks (TM) | |
# | |
p Marshal.load(Marshal.dump(Model.create)) | |
BEGIN { | |
require 'rubygems' | |
require 'mongoid' | |
Mongoid.configure{|config| config.connect_to('mongoid-marshal')} | |
} | |
__END__ | |
teh outputz: | |
can't dump anonymous class #<Class:0x007fa89dc23768> (TypeError) | |
#<Model _id: 5130edd0af481ccd3d000002, > | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment