X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup.rb;h=43daa7e7be19b0ba12c83bec1d67c5f50d0b6d94;hb=aa5608229171a78694c64d0a30375e765ffe86e0;hp=ed144589a705abd854573e9b9f6eddf247ec458a;hpb=3dc202c1f8880357ce31bb65e4f52fe1110dad60;p=sup diff --git a/lib/sup.rb b/lib/sup.rb index ed14458..43daa7e 100644 --- a/lib/sup.rb +++ b/lib/sup.rb @@ -85,25 +85,37 @@ module Redwood module_function :reporting_thread, :record_exception, :exceptions ## one-stop shop for yamliciousness - def save_yaml_obj object, fn, safe=false + def save_yaml_obj o, fn, safe=false + o = if o.is_a?(Array) + o.map { |x| (x.respond_to?(:before_marshal) && x.before_marshal) || x } + else + o.respond_to?(:before_marshal) && o.before_marshal + end + if safe safe_fn = "#{File.dirname fn}/safe_#{File.basename fn}" mode = File.stat(fn).mode if File.exists? fn - File.open(safe_fn, "w", mode) { |f| f.puts object.to_yaml } + File.open(safe_fn, "w", mode) { |f| f.puts o.to_yaml } FileUtils.mv safe_fn, fn else - File.open(fn, "w") { |f| f.puts object.to_yaml } + File.open(fn, "w") { |f| f.puts o.to_yaml } end end def load_yaml_obj fn, compress=false - if File.exists? fn + o = if File.exists? fn if compress Zlib::GzipReader.open(fn) { |f| YAML::load f } else YAML::load_file fn end end + if o.is_a?(Array) + o.each { |x| x.after_unmarshal! if x.respond_to?(:after_unmarshal!) } + else + o.after_unmarshal! if o.respond_to?(:after_unmarshal!) + end + o end def start