Class: Lumix::Filter

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(suffix, filter, &result_proc) ⇒ Filter

Returns a new instance of Filter.



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

def initialize(suffix, filter, &result_proc)
  @suffix = suffix.gsub(/\\\|/, '[\|]') # workaround to make handle_dangling_tags play nicely
  @filter = filter
  @result_proc = result_proc

  @re = to_re(filter)
  @results = 0
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



7
8
9
# File 'lib/lumix/filter.rb', line 7

def filter
  @filter
end

#resultsObject (readonly)

Returns the value of attribute results.



7
8
9
# File 'lib/lumix/filter.rb', line 7

def results
  @results
end

Instance Method Details

#<<(result) ⇒ Object



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

def <<(result)
  @results += 1
  @result_proc[*result] if @result_proc
end

#ensure_wordbounds(re) ⇒ Object



84
85
86
# File 'lib/lumix/filter.rb', line 84

def ensure_wordbounds(re)
  re # ending wordbounds is being taken of earlier
end

#handle_choice(re) ⇒ Object

Takes (!A B C) and transforms it



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

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

#handle_dangling_tags(re) ⇒ Object

add wildcard word match on tag-only search criteria



69
70
71
72
73
74
75
76
77
# File 'lib/lumix/filter.rb', line 69

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

#handle_literals(re) ⇒ Object

transforms literals delimited by “”



60
61
62
63
64
65
66
# File 'lib/lumix/filter.rb', line 60

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



80
81
82
# File 'lib/lumix/filter.rb', line 80

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

#handle_wildcard(re) ⇒ Object

character wildcard replacement



47
48
49
# File 'lib/lumix/filter.rb', line 47

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

#scan(text, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lumix/filter.rb', line 23

def scan(text, &block)
  results = []
  return results unless text
  (' ' + text + ' ').scan(@re) do |s|
    t_begin = $~.begin(0) - 1
    t_end = $~.end(0) - 1

    s = block ? block[s, t_begin, t_end, $~] : s
    results << s
  end
  results
end

#to_re(filter) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lumix/filter.rb', line 36

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