Class: SupTag

Inherits:
Object show all
Defined in:
lib/sup_tag.rb

Overview

Class to make tagging simple.

Instance Method Summary collapse

Constructor Details

#initialize(message) ⇒ SupTag

Create a new SupTag.

Parameters:

  • message (Redwood::Message)

    A Message to tag.



7
8
9
10
# File 'lib/sup_tag.rb', line 7

def initialize(message)
  @message = 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.

Parameters:

  • block (Block)

    Block to add tags.

Returns:

  • (Array)

    Tags on the message.



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.

Parameters:

  • b (Block)

    A block to bind.

Returns:

  • (Block)

    The given block bound so it will eval in this



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.

Parameters:

  • labels (Symbol, Array)

    Labels to add.

  • Block (Block)

    for adding tags.

Returns:

  • (Array)

    The tags on the message



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.

Parameters:

  • tags (Symbol, Array)

    Tags to remove.

Returns:

  • (Array)

    The tags on the message.



16
17
18
19
# File 'lib/sup_tag.rb', line 16

def remove(tags)
  Array(tags).each { |t| @message.remove_label(t) }
  return @message.labels
end

#respond_to?(method, include_private = false) ⇒ Boolean

Override to include methods from messages.

Returns:

  • (Boolean)


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.

Parameters:

  • Block (Block)

    for adding tags.

Returns:

  • (Array)

    The tags on the 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