Navigation Menu

Skip to content

Commit

Permalink
Add geo_in_rectangle method in select.rb.
Browse files Browse the repository at this point in the history
  • Loading branch information
komainu8 committed Apr 27, 2017
1 parent d78ea25 commit 36407ea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/groonga/client/request/select.rb
Expand Up @@ -93,6 +93,11 @@ def query(value)
# filter.in_values(:tags, "tag1", "tag2")
# # -> --filter 'in_values(tags, "tag1", "tag2")'
#
# @example: Use geo_in_rectangle function
# request.
# filter.geo_in_rectangle("50x50", "0x100", "100x0")
# # -> --filter 'geo_in_rectangle("50x50", "0x100", "100x0")'
#
# @example Use geo_in_circle function
# request.
# filter.geo_in_circle(:location, "100x100", 300)
Expand Down Expand Up @@ -218,6 +223,39 @@ def initialize(request)
@request = request
end

# Adds a `geo_in_rectangle` condition then return a new `select`
# request object.
#
# @see http://groonga.org/docs/reference/functions/geo_in_rectangle.html
# geo_in_rectangle function in the Groonga document
#
# @example: Basic usage
# request.
# filter.geo_in_rectangle("50x50", "0x100", "100x0").
# # -> --filter 'in_values(50x50, 0x100, 100x0)'
#
# @param point [String] The point to be checked.
# `"#{LONGITUDE}x#{LATITUDE}"` is the point format.
#
# @param top_left [String] The top left of the condition rectangle.
#
# @param bottom_right [String] The bottom right of the condition rectangle.
#
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
#
# @since 0.5.0
def geo_in_rectangle(point, top_left, bottom_right)
expression = "geo_in_rectangle(%{point}"
expression << ", %{top_left}"
expression << ", %{bottom_right}"
expression << ")"
@request.filter(expression,
point: point,
top_left: top_left,
bottom_right: bottom_right)
end

# Adds a `geo_in_circle` condition then returns a new `select`
# request object.
#
Expand Down
14 changes: 14 additions & 0 deletions test/request/select/test-filter.rb
Expand Up @@ -202,6 +202,20 @@ def test_empty_string
end

sub_test_case("Filter") do
sub_test_case("#geo_in_rectangle") do
def geo_in_rectangle(*args)
@request.filter.geo_in_rectangle(*args).to_parameters
end

test("point") do
assert_equal({
:table => "posts",
:filter => "geo_in_rectangle(\"50x50\", \"0x100\", \"100x0\")",
},
geo_in_rectangle("50x50", "0x100", "100x0"))
end
end

sub_test_case("#geo_in_circle") do
def geo_in_circle(*args)
@request.filter.geo_in_circle(*args).to_parameters
Expand Down

0 comments on commit 36407ea

Please sign in to comment.