summaryrefslogtreecommitdiff
path: root/lib/yaml
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-01 14:15:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-01 14:15:38 +0000
commit7800d8ad76bfda9884f5127c576ba08b1fe82583 (patch)
tree60f53b0878437539143899edaaa35a54635b3182 /lib/yaml
parentc7bd86ed15eb4baf2dcb9a7b732891de4619d88a (diff)
* lib/pstore.rb (transaction): safer backup scheme. [ruby-list:39102]
* lib/pstore.rb (commit_new): use FileUtils.copy_stream for Cygwin. [ruby-dev:23157] * lib/pstore.rb (transaction): allow overriding dump and load. [ruby-dev:23567] * lib/pstore.rb (PStore#transaction): get rid of opening in write mode when read only transaction. [ruby-dev:23842] * lib/yaml/store.rb: follow lib/pstore.rb's change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6562 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml')
-rw-r--r--lib/yaml/store.rb84
1 files changed, 19 insertions, 65 deletions
diff --git a/lib/yaml/store.rb b/lib/yaml/store.rb
index 2e74b27221..2ffa9554b9 100644
--- a/lib/yaml/store.rb
+++ b/lib/yaml/store.rb
@@ -4,72 +4,26 @@
require 'yaml'
require 'pstore'
-module YAML
+class YAML::Store < PStore
+ def initialize( *o )
+ @opt = YAML::DEFAULTS.dup
+ if String === o.first
+ super(o.shift)
+ end
+ if o.last.is_a? Hash
+ @opt.update(o.pop)
+ end
+ end
- class Store < PStore
- #
- # Constructor
- #
- def initialize( *o )
- @opt = YAML::DEFAULTS.dup
- if String === o.first
- super(o.shift)
- end
- if o.last.is_a? Hash
- @opt.update(o.pop)
- end
- end
+ def dump(table)
+ @table.to_yaml(@opt)
+ 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
+ def load(content)
+ YAML::load(content)
+ end
+ def load_file(file)
+ YAML::load(file)
+ end
end