diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index d2bdf03..67261ca 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -1,7 +1,10 @@ +require 'tempfile' require 'yaml' module Chicanery module Persistence + TEMP_DIR = './tmp' + def persist state File.open persist_state_to, 'w' do |file| file.puts state.to_yaml @@ -15,7 +18,8 @@ def restore def persist_state_to path=nil @state = path if path - @state || 'state' + Dir.mkdir TEMP_DIR if not Dir.exist? TEMP_DIR + @state ||= Dir::Tmpname.make_tmpname "#{TEMP_DIR}/state", nil end end -end \ No newline at end of file +end diff --git a/spec/chicanery/collections_spec.rb b/spec/chicanery/collections_spec.rb index d72c0bf..4d2e60b 100644 --- a/spec/chicanery/collections_spec.rb +++ b/spec/chicanery/collections_spec.rb @@ -1,3 +1,5 @@ +require 'chicanery' + describe Chicanery::Collections do include Chicanery::Collections @@ -11,4 +13,4 @@ send("#{entity}s").should == [:entity] end end -end \ No newline at end of file +end diff --git a/spec/chicanery/persistence_spec.rb b/spec/chicanery/persistence_spec.rb index 7b1d145..2bd0586 100644 --- a/spec/chicanery/persistence_spec.rb +++ b/spec/chicanery/persistence_spec.rb @@ -3,10 +3,11 @@ let(:file) { double 'file' } let(:state) { double 'state', to_yaml: :yaml } + let(:state_regex) { /.*\/state.*/ } describe '#persist' do it 'should write state to disk as yaml' do - File.should_receive(:open).with('state', 'w').and_yield file + File.should_receive(:open).with(state_regex, 'w').and_yield file file.should_receive(:puts).with :yaml persist state end @@ -21,13 +22,13 @@ describe '#restore' do it 'should return empty hash if state file does not exist' do - File.should_receive(:exist?).with('state').and_return false + File.should_receive(:exist?).with(state_regex).and_return false restore.should == {} end it 'should read yaml from disk' do - File.should_receive(:exist?).with('state').and_return true - YAML.should_receive(:load_file).with('state').and_return state + File.should_receive(:exist?).with(state_regex).and_return true + YAML.should_receive(:load_file).with(state_regex).and_return state restore.should == state end @@ -38,4 +39,4 @@ restore.should == state end end -end \ No newline at end of file +end