From 0cb40b86f2f3ec930674dc24ff6eb41f5dbe4ab6 Mon Sep 17 00:00:00 2001 From: Gibheer Date: Wed, 8 Jun 2011 08:14:03 +0200 Subject: [PATCH] it is now building a small block out of the term parameters --- lib/polecat/index_searcher.rb | 28 +++------------------------- lib/polecat/term.rb | 24 ++++++++++++++++++++++-- spec/term/new_spec.rb | 11 +++++++++++ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/polecat/index_searcher.rb b/lib/polecat/index_searcher.rb index 08726c2..0a7796c 100644 --- a/lib/polecat/index_searcher.rb +++ b/lib/polecat/index_searcher.rb @@ -42,12 +42,12 @@ module Polecat # @param [String] query a String which get's matched against the documents # @return [Array] a list of all matching documents def search query - @reader.read.select do |doc| + @content = @reader.read if @content.nil? + @content.select do |doc| #doc.attributes.fetch(@default_field).fetch(:value) == query rs = [] query.terms.each do |term| - val = doc.send(term.field.to_sym) - if compare val, term.operator, term.value + if term.compare(doc.send(term.field)) rs << true end end @@ -58,27 +58,5 @@ module Polecat end end end - - # compare the document value with the searched value - # - # This compares the two values with the operator - # @return [Any] trueish for matches or falsey - # @private - def compare ival, op, tval - if op == :eq - if tval.class == Regexp - ival.match tval - else - ival == tval - end - elsif op == :gt - ival < tval - elsif op == :lt - ival > tval - else - false - end - end - private :compare end end diff --git a/lib/polecat/term.rb b/lib/polecat/term.rb index ca176ae..b952f92 100644 --- a/lib/polecat/term.rb +++ b/lib/polecat/term.rb @@ -12,10 +12,30 @@ module Polecat @field = field @operator = operator if @operator == :eq && value.class == String - @value = /^#{value}$/ + value = /^#{value}$/ else - @value = value + value = value end + @value = value + + method_string = 'def compare(inval);' + if value.class == Regexp + if operator == :eq + method_string += " inval.match(@value);" + else + raise ArgumentError, "operation #{operator} does not work with a regexp" + end + else + if operator == :eq + method_string += " inval == @value;" + elsif operator == :lt + method_string += " inval < @value;" + elsif operator == :gt + method_string += " inval > @value;" + end + end + method_string += 'end;' + self.instance_eval method_string end end end diff --git a/spec/term/new_spec.rb b/spec/term/new_spec.rb index c846262..17411c2 100644 --- a/spec/term/new_spec.rb +++ b/spec/term/new_spec.rb @@ -13,6 +13,17 @@ describe "Term#new" do t.value.should == /^foo$/ end + it "creates a method #compare with one argument after initialization" do + t = Polecat::Term.new :name, :eq, "foo" + t.respond_to?(:compare).should == true + t.method(:compare).arity.should == 1 + end + + it "raises an error if a regexp shall be compared for larger or minor" do + lambda { Polecat::Term.new(:name, :lt, /foo/) }.should( + raise_error(ArgumentError)) + end + it "raises an error if no argument is given" do lambda { Polecat::Term.new }.should raise_error end