Navigation Menu

Skip to content

Commit

Permalink
Deprecate filter("column", value)
Browse files Browse the repository at this point in the history
Use filter(:column, value) instead.
  • Loading branch information
kou committed Apr 27, 2017
1 parent 24a53b4 commit 7501860
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 40 deletions.
42 changes: 24 additions & 18 deletions lib/groonga/client/request/select.rb
Expand Up @@ -121,7 +121,9 @@ def filter(*args)
else
expression = "%{column} == %{value}"
column_name = expression_or_column_name
column_name = column_name.to_sym if column_name.is_a?(String)
column_name = Filter.column_namify(column_name,
"first",
"#{self.class}\##{__method__}")
values = {
column: column_name,
value: values_or_value,
Expand Down Expand Up @@ -199,6 +201,19 @@ def paginated?

# @since 0.4.3
class Filter
class << self
# @private
def column_namify(column_name, ith, signature)
return column_name unless column_name.is_a?(String)

message = "column name (the #{ith} argument) of #{signature} "
message << "should be Symbol: #{column_name.inspect}: "
message << caller(2, 1)[0]
warn(message)
column_name.to_sym
end
end

def initialize(request)
@request = request
end
Expand Down Expand Up @@ -304,9 +319,10 @@ def geo_in_circle(column_name_or_point,
def between(column_name, min, min_border, max, max_border)
# TODO: Accept not only column name but also literal as
# the first argument.
column_name = column_namify(column_name,
"first",
"#{self.class}\##{__method__}")
column_name =
self.class.column_namify(column_name,
"first",
"#{self.class}\##{__method__}")
expression = "between(%{column_name}"
expression << ", %{min}"
expression << ", %{min_border}"
Expand Down Expand Up @@ -351,9 +367,10 @@ def in_values(column_name, *values)

# TODO: Accept not only column name but also literal as
# the first argument.
column_name = column_namify(column_name,
"first",
"#{self.class}\##{__method__}")
column_name =
self.class.column_namify(column_name,
"first",
"#{self.class}\##{__method__}")
expression_values = {column_name: column_name}
expression = "in_values(%{column_name}"
values.each_with_index do |value, i|
Expand All @@ -363,17 +380,6 @@ def in_values(column_name, *values)
expression << ")"
@request.filter(expression, expression_values)
end

private
def column_namify(column_name, ith, signature)
return column_name unless column_name.is_a?(String)

message = "column name (the #{ith} argument) of #{signature} "
message << "should be Symbol: #{column_name.inspect}: "
message << caller(2, 1)[0]
warn(message)
column_name.to_sym
end
end

class LabeledDrilldown
Expand Down
34 changes: 12 additions & 22 deletions test/request/select/test-filter.rb
Expand Up @@ -138,22 +138,12 @@ def test_empty_string
end
end

sub_test_case("column name") do
test("String") do
assert_equal({
:table => "posts",
:filter => "_key == 29",
},
filter("_key", 29))
end

test("Symbol") do
assert_equal({
:table => "posts",
:filter => "_key == 29",
},
filter(:_key, 29))
end
test("column name") do
assert_equal({
:table => "posts",
:filter => "_key == 29",
},
filter(:_key, 29))
end

sub_test_case("value") do
Expand All @@ -165,7 +155,7 @@ def test_empty_string
:table => "posts",
:filter => filter,
},
filter("title", "[\"He\\ llo\"]"))
filter(:title, "[\"He\\ llo\"]"))
end

sub_test_case("Symbol") do
Expand All @@ -174,15 +164,15 @@ def test_empty_string
:table => "posts",
:filter => "title == normalized_title",
},
filter("title", :normalized_title))
filter(:title, :normalized_title))
end

test("invalid ID") do
assert_equal({
:table => "posts",
:filter => "title == \"Hello World\"",
},
filter("title", :"Hello World"))
filter(:title, :"Hello World"))
end
end

Expand All @@ -191,23 +181,23 @@ def test_empty_string
:table => "posts",
:filter => "age == 29",
},
filter("age", 29))
filter(:age, 29))
end

test("true") do
assert_equal({
:table => "posts",
:filter => "published == true",
},
filter("published", true))
filter(:published, true))
end

test("false") do
assert_equal({
:table => "posts",
:filter => "published == false",
},
filter("published", false))
filter(:published, false))
end
end

Expand Down

0 comments on commit 7501860

Please sign in to comment.