Navigation Menu

Skip to content

Commit

Permalink
between: make min_border and max_border are optional
Browse files Browse the repository at this point in the history
GitHub: fix #17

Reported by okkez. Thanks!!!
  • Loading branch information
kou committed Apr 27, 2017
1 parent d78ea25 commit cc82c6e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 30 deletions.
82 changes: 59 additions & 23 deletions lib/groonga/client/request/select.rb
Expand Up @@ -199,7 +199,6 @@ def paginated?
parameters.key?(:offset) and parameters.key?(:limit)
end

# @since 0.4.3
class Filter
class << self
# @private
Expand Down Expand Up @@ -286,37 +285,72 @@ def geo_in_circle(column_name_or_point,
# Adds a `between` condition then returns a new `select`
# request object.
#
# @example Basic usage
# request.
# filter.between(:age, 19, "include", 32, "exclude").
# # -> --filter 'between(age, 19, "include", 32, "exclude")'
#
# @see http://groonga.org/docs/reference/functions/between.html
# between function in the Groonga document
#
# @param column_name [String, Symbol] The target column name.
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
#
# @overload between(column_name, min, max, min_border: "include", max_border: "include")
#
# @param min [Integer] The minimal value of the condition
# range.
# @example Basic usage
# request.
# filter.between(:age, 19, 32)
# # -> --filter 'between(age, 19, "include", 32, "exclude")'
#
# @param min_border ["include", "exclude"] Whether `min` is
# included or not. If `"include"` is specified, `min` is
# included. If `"exclude"` is specified, `min` isn't
# included.
# @!macro [new] between_common
#
# @param max [Integer] The maximum value of the condition
# range.
# @param column_name [Symbol] The target column name.
#
# @param max_border ["include", "exclude"] Whether `max` is
# included or not. If `"include"` is specified, `max` is
# included. If `"exclude"` is specified, `max` isn't
# included.
# @param min [Integer] The minimal value of the
# condition range.
#
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
# @param max [Integer] The maximum value of the
# condition range.
#
# @param min_border ["include", "exclude"] Whether `min` is
# included or not. If `"include"` is specified, `min` is
# included. If `"exclude"` is specified, `min` isn't
# included.
#
# @since 0.4.4
def between(column_name, min, min_border, max, max_border)
# @param max_border ["include", "exclude"] Whether `max` is
# included or not. If `"include"` is specified, `max` is
# included. If `"exclude"` is specified, `max` isn't
# included.
#
# @macro between_common
#
# @since 0.5.0
#
# @overload between(column_name, min, min_border, max, max_border)
#
# @example Basic usage
# request.
# filter.between(:age, 19, "include", 32, "exclude")
# # -> --filter 'between(age, 19, "include", 32, "exclude")'
#
# @macro between_common
#
# @since 0.4.4
def between(*args)
n_args = args.size
case n_args
when 3
column_name, min, max = args
min_border = "include"
max_border = "include"
when 4
column_name, min, max, options = args
min_border = options[:min_border] || "include"
max_border = options[:max_border] || "include"
when 5
column_name, min, min_border, max, max_border = args
else
message =
"wrong number of arguments (given #{n_args}, expected 3..5)"
raise ArgumentError, message
end

# TODO: Accept not only column name but also literal as
# the first argument.
column_name =
Expand Down Expand Up @@ -362,6 +396,8 @@ def between(column_name, min, min_border, max, max_border)
#
# @return [Groonga::Client::Request::Select]
# The new request with the given condition.
#
# @since 0.4.3
def in_values(column_name, *values)
return @request if values.empty?

Expand Down
28 changes: 21 additions & 7 deletions test/request/select/test-filter.rb
Expand Up @@ -233,15 +233,29 @@ def geo_in_circle(*args)
end

sub_test_case("#between") do
def between(column_name,
min, min_border,
max, max_border)
@request.filter.between(column_name,
min, min_border,
max, max_border).to_parameters
def between(*args)
@request.filter.between(*args).to_parameters
end

test("border") do
test("min and max") do
assert_equal({
:table => "posts",
:filter => "between(ages, 2, \"include\", 29, \"include\")",
},
between(:ages, 2, 29))
end

test("min, max and options") do
assert_equal({
:table => "posts",
:filter => "between(ages, 2, \"exclude\", 29, \"exclude\")",
},
between(:ages, 2, 29,
min_border: "exclude",
max_border: "exclude"))
end

test("all") do
assert_equal({
:table => "posts",
:filter => "between(ages, 2, \"include\", 29, \"exclude\")",
Expand Down

0 comments on commit cc82c6e

Please sign in to comment.