Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #89 from ranguba/add-regexp-expression-builder
Browse files Browse the repository at this point in the history
Add RegexpExpressionBuilder
  • Loading branch information
kou committed Apr 8, 2015
2 parents 15d17ab + 2d18e2c commit 4f204e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/groonga/expression-builder.rb
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Masafumi Yokoyama <yokoyama@clear-code.com>
# Copyright (C) 2009-2012 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -173,7 +174,12 @@ def =~(other)
raise ArgumentError,
"match word should not be nil: #{full_column_name}"
end
MatchExpressionBuilder.new(self, normalize(other))

if other.is_a?(Regexp)
RegexpExpressionBuilder.new(self, normalize(other.source))
else
MatchExpressionBuilder.new(self, normalize(other))
end
end

def <(other)
Expand Down Expand Up @@ -332,6 +338,13 @@ def initialize(column_value_builder, value)
end
end

# @private
class RegexpExpressionBuilder < BinaryExpressionBuilder
def initialize(column_value_builder, value)
super(Groonga::Operation::REGEXP, column_value_builder, value)
end
end

# @private
class LessExpressionBuilder < BinaryExpressionBuilder
def initialize(column_value_builder, value)
Expand Down
18 changes: 18 additions & 0 deletions test/test-expression-builder.rb
Expand Up @@ -275,6 +275,24 @@ def setup_data
@users.add("ito", :name => "Takashi Ito")
end

class BlockTest < self
def test_match
result = @users.select do |record|
record["name"] =~ /sh/
end
assert_equal(["ito", "suzuki"],
result.collect {|record| record.key.key}.sort)
end

def test_not_match
result = @users.select do |record|
record["name"] =~ /abcabcabc/
end
assert_equal([],
result.collect {|record| record.key.key}.sort)
end
end

class QueryStringTest < self
def test_match
result = @users.select("name:~t")
Expand Down

0 comments on commit 4f204e2

Please sign in to comment.