Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use multiple temporary files for storing state #9

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/chicanery/persistence.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
end
4 changes: 3 additions & 1 deletion spec/chicanery/collections_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'chicanery'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated; fixes specs


describe Chicanery::Collections do
include Chicanery::Collections

Expand All @@ -11,4 +13,4 @@
send("#{entity}s").should == [:entity]
end
end
end
end
11 changes: 6 additions & 5 deletions spec/chicanery/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -38,4 +39,4 @@
restore.should == state
end
end
end
end