summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-05-27 07:43:38 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-05-27 07:43:38 +0000
commit979de48915cd1c0d903eeed0fcec181ab80ef1ae (patch)
tree5caa2535e7447380b264c1ddedbad23da070ceb2 /lib
parent87fb7bb66daa35dcf1750e2f764da0167ac79938 (diff)
* lib/pstore.rb (transaction): allow overriding dump and load.
[ruby-dev:23567] * lib/yaml/store.rb: follow lib/pstore.rb's change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6420 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/cgi.rb9
-rw-r--r--lib/pstore.rb18
-rw-r--r--lib/thread.rb11
-rw-r--r--lib/yaml/store.rb85
4 files changed, 43 insertions, 80 deletions
diff --git a/lib/cgi.rb b/lib/cgi.rb
index 6ec980fd70..3141d3d692 100644
--- a/lib/cgi.rb
+++ b/lib/cgi.rb
@@ -1549,14 +1549,9 @@ class CGI
body = ""
end
if @output_hidden
- hidden = @output_hidden.collect{|k,v|
- "<INPUT TYPE=HIDDEN NAME=\"#{k}\" VALUE=\"#{v}\">"
+ body += @output_hidden.collect{|k,v|
+ "<INPUT TYPE=\"HIDDEN\" NAME=\"#{k}\" VALUE=\"#{v}\">"
}.to_s
- if defined? fieldset
- body += fieldset{ hidden }
- else
- body += hidden
- end
end
super(attributes){body}
end
diff --git a/lib/pstore.rb b/lib/pstore.rb
index d6f5872fe9..25294fe009 100644
--- a/lib/pstore.rb
+++ b/lib/pstore.rb
@@ -98,7 +98,7 @@ class PStore
content = nil
file = File.open(@filename, File::RDWR | File::CREAT)
- if !read_only
+ unless read_only
file.flock(File::LOCK_EX)
commit_new(file) if FileTest.exist?(new_file)
content = file.read()
@@ -112,7 +112,7 @@ class PStore
end
if content != ""
- @table = Marshal::load(content)
+ @table = load(content)
if !read_only
size = content.size
md5 = Digest::MD5.digest(content)
@@ -132,7 +132,7 @@ class PStore
ensure
if !read_only and !@abort
tmp_file = @filename + ".tmp"
- content = Marshal::dump(@table)
+ content = dump(@table)
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
File.open(tmp_file, "w") {|t|
t.write(content)
@@ -151,6 +151,18 @@ class PStore
value
end
+ def dump(table)
+ Marshal::dump(table)
+ end
+
+ def load(content)
+ Marshal::load(content)
+ end
+
+ def load_file(file)
+ Marshal::load(file)
+ end
+
private
def commit_new(f)
f.truncate(0)
diff --git a/lib/thread.rb b/lib/thread.rb
index 3baa951522..8b27356c48 100644
--- a/lib/thread.rb
+++ b/lib/thread.rb
@@ -189,11 +189,14 @@ class ConditionVariable
# Releases the lock held in +mutex+ and waits; reacquires the lock on wakeup.
#
def wait(mutex)
- mutex.exclusive_unlock do
- @waiters.push(Thread.current)
- Thread.stop
+ begin
+ mutex.exclusive_unlock do
+ @waiters.push(Thread.current)
+ Thread.stop
+ end
+ ensure
+ mutex.lock
end
- mutex.lock
end
#
diff --git a/lib/yaml/store.rb b/lib/yaml/store.rb
index d36887720f..2ffa9554b9 100644
--- a/lib/yaml/store.rb
+++ b/lib/yaml/store.rb
@@ -3,74 +3,27 @@
#
require 'yaml'
require 'pstore'
-require 'fileutils'
-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
- FileUtils::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