From 84800e3b7d43a7db3b1fe49348b928aafa8ce871 Mon Sep 17 00:00:00 2001 From: deeeki Date: Thu, 29 May 2014 23:24:29 +0900 Subject: [PATCH] Enable `.clean_with` to receive simple arguments --- lib/database_rewinder/cleaner.rb | 5 +-- spec/database_rewinder_spec.rb | 56 ++++++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/database_rewinder/cleaner.rb b/lib/database_rewinder/cleaner.rb index 5da5798..0b0d98a 100644 --- a/lib/database_rewinder/cleaner.rb +++ b/lib/database_rewinder/cleaner.rb @@ -27,9 +27,10 @@ def clean_all reset end - def clean_with(_strategy, only: nil, except: nil, **) + def clean_with(*args) + options = args.extract_options! originals = @only, @except - @only, @except = Array(only), Array(except) + @only, @except = Array(options[:only]), Array(options[:except]) clean_all ensure @only, @except = originals diff --git a/spec/database_rewinder_spec.rb b/spec/database_rewinder_spec.rb index ad82973..d9a6d95 100644 --- a/spec/database_rewinder_spec.rb +++ b/spec/database_rewinder_spec.rb @@ -66,24 +66,54 @@ @except = @cleaner.instance_variable_get(:@except) Foo.create! name: 'foo1' Bar.create! name: 'bar1' - DatabaseRewinder.clean_with :truncation, options end - context 'with only option' do - let(:options) { { only: ['foos'] } } - it 'should clean with only option and restore original one' do - Foo.count.should == 0 - Bar.count.should == 1 - expect(@cleaner.instance_variable_get(:@only)).to eq(@only) + context 'simple arguments' do + before do + DatabaseRewinder.clean_with options + end + + context 'with only option' do + let(:options) { { only: ['foos'] } } + it 'should clean with only option and restore original one' do + Foo.count.should == 0 + Bar.count.should == 1 + expect(@cleaner.instance_variable_get(:@only)).to eq(@only) + end + end + + context 'with except option' do + let(:options) { { except: ['bars'] } } + it 'should clean with except option and restore original one' do + Foo.count.should == 0 + Bar.count.should == 1 + expect(@cleaner.instance_variable_get(:@except)).to eq(@except) + end end end - context 'with except option' do - let(:options) { { except: ['bars'] } } - it 'should clean with except option and restore original one' do - Foo.count.should == 0 - Bar.count.should == 1 - expect(@cleaner.instance_variable_get(:@except)).to eq(@except) + + context 'compatible arguments' do + before do + DatabaseRewinder.clean_with :truncation, options + end + + context 'with only option' do + let(:options) { { only: ['foos'] } } + it 'should clean with only option and restore original one' do + Foo.count.should == 0 + Bar.count.should == 1 + expect(@cleaner.instance_variable_get(:@only)).to eq(@only) + end + end + + context 'with except option' do + let(:options) { { except: ['bars'] } } + it 'should clean with except option and restore original one' do + Foo.count.should == 0 + Bar.count.should == 1 + expect(@cleaner.instance_variable_get(:@except)).to eq(@except) + end end end end