Class: Redwood::LabelManager
- 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 ]
- LISTABLE_RESERVED_LABELS =
labels which it nonetheless makes sense to search for by
[ :starred, :spam, :draft, :sent, :killed, :deleted, :inbox ]
- HIDDEN_RESERVED_LABELS =
labels that will typically be hidden from the user
[ :starred, :unread ]
Instance Method Summary collapse
- #<<(t) ⇒ Object
-
#applyable_labels ⇒ Object
all apply-able (user-defined and system listable) labels, ordered nicely and converted to pretty strings.
- #delete(t) ⇒ Object
-
#initialize(fn) ⇒ LabelManager
constructor
A new instance of LabelManager.
- #label_for(s) ⇒ Object
-
#listable_labels ⇒ Object
all listable (just user-defined at the moment) labels, ordered nicely and converted to pretty strings.
- #save ⇒ Object
-
#string_for(l) ⇒ Object
reverse the label->string mapping, for convenience!.
Constructor Details
#initialize(fn) ⇒ LabelManager
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
#<<(t) ⇒ Object
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 |
#applyable_labels ⇒ Object
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 |
#delete(t) ⇒ Object
73 74 75 76 77 |
# File 'lib/sup/label.rb', line 73 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 |
#listable_labels ⇒ Object
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 |
#save ⇒ Object
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 } 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.ucfirst else l.to_s end end |