]> git.cworth.org Git - sup/commitdiff
Merge branch 'various-api-refactors' into next
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 19 Aug 2009 18:34:34 +0000 (14:34 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 19 Aug 2009 18:34:34 +0000 (14:34 -0400)
lib/sup.rb
lib/sup/imap.rb
lib/sup/maildir.rb
lib/sup/mbox/loader.rb
lib/sup/source.rb
lib/sup/util.rb

index deae3d4c7474fa5910da56ec186bfe1b9fe585e9..12513f7df7397dd1c9a6a25c507e56f7c5f44ba6 100644 (file)
@@ -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
index c2904fb8a4a8774921f0b6fe2ea66508b8c89ad9..3cf6489c59ad62d6dc6cae83cb2ad0dae419062a 100644 (file)
@@ -48,6 +48,7 @@ require 'set'
 module Redwood
 
 class IMAP < Source
+  include SerializeLabelsNicely
   SCAN_INTERVAL = 60 # seconds
 
   ## upon these errors we'll try to rereconnect a few times
index 5e3ae30f0b896c11494e573a439e06076a94fc8a..2c33e3bdc06b08f68b1d4b3caf564021eb6d71b8 100644 (file)
@@ -9,6 +9,7 @@ module Redwood
 ## pathnames on disk.
 
 class Maildir < Source
+  include SerializeLabelsNicely
   SCAN_INTERVAL = 30 # seconds
   MYHOSTNAME = Socket.gethostname
 
index 26177f76bb12041c7cbfee16414f656a6dd15afc..030759483289f2ee6bd115b818e9dfecb5a986de 100644 (file)
@@ -6,6 +6,7 @@ module Redwood
 module MBox
 
 class Loader < Source
+  include SerializeLabelsNicely
   yaml_properties :uri, :cur_offset, :usual, :archived, :id, :labels
 
   ## uri_or_fp is horrific. need to refactor.
index 8154591f6adce27c071e16d11784f8dd77b05888..78386ff3fd19b0f74c75820827346df036f84c61 100644 (file)
@@ -161,6 +161,21 @@ protected
   end
 end
 
+## if you have a @labels instance variable, include this
+## to serialize them nicely as an array, rather than as a
+## nasty set.
+module SerializeLabelsNicely
+  def before_marshal # can return an object
+    c = clone
+    c.instance_eval { @labels = @labels.to_a.map { |l| l.to_s } }
+    c
+  end
+
+  def after_unmarshal!
+    @labels = Set.new(@labels.map { |s| s.to_sym })
+  end
+end
+
 class SourceManager
   include Singleton
 
@@ -209,7 +224,7 @@ class SourceManager
           File.chmod 0600, fn
           FileUtils.mv fn, bakfn, :force => true unless File.exists?(bakfn) && File.size(fn) == 0
         end
-        Redwood::save_yaml_obj sources.sort_by { |s| s.id.to_i }, fn, true
+        Redwood::save_yaml_obj sources, fn, true
         File.chmod 0600, fn
       end
       @sources_dirty = false
index 518866beff61e308319ccde80042cdb9c442d8a0..aa3ee764ac59b276d7f4ae758d4b119e141832c2 100644 (file)
@@ -92,7 +92,7 @@ end
 
 class Range
   ## only valid for integer ranges (unless I guess it's exclusive)
-  def size 
+  def size
     last - first + (exclude_end? ? 0 : 1)
   end
 end