[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sup-devel] [PATCH] Message#edit_labels [was: [sup-talk] Ruby question: before-add-message.rb and adding multiple labels at once]



Gregor Hoffleit, 2010-01-15 19:01:
> Ok. I ended up with
> 
>     ([:list,:"sup-devel"].each {|l| message.add_label l}; message.remove_label :inbox) if message.subj =~ /\[sup-devel\]/
> 
> May I request a new function "message.edit_labels" that groks the syntax
> of the 'edit labels' command (cf. multi_edit_labels in thread-index-mode.rb):
> 
>     message.edit_labels "list sup-devel -inbox" if message.subj =~ /\[sup-devel\]/
> 
> Comments?

I'll be using that one too.  Here's a patch.  Message#edit_labels
accepts array of strings.  I thought it'll be better that way.
Otherwise it gets really ugly with spaces in labels.

So your usecase would be

  message.edit_labels %w{list sup-devel -inbox} if message.subj =~ /\[sup-devel\]/

By the way, my pretty general list-labeling code looks like this
<http://pastie.org/781240>.

And then the patch...

>From 9eff953dbda404b149a77969305c5732fa1d694e Mon Sep 17 00:00:00 2001
From: Tero Tilus <tero@tilus.net>
Date: Sun, 17 Jan 2010 00:23:26 +0200
Subject: [PATCH] Message#edit_labels

Signed-off-by: Tero Tilus <tero@tilus.net>
---
 lib/sup/message.rb |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)

diff --git a/lib/sup/message.rb b/lib/sup/message.rb
index 3e55de5..e047927 100644
--- a/lib/sup/message.rb
+++ b/lib/sup/message.rb
@@ -206,6 +206,18 @@ class Message
     @labels.delete l
     @dirty = true
   end
+  ## calling m.edit_labels ['foo', '-index', '+bar']
+  ## adds labels foo and bar and removes label index
+  def edit_labels l_arr
+    l_arr.each do |signedlabel|
+      signedlabel, sign, label = signedlabel.match(/^(-|\+)?(.*)$/).to_a
+      if sign == '-'
+        remove_label label
+      else
+        add_label label
+      end
+    end
+  end
 
   def recipients
     @to + @cc + @bcc
-- 
1.5.6.5


-- 
Tero Tilus ## 050 3635 235 ## http://tero.tilus.net/
_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel