diff options
author | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-05-09 21:25:50 +0000 |
---|---|---|
committer | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-05-09 21:25:50 +0000 |
commit | 55f4dc4c9a5345c28d0da750d1ee00fbb0870885 (patch) | |
tree | 904359659b75882365348decb2ca0789ca1ac803 /lib/yaml/store.rb | |
parent | 605adb86e2c2889c13e07cadf328f8032eebae7c (diff) |
Initial checkin of YAML substances.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml/store.rb')
-rw-r--r-- | lib/yaml/store.rb | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/yaml/store.rb b/lib/yaml/store.rb new file mode 100644 index 0000000000..b2924b0660 --- /dev/null +++ b/lib/yaml/store.rb @@ -0,0 +1,75 @@ +# +# YAML::Store +# +require 'yaml' +require 'pstore' + +module YAML + + class Store < PStore + # + # Constructor + # + def initialize( *o ) + @opt = YAML::DEFAULTS.dup + if String === o.first + super(o.pop) + 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 + File::copy @filename, backup + @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 |