Skip to content

Commit

Permalink
Allow mongodb to accept uri instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
erithmetic committed Jan 21, 2011
1 parent 49d02bd commit ae697fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
21 changes: 13 additions & 8 deletions lib/moneta/adapters/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ class MongoDB
include Moneta::Defaults

def initialize(options = {})
options = {
:host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
:port => ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT,
:db => 'cache',
:collection => 'cache'
}.update(options)
conn = Mongo::Connection.new(options[:host], options[:port])
@cache = conn.db(options[:db]).collection(options[:collection])
if options[:uri]
db = Mongo::Connection.from_uri options[:uri]
else
options = {
:host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
:port => ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT,
:db => 'cache',
:collection => 'cache'
}.update(options)
conn = Mongo::Connection.new(options[:host], options[:port])
db = conn.db(options[:db])
end
@cache = db.collection(options[:collection])
end

def key?(key, *)
Expand Down
27 changes: 22 additions & 5 deletions spec/moneta_mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
@cache.clear
end

it_should_behave_like "a read/write Moneta cache"
describe '#initialize' do
it 'should initialize with a URI' do
mock_mongo = mock(Mongo::Connection, :collection => [])
Mongo::Connection.should_receive(:from_uri).
with('mongodb://a:b@localhost:27059/cache').
and_return mock_mongo
m = Moneta::Adapters::MongoDB.new :uri => 'mongodb://a:b@localhost:27059/cache'
end
it 'should initialize with a hash of options' do
m = Moneta::Adapters::MongoDB.new
m['example'] = 3.0
m['example'].should == 3.0
end
end

context 'initialized' do
it_should_behave_like "a read/write Moneta cache"

describe '#store' do
it 'should store marshalled data that contains "invalid" UTF-8 characters' do
expect { @cache['example'] = 17.8 }.
should_not raise_error
describe '#store' do
it 'should store marshalled data that contains "invalid" UTF-8 characters' do
expect { @cache['example'] = 17.8 }.
should_not raise_error
end
end
end
end
Expand Down

0 comments on commit ae697fc

Please sign in to comment.