Permalink
Browse files

Default _id and allow it to be changed.

  • Loading branch information...
1 parent d19b478 commit 76c68cc2a45a95c9ea6ee85250af662440d51e3a @jnunemaker committed Aug 3, 2012
@@ -4,10 +4,12 @@
module Flipper
module Adapters
class Mongo
- def initialize(collection, id)
+ DefaultId = 'flipper'
+
+ def initialize(collection, options = {})
@collection = collection
- @id = id
- @mongo_criteria = {:_id => @id}
+ @options = options
+ @mongo_criteria = {:_id => id}
@mongo_options = {:upsert => true, :safe => true}
end
@@ -37,6 +39,10 @@ def set_members(key)
private
+ def id
+ @id ||= @options.fetch(:id) { DefaultId }
+ end
+
def update(updates)
@collection.update(@mongo_criteria, updates, @mongo_options)
end
@@ -5,12 +5,6 @@
module Flipper
module Adapters
class MongoWithTTL < Mongo
-
- def initialize(collection, id, options = {})
- super collection, id
- @options = options
- end
-
private
# Override Mongo adapters load
@@ -4,10 +4,10 @@
describe Flipper::Adapters::Mongo do
let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
- let(:oid) { BSON::ObjectId.new }
- let(:criteria) { {:_id => oid} }
+ let(:criteria) { {:_id => id} }
+ let(:id) { described_class::DefaultId }
- subject { Flipper::Adapters::Mongo.new(collection, oid) }
+ subject { Flipper::Adapters::Mongo.new(collection) }
before do
collection.remove(criteria)
@@ -4,10 +4,10 @@
describe Flipper::Adapters::MongoWithTTL do
let(:collection) { Mongo::Connection.new.db('testing')['testing'] }
- let(:oid) { BSON::ObjectId.new }
- let(:criteria) { {:_id => oid} }
+ let(:criteria) { {:_id => id} }
+ let(:id) { described_class::DefaultId }
- subject { Flipper::Adapters::MongoWithTTL.new(collection, oid) }
+ subject { Flipper::Adapters::MongoWithTTL.new(collection) }
before do
collection.remove(criteria)
@@ -17,12 +17,12 @@
it "can cache document in process for a number of seconds" do
options = {:ttl => 10}
- adapter = Flipper::Adapters::MongoWithTTL.new(collection, oid, options)
+ adapter = Flipper::Adapters::MongoWithTTL.new(collection, options)
adapter.write('foo', 'bar')
now = Time.now
Timecop.freeze(now)
- collection.should_receive(:find_one).with(:_id => oid)
+ collection.should_receive(:find_one).with(:_id => id)
adapter.read('foo')
adapter.read('foo')
@@ -34,7 +34,7 @@
Timecop.travel(6)
adapter.read('foo')
- collection.should_receive(:find_one).with(:_id => oid)
+ collection.should_receive(:find_one).with(:_id => id)
Timecop.travel(1)
adapter.read('foo')

0 comments on commit 76c68cc

Please sign in to comment.