Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #19 from komainu8/feature/add_geo_in_rectangle
Browse files Browse the repository at this point in the history
Add geo_in_rectangle method in `select.rb`

Patch by komainu8. Thanks!!!
  • Loading branch information
kou committed Apr 27, 2017
2 parents cc82c6e + c22047f commit 21cf21b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
58 changes: 58 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(:location, "0x100", "100x0")
# # -> --filter 'geo_in_rectangle(location, "0x100", "100x0")'
#
# @example Use geo_in_circle function
# request.
# filter.geo_in_circle(:location, "100x100", 300)
Expand Down Expand Up @@ -217,6 +222,59 @@ 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
#
# @overload geo_in_rectangle(column_name, top_left, bottom_right)
#
# @example: Basic usage
# request.
# filter.geo_in_rectangle(:location, "0x100", "100x0").
# # -> --filter 'geo_in_rectangle(location, "0x100", "100x0")'
#
# @param column_name [Symbol] The column name to be checked.
#
# @!macro [new] geo_in_rectangle
#
# @param top_left [String] The top left of the condition rectangle.
# `"#{LONGITUDE}x#{LATITUDE}"` is the point format.
#
# @param bottom_right [String] The bottom right of the condition rectangle.
# `"#{LONGITUDE}x#{LATITUDE}"` is the point format.
#
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
#
# @macro geo_in_rectangle
#
# @overload geo_in_rectangle(point, top_left, bottom_right)
#
# @example Basic usage
# request.
# filter.geo_in_rectangle("50x50", "0x100", "100x0").
# # -> --filter 'geo_in_rectangle("50x50", "0x100", "100x0")'
#
# @param point [String] The point to be checked.
# `"#{LONGITUDE}x#{LATITUDE}"` is the point format.
#
# @macro geo_in_rectangle
#
# @since 0.5.0
def geo_in_rectangle(column_name_or_point,
top_left, bottom_right)
expression = "geo_in_rectangle(%{column_name_or_point}"
expression << ", %{top_left}"
expression << ", %{bottom_right}"
expression << ")"
@request.filter(expression,
column_name_or_point: column_name_or_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
22 changes: 22 additions & 0 deletions test/request/select/test-filter.rb
Expand Up @@ -202,6 +202,28 @@ 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("column") do
assert_equal({
:table => "posts",
:filter => "geo_in_rectangle(location, \"0x100\", \"100x0\")",
},
geo_in_rectangle(:location, "0x100", "100x0"))
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 21cf21b

Please sign in to comment.