Class: SupTag
Overview
Class to make tagging simple.
Instance Method Summary collapse
-
#archive(&block) ⇒ Array
Archive a message.
-
#cloaker(&b) ⇒ Block
Instance eval for blocks stolen from Trollop.
-
#initialize(message) ⇒ SupTag
constructor
Create a new SupTag.
-
#method_missing(method, *args, &block) ⇒ Object
Override method missing to allow for matching the results of a method on message against some queries.
-
#multi(*labels, &block) ⇒ Array
Test queries across several feilds to add tags.
-
#remove(tags) ⇒ Array
Remove the given tags from the message.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Override to include methods from messages.
-
#tag(&block) ⇒ Array
Tag a message.
Constructor Details
#initialize(message) ⇒ SupTag
Create a new SupTag.
7 8 9 10 |
# File 'lib/sup_tag.rb', line 7 def initialize() @message = @add = true end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Override method missing to allow for matching the results of a method on message against some queries.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sup_tag.rb', line 83 def method_missing(method, *args, &block) super if !respond_to?(method) parts = split_args(args) queries = parts.first results = Array(@message.send(method)) count = match_args(results, queries) if count.size == queries.size @labels ||= [] @multi &= true @labels.concat(parts.last.compact) if @add else @multi = false end end |
Instance Method Details
#archive(&block) ⇒ Array
Archive a message. This adds the matching tags and removes the inbox tag.
26 27 28 29 30 |
# File 'lib/sup_tag.rb', line 26 def archive(&block) tag(&block) remove(:inbox) if @labels return @message.labels end |
#cloaker(&b) ⇒ Block
Instance eval for blocks stolen from Trollop. Orignally from: redhanded.hobix.com/inspect/aBlockCostume.html, which now seems to be down.
instance context.
67 68 69 70 71 72 73 74 |
# File 'lib/sup_tag.rb', line 67 def cloaker(&b) (class << self; self; end).class_eval do define_method :cloaker_, &b meth = instance_method :cloaker_ remove_method :cloaker_ meth end end |
#multi(*labels, &block) ⇒ Array
Test queries across several feilds to add tags.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sup_tag.rb', line 48 def multi(*labels, &block) @multi = true @add = false cloaker(&block).bind(self).call @add = true if @multi @labels.concat(labels) @match = true end return @message.labels end |
#remove(tags) ⇒ Array
Remove the given tags from the message.
16 17 18 19 |
# File 'lib/sup_tag.rb', line 16 def remove() Array().each { |t| @message.remove_label(t) } return @message.labels end |
#respond_to?(method, include_private = false) ⇒ Boolean
Override to include methods from messages.
77 78 79 |
# File 'lib/sup_tag.rb', line 77 def respond_to?(method, include_private = false) return @message.respond_to?(method) || super end |
#tag(&block) ⇒ Array
Tag a message.
36 37 38 39 40 41 |
# File 'lib/sup_tag.rb', line 36 def tag(&block) @labels = nil cloaker(&block).bind(self).call @labels.each { |l| @message.add_label(l) } if @labels return @message.labels end |