Class: Lumix::LookupFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/lumix/lookup_filter.rb,
lib/lumix/proto/lookup_filter.rb

Defined Under Namespace

Classes: Filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter, &result_proc) ⇒ LookupFilter

Returns a new instance of LookupFilter.



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

def initialize(lookup, filter, &result_proc)
  @filter = filter
  @result_proc = result_proc

  @filters = create_filters(lookup, filter)
  @results = 0
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



4
5
6
# File 'lib/lumix/lookup_filter.rb', line 4

def filter
  @filter
end

#resultsObject (readonly)

Returns the value of attribute results.



4
5
6
# File 'lib/lumix/lookup_filter.rb', line 4

def results
  @results
end

Instance Method Details

#<<(result) ⇒ Object



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

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

#apply(lookup, &block) ⇒ Object



21
22
23
24
25
# File 'lib/lumix/lookup_filter.rb', line 21

def apply(lookup, &block)
  lookup.find(@filters) do |range|
    block[*range] if block and range
  end
end

#create_filters(lookup, filter) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/lumix/lookup_filter.rb', line 27

def create_filters(lookup, filter)
  filter.scan(/(?:(?:\"([^\"]+)\")|(\S+))+/).map do |word, tag|
    word_re = to_re(word)
    tag_re = to_re(tag)
    word_ids = lookup.find_word(word_re) if word_re
    tag_ids = lookup.find_tag(tag_re) if tag_re
    Filter.new(word_ids, tag_ids)
  end
end

#create_re(filter) ⇒ Object



29
30
31
32
33
# File 'lib/lumix/proto/lookup_filter.rb', line 29

def create_re(filter)
  filter.scan(/(?:(?:\"([^\"]+)\")|(\S+))+/).map do |word, tag|
    word ? [:word, to_re(word)] : [:tag, to_re(tag)]
  end
end

#to_re(txt) ⇒ Object



37
38
39
40
# File 'lib/lumix/lookup_filter.rb', line 37

def to_re(txt)
  return nil if txt.nil? || txt.empty?
  Regexp.new('^' + txt.gsub(/\s/, '_').gsub(/\*/, '\S*').gsub(/\?/, '\S') + '$')
end