Class: Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/whale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



28
29
30
# File 'lib/whale.rb', line 28

def initialize()
  @stack = []
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



26
27
28
# File 'lib/whale.rb', line 26

def stack
  @stack
end

Instance Method Details

#filter(entry, logger) ⇒ Object

apply the stack to the entry tags



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/whale.rb', line 72

def filter(entry, logger)
  s = []
  last_tags = []
  eq = false
  @stack.each do |t|
    case t
    when :FILTER_AND then s << (s.pop & s.pop)
    when :FILTER_OR then s << (s.pop | s.pop)
    when :FILTER_NOT then s << !s.pop
    when :FILTER_EQ then eq = true
    else
      if eq
        logger.debug("last tags: #{last_tags}")
        match = false
        s.pop
        last_tags.each do |u|
          if t.match entry.tags[u]
            match = true 
            logger.debug("#{t} matches #{entry.tags[u]}")
            break
          end
        end
        s << match
        eq = false
      else
        last_tags = match_token entry, t
        s << !last_tags.empty?
      end
    end
  end
  logger.debug(s)
  logger.warn("Malformed filter") if s.length != 1
  return s.first
end

#match_token(entry, token) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/whale.rb', line 63

def match_token(entry, token)
  tags = []
  entry.tags.each do |t, v|
    tags << t if token.match t.to_s
  end
  return tags
end

#parse_filter(f, logger) ⇒ Object

For regex options (e.g. ignorecase) use the (?opt:source) notation, e.g. /(?i-mx:hEllo .*)/



34
35
36
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
# File 'lib/whale.rb', line 34

def parse_filter(f, logger)
  logger.debug("parsing #{f}")
  regex = false
  escape = false
  # literal = false
  token = 0
  stack = []
  (0...f.length).each do |i|
    if f[i] == '\\' or escape
      escape ^= true
    elsif f[i] == '/'
      if regex
        stack << Regexp.new(f[token, i - token])
        regex = false
      else
        token = i + 1
        regex = true
      end
    elsif !regex
      stack << :FILTER_AND if f[i] == '&'
      stack << :FILTER_OR if f[i] == '|'
      stack << :FILTER_NOT if f[i] == '*'
      stack << :FILTER_EQ if f[i] == '='
    end
  end
  logger.debug(stack)
  @stack = stack
end