Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dumpable plugin for specific control over marshalling. Add specs …
…to cover #330.
- Loading branch information
Showing
5 changed files
with
50 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |