Subject: Re: [sup-talk] Ruby question: before-add-message.rb and adding multiple labels at once

From: Gregor Hoffleit <[email-masked]>
Date: Fri, 15 Jan 2010 18:01:12 +0100

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?

Regards,
    Gregor Hoffleit

* Eric Sherman <[email-masked]> [Mi Jan 13 16:49:00 +0100 2010]
> You could try this:
> [:list, :sup-devel].each { |l| message.add_label l } if condition
> [:inbox, :unread].each { |l| message.remove_label l } if condition
> 
> Or something like this:
> case [message.from, message.subj].flatten.to_s
>   when /foo/ then
>     [:label1, :label2].each { |l| message.add_label l }
>     [:inbox, :unread].each { |l| message.remove_label l }
>   when /bar/ then
>     [:label3, :label4].each { |l| message.add_label l }
>     [:inbox, :unread].each { |l| message.remove_label l }
> end
>
> Excerpts from Gregor Hoffleit's message of Wed Jan 13 08:13:18 -0500 2010:
> > In order to keep before-add-message.rb readable, I was using one-line
> > filters like:
> > 
> >     message.add_label "ad apple" if message.from.email =~ /@insideapple.apple.com/
> >     message.add_label "ad amazon" if message.from.email =~ /[email-masked]/
> >     message.add_label "list sup" if message.subj =~ /\[sup-talk\]/
> >     message.add_label "list sup-devel" if message.subj =~ /\[sup-devel\]/
> > 
> > That seemed to work, somehow. Only now I realized that message.add_label
> > in fact only takes a single label as argument.
> > 
> > Is it true that I have to change the above syntax into the much more
> > verbose:
> > 
> >     if message.subj =~ /\[sup-devel\]/
> >     message.add_label :list
> >     message.add_label :"sup-devel"
> >     end
> > 
> > Removing a message from the inbox ("message.remove_label :inbox") while
> > applying a label leads to a similar 'code bloat'.
> 
_______________________________________________
sup-talk mailing list
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-talk

---

From: Tero Tilus <[email-masked]>
Date: Sun, 17 Jan 2010 00:41:09 +0200

>     message.edit_labels "list sup-devel -inbox" if message.subj =~ /\[sup-devel\]/

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 <[email-masked]>
Date: Sun, 17 Jan 2010 00:23:26 +0200
Subject: [PATCH] Message#edit_labels

Signed-off-by: Tero Tilus <[email-masked]>
---
 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
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-devel