0
0
Fork 0

it is now building a small block out of the term parameters

This commit is contained in:
Gibheer 2011-06-08 08:14:03 +02:00
parent 41ec65a7a2
commit 0cb40b86f2
3 changed files with 36 additions and 27 deletions

View File

@ -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

View File

@ -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

View File

@ -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