Navigation Menu

Skip to content

Commit

Permalink
schema response: add convenience []
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 25, 2017
1 parent a1a179c commit f899d06
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/groonga/client/response/schema.rb
Expand Up @@ -96,8 +96,25 @@ def tables
@tables
end

private
def coerce_tables
# @param name [String] The object name to be retrieved.
#
# @return [Plugin, Type, Tokenizer, Normalizer, TokenFilter, Table, Column]
# The object named `name`.
#
# @since 0.5.3
def [](name)
name = name.to_s if name.is_a?(Symbol)
if name.include?(".")
table_name, column_name = name.split(".", 2)
tables[table_name].columns[column_name]
else
tables[name] ||
types[name] ||
tokenizers[name] ||
normalizers[name] ||
token_filters[name] ||
plugins[name]
end
end

module HashValueConverter
Expand Down
133 changes: 133 additions & 0 deletions test/response/test-schema.rb
Expand Up @@ -40,6 +40,139 @@ def create_response(body)
Groonga::Client::Response::Schema.new(@command, header, body)
end

class TestReferenceShortCut < self
def test_plugin
body = {
"plugins" => {
"token_filters/stop_word" => {
"name" => "token_filters/stop_word",
},
},
"types" => {},
"tokenizers" => {},
"normalizers" => {},
"token_filters" => {},
"tables" => {},
}
response = create_response(body)
assert_equal({"name" => "token_filters/stop_word"},
response["token_filters/stop_word"])
end

def test_type
body = {
"plugins" => {},
"types" => {
"ShortText" => {
"name" => "ShortText",
},
},
"tokenizers" => {},
"normalizers" => {},
"token_filters" => {},
"tables" => {},
}
response = create_response(body)
assert_equal({"name" => "ShortText"},
response["ShortText"])
end

def test_tokenizer
body = {
"plugins" => {},
"types" => {},
"tokenizers" => {
"TokenBigram" => {
"name" => "TokenBigram",
},
},
"normalizers" => {},
"token_filters" => {},
"tables" => {},
}
response = create_response(body)
assert_equal({"name" => "TokenBigram"},
response["TokenBigram"])
end

def test_normalizer
body = {
"plugins" => {},
"types" => {},
"tokenizers" => {},
"normalizers" => {
"NormalizerAuto" => {
"name" => "NormalizerAuto",
},
},
"token_filters" => {},
"tables" => {},
}
response = create_response(body)
assert_equal({"name" => "NormalizerAuto"},
response["NormalizerAuto"])
end

def test_token_filters
body = {
"plugins" => {},
"types" => {},
"tokenizers" => {},
"normalizers" => {},
"token_filters" => {
"TokenFilterStopWord" => {
"name" => "TokenFilterStopWord",
},
},
"tables" => {},
}
response = create_response(body)
assert_equal({"name" => "TokenFilterStopWord"},
response["TokenFilterStopWord"])
end

def test_table
body = {
"plugins" => {},
"types" => {},
"tokenizers" => {},
"normalizers" => {},
"token_filters" => {},
"tables" => {
"Users" => {
"name" => "Users",
},
},
}
response = create_response(body)
assert_equal({"name" => "Users"},
response["Users"])
end

def test_column
body = {
"plugins" => {},
"types" => {},
"tokenizers" => {},
"normalizers" => {},
"token_filters" => {},
"tables" => {
"Users" => {
"name" => "Users",
"columns" => {
"age" => {
"name" => "age",
},
},
},
},
}
response = create_response(body)
assert_equal({"name" => "age"},
response["Users.age"])
end
end

class TestTable < self
def test_key_type
body = {
Expand Down

0 comments on commit f899d06

Please sign in to comment.