Skip to content

Commit

Permalink
DRY up file locks.
Browse files Browse the repository at this point in the history
  • Loading branch information
dougal committed Feb 9, 2011
1 parent 04a7b0b commit 75cb18d
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/acts_as_indexed/storage.rb
Expand Up @@ -90,10 +90,8 @@ def operate(operation, atoms)

atoms = from_file.merge(atoms){ |k,o,n| o.send(operation, n) }

lock_file(path) do
write_file(path) do |f|
Marshal.dump(atoms,f)
end
write_file(path) do |f|
Marshal.dump(atoms,f)
end
end
end
Expand All @@ -102,10 +100,8 @@ def update_record_count(delta)
new_count = self.record_count + delta
new_count = 0 if new_count < 0

lock_file(@size_path) do
write_file(@size_path) do |f|
f.write(new_count)
end
write_file(@size_path) do |f|
f.write(new_count)
end
end

Expand Down Expand Up @@ -150,14 +146,16 @@ def encode_character(char)
end

def write_file(file_path)
new_file = file_path.to_s
tmp_file = new_file + TEMP_FILE_EXTENSION
lock_file(file_path) do
new_file = file_path.to_s
tmp_file = new_file + TEMP_FILE_EXTENSION

File.open(tmp_file, 'w+') do |f|
yield(f)
end
File.open(tmp_file, 'w+') do |f|
yield(f)
end

FileUtils.mv(tmp_file, new_file)
FileUtils.mv(tmp_file, new_file)
end
end

# Borrowed from Rails' ActiveSupport FileStore. Also under MIT licence.
Expand Down

0 comments on commit 75cb18d

Please sign in to comment.