Class: Redwood::LabelManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sup/label.rb

Constant Summary collapse

RESERVED_LABELS =

labels that have special semantics. user will be unable to add/remove these via normal label mechanisms.

[ :starred, :spam, :draft, :unread, :killed, :sent, :deleted, :inbox, :attachment, :forwarded, :replied ]
HIDDEN_RESERVED_LABELS =

labels that will typically be hidden from the user

[ :starred, :unread, :attachment, :forwarded, :replied ]

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initialize(fn) ⇒ LabelManager

Returns a new instance of LabelManager.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sup/label.rb', line 15

def initialize fn
  @fn = fn
  labels =
    if File.exist? fn
      IO.readlines(fn).map { |x| x.chomp.intern }
    else
      []
    end
  @labels = {}
  @new_labels = {}
  @modified = false
  labels.each { |t| @labels[t] = true }
end

Instance Method Details

#<<(t) ⇒ Object

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
72
# File 'lib/sup/label.rb', line 65

def << t
  raise ArgumentError, "expecting a symbol" unless t.is_a? Symbol
  unless @labels.member?(t) || RESERVED_LABELS.member?(t)
    @labels[t] = true
    @new_labels[t] = true
    @modified = true
  end
end

#all_labelsObject

all labels user-defined and system, ordered nicely and converted to pretty strings. use #label_for to recover the original label.



34
35
36
37
# File 'lib/sup/label.rb', line 34

def all_labels
  ## uniq's only necessary here because of certain upgrade issues
  (RESERVED_LABELS + @labels.keys).uniq
end

#delete(t) ⇒ Object



74
75
76
77
78
# File 'lib/sup/label.rb', line 74

def delete t
  if @labels.delete(t)
    @modified = true
  end
end

#label_for(s) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/sup/label.rb', line 55

def label_for s
  l = s.intern
  l2 = s.downcase.intern
  if RESERVED_LABELS.include? l2
    l2
  else
    l
  end
end

#new_label?(l) ⇒ Boolean

Returns:

  • (Boolean)


29
# File 'lib/sup/label.rb', line 29

def new_label? l; @new_labels.include?(l) end

#saveObject



80
81
82
83
84
# File 'lib/sup/label.rb', line 80

def save
  return unless @modified
  File.open(@fn, "w:UTF-8") { |f| f.puts @labels.keys.sort_by { |l| l.to_s } }
  @new_labels = {}
end

#string_for(l) ⇒ Object

reverse the label->string mapping, for convenience!



47
48
49
50
51
52
53
# File 'lib/sup/label.rb', line 47

def string_for l
  if RESERVED_LABELS.include? l
    l.to_s.capitalize
  else
    l.to_s
  end
end

#user_defined_labelsObject

all user-defined labels, ordered nicely and converted to pretty strings. use #label_for to recover the original label.



42
43
44
# File 'lib/sup/label.rb', line 42

def user_defined_labels
  @labels.keys
end