Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updates DatabaseCleaner#[] to adhere to the Principle Of Least Surprise

This is really ugly.  What it was doing before was worse.. but this API
of query + command is really a mess.  This should, hopefully, prevent
people from being confused about the API as in #144.
  • Loading branch information...
commit 016d66c4c9621fceadbebeb80feb5744d810c681 1 parent d23c17c
@bmabey bmabey authored
View
2  lib/database_cleaner/base.rb
@@ -85,7 +85,7 @@ def auto_detected?
#TODO make strategies directly comparable
def ==(other)
- self.orm == other.orm && self.db == other.db && self.strategy.class == other.strategy.class
+ self.orm == other.orm && self.db == other.db
end
private
View
29 lib/database_cleaner/configuration.rb
@@ -6,11 +6,29 @@ class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end
class << self
+ def init_cleaners
+ @cleaners ||= {}
+ # ghetto ordered hash.. maintains 1.8 compat and old API
+ @connections ||= []
+ end
+
def [](orm,opts = {})
raise NoORMDetected unless orm
- @connections ||= []
+ init_cleaners
+ # TODO: deprecate
+ # this method conflates creation with lookup. Both a command and a query. Yuck.
+ if @cleaners.has_key? [orm, opts]
+ @cleaners[[orm, opts]]
+ else
+ add_cleaner(orm, opts)
+ end
+ end
+
+ def add_cleaner(orm,opts = {})
+ init_cleaners
cleaner = DatabaseCleaner::Base.new(orm,opts)
- connections.push cleaner
+ @cleaners[[orm, opts]] = cleaner
+ @connections << cleaner
cleaner
end
@@ -23,7 +41,12 @@ def app_root
end
def connections
- @connections ||= [::DatabaseCleaner::Base.new]
+ # double yuck.. can't wait to deprecate this whole class...
+ unless @cleaners
+ autodetected = ::DatabaseCleaner::Base.new
+ add_cleaner(autodetected.orm)
+ end
+ @connections
end
def logger=(log_source)
View
19 spec/database_cleaner/configuration_spec.rb
@@ -1,12 +1,20 @@
require 'spec_helper'
+module ArrayHelper
+ def zipmap(array, vals)
+ Hash[*(array.zip(vals).flatten)]
+ end
+ module_function :zipmap
+end
module DatabaseCleaner
class << self
def reset
+ @cleaners = nil
@connections = nil
end
-
+ # hackey, hack.. connections needs to stick around until I can properly deprecate the API
def connections_stub!(array)
+ @cleaners = ArrayHelper.zipmap((1..array.size).to_a, array)
@connections = array
end
end
@@ -108,14 +116,11 @@ def connections_stub!(array)
context "class methods" do
subject { ::DatabaseCleaner }
- its(:connections) { should respond_to(:each) }
-
it "should give me a default (autodetection) databasecleaner by default" do
cleaner = mock("cleaner").as_null_object
- ::DatabaseCleaner::Base.should_receive(:new).with().and_return(cleaner)
-
- ::DatabaseCleaner.connections.should have(1).items
- ::DatabaseCleaner.connections.first.should == cleaner
+ ::DatabaseCleaner::Base.stub!(:new).and_return(cleaner)
+
+ ::DatabaseCleaner.connections.should == [cleaner]
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.