Skip to content

Commit

Permalink
Add Dumpable plugin for specific control over marshalling. Add specs …
Browse files Browse the repository at this point in the history
…to cover #330.
  • Loading branch information
cheald committed Jul 5, 2013
1 parent 4f829f0 commit 520cc65
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/mongo_mapper.rb
Expand Up @@ -36,6 +36,7 @@ module Plugins
autoload :Dirty, 'mongo_mapper/plugins/dirty'
autoload :Document, 'mongo_mapper/plugins/document'
autoload :DynamicQuerying, 'mongo_mapper/plugins/dynamic_querying'
autoload :Dumpable, 'mongo_mapper/plugins/dumpable'
autoload :EmbeddedCallbacks, 'mongo_mapper/plugins/embedded_callbacks'
autoload :EmbeddedDocument, 'mongo_mapper/plugins/embedded_document'
autoload :Equality, 'mongo_mapper/plugins/equality'
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_mapper/document.rb
Expand Up @@ -6,6 +6,7 @@ module Document

include Plugins::ActiveModel
include Plugins::Document
include Plugins::Dumpable
include Plugins::Querying # for now needs to be before associations (save_to_collection)
include Plugins::Associations
include Plugins::Caching
Expand Down
22 changes: 22 additions & 0 deletions lib/mongo_mapper/plugins/dumpable.rb
@@ -0,0 +1,22 @@
require 'set'

module MongoMapper
module Plugins
module Dumpable
DUMP_BLACKLIST = Set.new([:@_mm_default_keys, :@_mm_keys])

def marshal_dump
instance_variables.inject({}) do |h, var|
h[var] = instance_variable_get(var) unless DUMP_BLACKLIST.include?(var)
h
end
end

def marshal_load(data)
data.each do |k, v|
instance_variable_set(k, v)
end
end
end
end
end
7 changes: 2 additions & 5 deletions lib/mongo_mapper/plugins/keys.rb
Expand Up @@ -297,8 +297,6 @@ def load_from_database(attrs)
internal_write_key key, value, false
end
end

@_mm_keys = nil # If we don't nil this, it causes problems for Marshal#dump
end

def set_parent_document(key, value)
Expand Down Expand Up @@ -327,13 +325,12 @@ def internal_write_key(name, value, cast = true)
end

def initialize_default_values(except = {})
@default_keys ||= self.class.keys.values.select(&:default?)
@default_keys.each do |key|
@_mm_default_keys ||= self.class.keys.values.select(&:default?)
@_mm_default_keys.each do |key|
if !(except && except.key?(key.name))
internal_write_key key.name, key.default_value, false
end
end
@_mm_keys = nil # If we don't nil this, it causes problems for Marshal#dump
end
#end private
end
Expand Down
24 changes: 24 additions & 0 deletions spec/functional/dumpable_spec.rb
@@ -0,0 +1,24 @@
require 'spec_helper'
require 'active_support/cache/memory_store'

describe "Documents with the Dumpable plugin" do
let(:doc) { Doc { key :foo, String } }
let(:answer) { Answer.create(:body => "answer body") }
let(:store) { ActiveSupport::Cache::MemoryStore.new(:size => 1.megabyte) }

it "should be able to be marshalled" do
expect { Marshal.dump(answer) }.to_not raise_error
end

it "should be able to be unmarshalled" do
dumped = Marshal.dump(answer)
reconstituted_answer = Marshal.load(dumped)
reconstituted_answer.attributes.should == answer.attributes
end

it "should be able to be saved in a Rails cache" do
fetched = store.fetch("foo") { Answer.find(answer._id) }
fetched.body.should == "answer body"
fetched._id.should == answer._id
end
end

0 comments on commit 520cc65

Please sign in to comment.