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

From: Gregor Hoffleit <[email-masked]>
Date: Wed, 13 Jan 2010 14:13:18 +0100

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'.

Also, it took me a while to realize that errors in before-add-message.rb
won't lead to a visible error message, but that before-add-message.rb
just silently quits.

The examples in http://sup.rubyforge.org/wiki/wiki.pl?Hooks are a very
good start.

Still, perhaps somebody with robust Ruby skills could add in a section
about the exact usage of message.add_label and message.remove_label,
e.g.:

- What's the difference between "label" and :label?
- Can I use multiple labels in one invocation?
- How about dashes in labels? Is :sup-devel valid, or should I use
  :"sup-devel"?
- How about spaces in label?
- How about "-inbox"? Does it work?

Regards,
    Gregor Hoffleit
_______________________________________________
sup-talk mailing list
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-talk

---

From: Gregor Hoffleit <[email-masked]>
Date: Fri, 15 Jan 2010 16:51:46 +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:

---

From: William Morgan <[email-masked]>
Date: Sun, 17 Jan 2010 10:29:43 -0500

> - What's the difference between "label" and :label?

Nothing. Strings get turned into symbols automagically and so both will
add the label :label.

> - Can I use multiple labels in one invocation?

No.

> - How about dashes in labels? Is :sup-devel valid, or should I use

The latter is necessary, but only because of Ruby's syntax. You can also
use "sup-devel", of course.

> - How about spaces in label?

Same as with -'s.

> - How about "-inbox"? Does it work?

It will work but will give you weird errors later on down the road as
punishment for being tricky. :)
-- 
William <[email-masked]>
_______________________________________________
sup-talk mailing list
[email-masked]
http://rubyforge.org/mailman/listinfo/sup-talk

---

From: Eric Sherman <[email-masked]>
Date: Wed, 13 Jan 2010 10:49:00 -0500

You could try this:

Or something like this: