Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Marshal values for storage

  • Loading branch information...
commit e80fdee123991d59865182fde2b0b3e89bd131a4 1 parent 66377d7
@captainpete authored
Showing with 47 additions and 21 deletions.
  1. +10 −2 lib/campchair/leveldb.rb
  2. +37 −19 spec/campchair/leveldb_spec.rb
View
12 lib/campchair/leveldb.rb
@@ -16,6 +16,14 @@ def self.generate_key
(0...40).map { @alpha[rand(@alpha.size)] }.join
end
+ def self.encode(value)
+ Marshal.dump(value)
+ end
+
+ def self.decode(value)
+ Marshal.load(value)
+ end
+
def self.included(base)
base.class_eval do
def self.db=(db)
@@ -27,11 +35,11 @@ def self.db
end
def self.[](key)
- db[key]
+ Campchair::LevelDB.decode(db[key])
end
def self.[]=(key, value)
- db[key] = value
+ db[key] = Campchair::LevelDB.encode(value)
end
def self.<<(value)
View
56 spec/campchair/leveldb_spec.rb
@@ -65,32 +65,50 @@ class TestEntityCustomDB
end
end
- describe "#[]" do
- it "is nil when there's no matching key in the database" do
- TestEntity['nonexistent'].should be_nil
+ context "no data serialization" do
+ before do
+ # Passthrough {en,de}coding for these tests
+ Campchair::LevelDB.stub(:encode).and_return {|v| v}
+ Campchair::LevelDB.stub(:decode).and_return {|v| v}
end
- it "is the value of the matching key in the database" do
- @db['something'] = '22'
- TestEntity['something'].should == '22'
+ describe ".[]" do
+ it "is nil when there's no matching key in the database" do
+ TestEntity['nonexistent'].should be_nil
+ end
+
+ it "is the value of the matching key in the database" do
+ @db['something'] = '22'
+ TestEntity['something'].should == '22'
+ end
end
- end
- describe "#[]=" do
- it "sets the value of the key in the database" do
- TestEntity['something'] = '22'
- @db['something'].should == '22'
+ describe ".[]=" do
+ it "sets the value of the key in the database" do
+ TestEntity['something'] = '22'
+ @db['something'].should == '22'
+ end
+ end
+
+ describe ".<<" do
+ it "adds the value to the database, generating a key" do
+ key = 'b78aa2f6b718651743fac2682003f2c63340ae34b'
+ Campchair::LevelDB.stub! :generate_key => key
+
+ id = TestEntity << '22'
+ id.should == key
+ TestEntity[id].should == '22'
+ end
end
end
- describe "#<<" do
- it "adds the value to the database, generating a key" do
- key = 'b78aa2f6b718651743fac2682003f2c63340ae34b'
- Campchair::LevelDB.stub! :generate_key => key
-
- id = TestEntity << '22'
- id.should == key
- @db[id].should == '22'
+ context "Campchair::LevelDB serialization" do
+ describe ".[]{,=}" do
+ it "persists non-string data structures" do
+ doc = { :sym => :sym, 'string' => 'string', 'types' => [1234.12341234, (55..44), /regexen/, Time.now, { 'a' => 22 }] }
+ TestEntity['something'] = doc
+ TestEntity['something'].should == doc
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.