lib/sup/service/label_service.rb (936B) - raw
1 require "sup/index"
2
3 module Redwood
4 # Provides label tweaking service to the user.
5 # Working as the backend of ConsoleMode.
6 #
7 # Should become the backend of bin/sup-tweak-labels in the future.
8 class LabelService
9 # @param index [Redwood::Index]
10 def initialize index=Index.instance
11 @index = index
12 end
13
14 def add_labels query, *labels
15 run_on_each_message(query) do |m|
16 labels.each {|l| m.add_label l }
17 end
18 end
19
20 def remove_labels query, *labels
21 run_on_each_message(query) do |m|
22 labels.each {|l| m.remove_label l }
23 end
24 end
25
26
27 private
28 def run_on_each_message query, &operation
29 count = 0
30
31 find_messages(query).each do |m|
32 operation.call(m)
33 @index.update_message_state m
34 count += 1
35 end
36
37 @index.save_index
38 count
39 end
40
41 def find_messages query
42 @index.find_messages(query)
43 end
44 end
45 end