From: Kazuhiro NISHIYAMA Date: 2004-05-23T04:59:25+09:00 Subject: [ruby-dev:23567] PStoreを継承しやすく 西山和広です。 PStoreのdumpとloadだけを差し替えしやすくして、 YAML::StoreをPStoreの変更に影響されにくくする というのはどうでしょうか? Index: lib/pstore.rb =================================================================== RCS file: /src/ruby/lib/pstore.rb,v retrieving revision 1.17 diff -u -p -r1.17 pstore.rb --- lib/pstore.rb 7 Jan 2003 07:36:39 -0000 1.17 +++ lib/pstore.rb 22 May 2004 19:53:50 -0000 @@ -98,9 +98,9 @@ class PStore end file.flock(read_only ? File::LOCK_SH : File::LOCK_EX) if read_only - @table = Marshal::load(file) + @table = load_file(file) elsif orig and (content = file.read) != "" - @table = Marshal::load(content) + @table = load(content) size = content.size md5 = Digest::MD5.digest(content) content = nil # unreference huge data @@ -117,7 +117,7 @@ class PStore ensure if !read_only and !@abort file.rewind - content = Marshal::dump(@table) + content = dump(@table) if !md5 || size != content.size || md5 != Digest::MD5.digest(content) File::copy @filename, backup begin @@ -141,6 +141,18 @@ class PStore file.close if file end value + end + + def dump(table) + Marshal::dump(table) + end + + def load(content) + Marshal::load(content) + end + + def load_file(file) + Marshal::load(file) end end % cat lib/yaml/store.rb # # YAML::Store # require 'yaml' require 'pstore' module YAML class 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 def dump(table) @table.to_yaml(@opt) end def load(content) YAML::load(content) end def load_file(file) YAML::load(file) end end end % -- |ZnZ(ゼット エヌ ゼット) |西山和広(Kazuhiro NISHIYAMA)