Navigation Menu

Skip to content

Commit

Permalink
Add geo_in_circle method in select.rb.
Browse files Browse the repository at this point in the history
  • Loading branch information
komainu8 committed Apr 26, 2017
1 parent ab55abb commit 440f420
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
68 changes: 68 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_circle function
# request.
# filter.geo_in_circle("0x0", "100x100", 300)
# # -> --filter 'geo_in_circle("0x0", "100x100", 300, "rectangle")'
#
# @example Use between function
# request.
# filter.between("age", 19, "include", 32, "include")
Expand Down Expand Up @@ -187,6 +192,44 @@ def initialize(request)
@request = request
end

# Adds a `geo_in_circle` condition then return a new `select`
# request object.
#
# @example Basic usage
# request.
# filter.geo_in_circle("0x0", "100x100", 300).
# # -> --filter 'geo_in_circle("0x0", "100x100", 300, "rectangle")'
#
# @see http://groonga.org/docs/reference/functions/geo_in_circle.html
# geo_in_circle function in the Groonga document
#
# @param point [String] Specify point for confirm whether to exit in circle or not.
#
# @param center [String] This value that center of circle.
#
# @param radious [Integer] This value that radious of circle.
#
# @param approximate_type ["rectangle", "sphere", "ellopsoid"]
# This value that type of approximate of geographical.
# If it is nil, approximate_type value is `"rectangle"`.
# If it is `"rectangle"`, calcurate distance from radious
# by approximate of rectangle.
# If it is `"sphere"`, calcurate distance from radious
# by approximate of sphere.
# If it is `"ellopsoid"`, calcurate distance from radious
# by approximate of ellopsoid.
#
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
#
# @since 0.4.4
def geo_in_circle(point, center, radious_or_point, approximate_type="rectangle")
parameter = FilterGeoInCircleParameter.new(point,
center, radious_or_point,
approximate_type)
add_parameter(FilterMerger, parameter)
end

# Adds a `between` condition then return a new `select`
# request object.
#
Expand Down Expand Up @@ -506,6 +549,30 @@ def initialize(expression, values)
end

# @private
class FilterGeoInCircleParameter
include ScriptSyntaxValueEscapable

def initialize(point,
center, radious,
approximate_type)
@point = point
@center = center
@radious = radious
@approximate_type = approximate_type
end

def to_parameters
filter = "geo_in_circle(#{escape_script_syntax_value(@point)}"
filter << ", #{escape_script_syntax_value(@center)}"
filter << ", #{escape_script_syntax_value(@radious)}"
filter << ", #{escape_script_syntax_value(@approximate_type)}"
filter << ")"
{
filter: filter,
}
end
end

class FilterBetweenParameter
include ScriptSyntaxValueEscapable

Expand All @@ -532,6 +599,7 @@ def to_parameters
end
end

# @private
class FilterInValuesParameter
include ScriptSyntaxValueEscapable

Expand Down
18 changes: 18 additions & 0 deletions test/request/test-select.rb
Expand Up @@ -92,6 +92,24 @@ def filter(*args)
}))
end

sub_test_case("#geo_in_circle") do
def geo_in_circle(point,
center, radious_or_point,
approximate_type="rectangle")
@request.filter.geo_in_circle(point,
center, radious_or_point,
approximate_type).to_parameters
end

test("approximate type") do
assert_equal({
:table => "posts",
:filter => "geo_in_circle(\"100x100\", \"140x250\", 300, \"rectangle\")",
},
geo_in_circle("100x100", "140x250", 300, "rectangle"))
end
end

sub_test_case("#between") do
def between(column_name,
min, min_border,
Expand Down

0 comments on commit 440f420

Please sign in to comment.