2003-05-09 17:25:50 -04:00
|
|
|
#
|
|
|
|
# YAML::Store
|
|
|
|
#
|
|
|
|
require 'yaml'
|
|
|
|
require 'pstore'
|
2004-05-09 00:25:15 -04:00
|
|
|
require 'fileutils'
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
module YAML
|
|
|
|
|
|
|
|
class Store < PStore
|
|
|
|
#
|
|
|
|
# Constructor
|
|
|
|
#
|
|
|
|
def initialize( *o )
|
|
|
|
@opt = YAML::DEFAULTS.dup
|
|
|
|
if String === o.first
|
2003-08-01 14:23:35 -04:00
|
|
|
super(o.shift)
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
if o.last.is_a? Hash
|
|
|
|
@opt.update(o.pop)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Override Pstore#transaction
|
|
|
|
#
|
|
|
|
def transaction
|
|
|
|
raise YAML::Error, "nested transaction" if @transaction
|
|
|
|
raise YAML::Error, "no filename for transaction" unless @filename
|
|
|
|
begin
|
|
|
|
@transaction = true
|
|
|
|
value = nil
|
|
|
|
backup = @filename+"~"
|
|
|
|
if File::exist?(@filename)
|
|
|
|
file = File::open(@filename, "rb+")
|
|
|
|
orig = true
|
|
|
|
else
|
|
|
|
@table = {}
|
|
|
|
file = File::open(@filename, "wb+")
|
|
|
|
file.write( @table.to_yaml( @opt ) )
|
|
|
|
end
|
|
|
|
file.flock(File::LOCK_EX)
|
|
|
|
if orig
|
2004-05-09 00:25:15 -04:00
|
|
|
FileUtils::copy @filename, backup
|
2003-05-09 17:25:50 -04:00
|
|
|
@table = YAML::load( file )
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
catch(:pstore_abort_transaction) do
|
|
|
|
value = yield(self)
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
@abort = true
|
|
|
|
raise
|
|
|
|
ensure
|
|
|
|
unless @abort
|
|
|
|
begin
|
|
|
|
file.rewind
|
|
|
|
file.write( @table.to_yaml( @opt ) )
|
|
|
|
file.truncate(file.pos)
|
|
|
|
rescue
|
|
|
|
File::rename backup, @filename if File::exist?(backup)
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@abort = false
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@table = nil
|
|
|
|
@transaction = false
|
|
|
|
file.close if file
|
|
|
|
end
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|