lib/sup/thread.rb in sup-0.8.1 vs lib/sup/thread.rb in sup-0.9
- old
+ new
@@ -22,10 +22,12 @@
## same subject but we don't have evidence from in-reply-to: or
## references: headers. In this case Thread#each can optionally yield
## a faked root object tying them all together into one tree
## structure.
+require 'set'
+
module Redwood
class Thread
include Enumerable
@@ -99,21 +101,20 @@
def remove_label t; each { |m, *o| m && m.remove_label(t) }; end
def toggle_label label
if has_label? label
remove_label label
- return false
+ false
else
apply_label label
- return true
+ true
end
end
def set_labels l; each { |m, *o| m && m.labels = l }; end
-
def has_label? t; any? { |m, *o| m && m.has_label?(t) }; end
- def save index; each { |m, *o| m && m.save(index) }; end
+ def save_state index; each { |m, *o| m && m.save_state(index) }; end
def direct_participants
map { |m, *o| [m.from] + m.to if m }.flatten.compact.uniq
end
@@ -121,19 +122,18 @@
map { |m, *o| [m.from] + m.to + m.cc + m.bcc if m }.flatten.compact.uniq
end
def size; map { |m, *o| m ? 1 : 0 }.sum; end
def subj; argfind { |m, *o| m && m.subj }; end
- def labels
- map { |m, *o| m && m.labels }.flatten.compact.uniq.sort_by { |t| t.to_s }
- end
+ def labels; inject(Set.new) { |s, (m, *o)| m ? s | m.labels : s } end
def labels= l
- each { |m, *o| m && m.labels = l.clone }
+ raise ArgumentError, "not a set" unless l.is_a?(Set)
+ each { |m, *o| m && m.labels = l.dup }
end
def latest_message
- inject(nil) do |a, b|
+ inject(nil) do |a, b|
b = b.first
if a.nil?
b
elsif b.nil?
a
@@ -160,11 +160,11 @@
def initialize id
raise "non-String #{id.inspect}" unless id.is_a? String
@id = id
@message, @parent, @thread = nil, nil, nil
@children = []
- end
+ end
def each_with_stuff parent=nil
yield self, 0, parent
@children.each do |c|
c.each_with_stuff(self) { |cc, d, par| yield cc, d + 1, par }
@@ -308,17 +308,19 @@
c.thread = nil
end
private :prune_thread_of
def remove_id mid
- return unless(c = @messages[mid])
+ return unless @messages.member?(mid)
+ c = @messages[mid]
remove_container c
prune_thread_of c
end
def remove_thread_containing_id mid
- c = @messages[mid] or return
+ return unless @messages.member?(mid)
+ c = @messages[mid]
t = c.root.thread
@threads.delete_if { |key, thread| t == thread }
end
## load in (at most) num number of threads from the index
@@ -353,10 +355,10 @@
## does its best, heuristically, to determine which is the parent.
def join_threads threads
return if threads.size < 2
containers = threads.map do |t|
- c = @messages[t.first.id]
+ c = @messages.member?(t.first.id) ? @messages[t.first.id] : nil
raise "not in threadset: #{t.first.id}" unless c && c.message
c
end
## use subject headers heuristically