Module: Filter

Defined in:
lib/lumix/filter.rb

Constant Summary collapse

HANDLERS =
%w[handle_wildcard handle_choice handle_literals
handle_dangling_tags handle_multiplicators assure_wordbounds]

Class Method Summary collapse

Class Method Details

.assure_wordbounds(re) ⇒ Object



55
56
57
# File 'lib/lumix/filter.rb', line 55

def assure_wordbounds(re)
  '\b' + re + '\b'
end

.handle_choice(re) ⇒ Object

Takes (!A B C) and transforms it



24
25
26
27
28
29
# File 'lib/lumix/filter.rb', line 24

def handle_choice(re)
  re.gsub(/\(\!([^\)]+)\)/) do
    c = $1.split.map{ |t| '(?!' + t + '\b)' }.join
    '(?:' + c + '\S)*'
  end
end

.handle_dangling_tags(re) ⇒ Object

add wildcard word match on tag-only search criteria



41
42
43
44
45
46
47
48
49
# File 'lib/lumix/filter.rb', line 41

def handle_dangling_tags(re)
  re.split(/ /).map do |s|
    if s['\|']
      s
    else
      s.gsub(/(\(?)(\S+)/, '\1[^\s\|]+\|\2')
    end
  end.join('\s+')
end

.handle_literals(re) ⇒ Object

transforms literals delimited by “”



32
33
34
35
36
37
38
# File 'lib/lumix/filter.rb', line 32

def handle_literals(re)
  re.gsub(/\"([^\"]*)\"(?:\|(\S+?))?/) do
    str = $1
    tag = $2 || '\S+?'
    str.gsub(/ /, '_') + '\|' + tag
  end
end

.handle_multiplicators(re) ⇒ Object

Handles the + * ? and {} qualifiers



51
52
53
# File 'lib/lumix/filter.rb', line 51

def handle_multiplicators(re)    
  re.gsub(/\(([^\)]+)(\)((\{[^\}]+\})|\*|\+|\?)\s?)/, '(?:\b\1\b\2')
end

.handle_wildcard(re) ⇒ Object

character wildcard replacement



19
20
21
# File 'lib/lumix/filter.rb', line 19

def handle_wildcard(re)
  re.gsub(/([^\)])\*/, '\1[^\b]*?')
end

.to_re(filter) ⇒ Object

TODO refactor



8
9
10
11
12
13
14
15
16
# File 'lib/lumix/filter.rb', line 8

def to_re(filter)
  re = HANDLERS.inject(filter) do |filter, handler|
    puts filter
    puts "#{handler} -->"
    send handler, filter
  end
  puts re
  Regexp.new(re)
end