Class: Lumix::LookupFilter
- Inherits:
-
Object
- Object
- Lumix::LookupFilter
- Defined in:
- lib/lumix/lookup_filter.rb,
lib/lumix/proto/lookup_filter.rb
Defined Under Namespace
Classes: Filter
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #<<(result) ⇒ Object
- #apply(lookup, &block) ⇒ Object
- #create_filters(lookup, filter) ⇒ Object
- #create_re(filter) ⇒ Object
-
#initialize(filter, &result_proc) ⇒ LookupFilter
constructor
A new instance of LookupFilter.
- #to_re(txt) ⇒ Object
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
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
4 5 6 |
# File 'lib/lumix/lookup_filter.rb', line 4 def filter @filter end |
#results ⇒ Object (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 |