Permalink
Browse files

Renamed mongo to mongo single doc and added mongo many doc adapter

  • Loading branch information...
1 parent 00c1b36 commit 71ffd97ce53ed9a848d47d68a4c46ed30226b14b @jnunemaker committed Aug 3, 2012
@@ -1,24 +1,55 @@
require 'set'
require 'forwardable'
require 'mongo'
-require 'flipper/adapters/mongo/document'
module Flipper
module Adapters
class Mongo
extend Forwardable
- def initialize(collection, options = {})
+ def initialize(collection)
@collection = collection
- @options = options
+ @update_options = {:safe => true, :upsert => true}
end
- def_delegators :document, :read, :write, :delete, :set_members, :set_add, :set_delete
+ def read(key)
+ find_one key
+ end
+
+ def write(key, value)
+ update key, {'$set' => {'v' => value}}
+ end
+
+ def delete(key)
+ @collection.remove criteria(key)
+ end
+
+ def set_members(key)
+ (find_one(key) || Set.new).to_set
+ end
+
+ def set_add(key, value)
+ update key, {'$addToSet' => {'v' => value}}
+ end
+
+ def set_delete(key, value)
+ update key, {'$pull' => {'v' => value}}
+ end
private
- def document
- Document.new(@collection, :id => @options[:id])
+ def find_one(key)
+ if (doc = @collection.find_one(criteria(key)))
+ doc['v']
+ end
+ end
+
+ def update(key, updates)
+ @collection.update criteria(key), updates, @update_options
+ end
+
+ def criteria(key)
+ {:_id => key}
end
end
end
@@ -3,7 +3,7 @@
module Flipper
module Adapters
- class Mongo
+ class MongoSingleDocument
class Document
DefaultId = 'flipper'
@@ -0,0 +1,25 @@
+require 'set'
+require 'forwardable'
+require 'mongo'
+require 'flipper/adapters/mongo/document'
+
+module Flipper
+ module Adapters
+ class MongoSingleDocument
+ extend Forwardable
+
+ def initialize(collection, options = {})
+ @collection = collection
+ @options = options
+ end
+
+ def_delegators :document, :read, :write, :delete, :set_members, :set_add, :set_delete
+
+ private
+
+ def document
+ Document.new(@collection, :id => @options[:id])
+ end
+ end
+ end
+end
@@ -1,7 +1,7 @@
require 'helper'
require 'flipper/adapters/mongo/document'
-describe Flipper::Adapters::Mongo::Document do
+describe Flipper::Adapters::MongoSingleDocument::Document do
subject { described_class.new(collection, :id => id, :source => source) }
let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
let(:id) { described_class::DefaultId }
@@ -0,0 +1,39 @@
+require 'helper'
+require 'flipper/adapters/mongo_single_document'
+require 'flipper/spec/shared_adapter_specs'
+
+describe Flipper::Adapters::MongoSingleDocument do
+ let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
+ let(:criteria) { {:_id => id} }
+ let(:id) { 'flipper' }
+
+ subject { described_class.new(collection, :id => id) }
+
+ before do
+ collection.remove(criteria)
+ end
+
+ def read_key(key)
+ if (doc = collection.find_one(criteria))
+ value = doc[key]
+
+ if value.is_a?(::Array)
+ value = value.to_set
+ end
+
+ value
+ end
+ end
+
+ def write_key(key, value)
+ if value.is_a?(::Set)
+ value = value.to_a
+ end
+
+ options = {:upsert => true}
+ updates = {'$set' => {key => value}}
+ collection.update criteria, updates, options
+ end
+
+ it_should_behave_like 'a flipper adapter'
+end
@@ -4,18 +4,17 @@
describe Flipper::Adapters::Mongo do
let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
- let(:criteria) { {:_id => id} }
let(:id) { 'flipper' }
- subject { Flipper::Adapters::Mongo.new(collection, :id => id) }
+ subject { described_class.new(collection) }
before do
- collection.remove(criteria)
+ collection.remove
end
def read_key(key)
- if (doc = collection.find_one(criteria))
- value = doc[key]
+ if (doc = collection.find_one(:_id => key))
+ value = doc['v']
if value.is_a?(::Array)
value = value.to_set
@@ -30,8 +29,9 @@ def write_key(key, value)
value = value.to_a
end
- options = {:upsert => true}
- updates = {'$set' => {key => value}}
+ criteria = {:_id => key}
+ updates = {'$set' => {'v' => value}}
+ options = {:upsert => true}
collection.update criteria, updates, options
end

0 comments on commit 71ffd97

Please sign in to comment.