Class: Redwood::LabelManager

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

Constant Summary

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 ]
LISTABLE_RESERVED_LABELS =

labels which it nonetheless makes sense to search for by

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

labels that will typically be hidden from the user

[ :starred, :unread, :attachment ]

Instance Method Summary (collapse)

Constructor Details

- (LabelManager) initialize(fn)

Returns a new instance of LabelManager



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

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

  self.class.i_am_the_instance self
end

Instance Method Details

- (Object) <<(t)



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

def << t
  t = t.intern unless t.is_a? Symbol
  unless @labels.member?(t) || RESERVED_LABELS.member?(t)
    @labels[t] = true
    @modified = true
  end
end

- (Object) applyable_labels

all apply-able (user-defined and system listable) 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 applyable_labels
  @labels.keys
end

- (Object) delete(t)



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

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

- (Object) label_for(s)



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

- (Object) listable_labels

all listable (just user-defined at the moment) labels, 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 listable_labels
  ## uniq's only necessary here because of certain upgrade issues
  (LISTABLE_RESERVED_LABELS + @labels.keys).uniq
end

- (Object) save



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

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

- (Object) string_for(l)

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.ucfirst
  else
    l.to_s
  end
end