Class: GmailBritta::NegatedChainingFilter

Inherits:
ChainingFilter show all
Defined in:
lib/gmail-britta/chaining_filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ NegatedChainingFilter

Returns a new instance of NegatedChainingFilter


33
34
35
# File 'lib/gmail-britta/chaining_filter.rb', line 33

def initialize(parent)
  super
end

Instance Method Details

#deep_invert(has_not, has) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gmail-britta/chaining_filter.rb', line 54

def deep_invert(has_not, has)
  case
  when has_not.first.is_a?(Hash) && has_not.first[:or]
    has_not.first[:or] += has
    has_not
  when has_not.length > 0
    [{:or => has_not + has}]
  else
    has
  end
end

#invert(old) ⇒ Object


44
45
46
47
48
49
50
51
52
# File 'lib/gmail-britta/chaining_filter.rb', line 44

def invert(old)
  old.map! do |addr|
    if addr[0] == '-'
      addr[1..-1]
    else
      '-' + addr
    end
  end
end

#load(name, filter) ⇒ Object


38
39
40
41
42
# File 'lib/gmail-britta/chaining_filter.rb', line 38

def load(name, filter)
  filter.send("get_#{name}").reject do |elt|
    instance_variable_get("@#{name}").member?(elt)
  end
end

#perform_merge(filter) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gmail-britta/chaining_filter.rb', line 37

def perform_merge(filter)
  def load(name, filter)
    filter.send("get_#{name}").reject do |elt|
      instance_variable_get("@#{name}").member?(elt)
    end
  end

  def invert(old)
    old.map! do |addr|
      if addr[0] == '-'
        addr[1..-1]
      else
        '-' + addr
      end
    end
  end

  def deep_invert(has_not, has)
    case
    when has_not.first.is_a?(Hash) && has_not.first[:or]
      has_not.first[:or] += has
      has_not
    when has_not.length > 0
      [{:or => has_not + has}]
    else
      has
    end
  end

  @to += invert(load(:to, filter))
  @from += invert(load(:from, filter))
  @has_not += deep_invert(load(:has_not, filter), load(:has, filter))
end