Kouhei Sutou
null+****@clear*****
Mon Mar 21 13:01:17 JST 2016
Kouhei Sutou 2016-03-21 13:01:17 +0900 (Mon, 21 Mar 2016) New Revision: b118c855f93a47d2044e9cb06764d6e694972991 https://github.com/ranguba/groonga-client/commit/b118c855f93a47d2044e9cb06764d6e694972991 Message: Support dynamic command execution Modified files: lib/groonga/client.rb test/test-command.rb Modified: lib/groonga/client.rb (+49 -30) =================================================================== --- lib/groonga/client.rb 2016-03-21 12:56:11 +0900 (0a61f3c) +++ lib/groonga/client.rb 2016-03-21 13:01:17 +0900 (be2a890) @@ -130,43 +130,43 @@ module Groonga end def cache_limit(parameters, &block) - execute_command("cache_limit", parameters, &block) + execute("cache_limit", parameters, &block) end def check(parameters, &block) - execute_command("check", parameters, &block) + execute("check", parameters, &block) end def clearlock(parameters={}, &block) - execute_command("clearlock", parameters, &block) + execute("clearlock", parameters, &block) end def column_create(parameters, &block) - execute_command("column_create", parameters, &block) + execute("column_create", parameters, &block) end def column_list(parameters, &block) - execute_command("column_list", parameters, &block) + execute("column_list", parameters, &block) end def column_remove(parameters, &block) - execute_command("column_remove", parameters, &block) + execute("column_remove", parameters, &block) end def column_rename(parameters, &block) - execute_command("column_rename", parameters, &block) + execute("column_rename", parameters, &block) end def defrag(parameters={}, &block) - execute_command("defrag", parameters, &block) + execute("defrag", parameters, &block) end def delete(parameters, &block) - execute_command("delete", parameters, &block) + execute("delete", parameters, &block) end def dump(parameters={}, &block) - execute_command("dump", parameters, &block) + execute("dump", parameters, &block) end def load(parameters, &block) @@ -184,60 +184,78 @@ module Groonga json << "\n]" parameters[:values] = json end - execute_command("load", parameters, &block) + execute("load", parameters, &block) end def log_level(parameters, &block) - execute_command("log_level", parameters, &block) + execute("log_level", parameters, &block) end def log_put(parameters, &block) - execute_command("log_put", parameters, &block) + execute("log_put", parameters, &block) end def log_reopen(parameters={}, &block) - execute_command("log_reopen", parameters, &block) + execute("log_reopen", parameters, &block) end def quit(parameters={}, &block) - execute_command("quit", parameters, &block) + execute("quit", parameters, &block) end def register(parameters, &block) - execute_command("register", parameters, &block) + execute("register", parameters, &block) end def select(parameters, &block) - execute_command("select", parameters, &block) + execute("select", parameters, &block) end def shutdown(parameters={}, &block) end def status(parameters={}, &block) - execute_command("status", parameters, &block) + execute("status", parameters, &block) end def table_create(parameters, &block) - execute_command("table_create", parameters, &block) + execute("table_create", parameters, &block) end def table_list(parameters={}, &block) - execute_command("table_list", parameters, &block) + execute("table_list", parameters, &block) end def table_remove(parameters, &block) - execute_command("table_remove", parameters, &block) + execute("table_remove", parameters, &block) end - def table_rename(parameters, &block) + def execute(command_or_name, parameters={}, &block) + if command_or_name.is_a?(Command) + command = command_or_name + else + command_name = command_or_name + parameters = normalize_parameters(parameters) + command_class = Groonga::Command.find(command_name) + command = command_class.new(command_name, parameters) + end + execute_command(command, &block) end - def truncate(parameters, &block) + def method_missing(name, *args, **kwargs, &block) + if groonga_command_name?(name) + execute(name, *args, **kwargs, &block) + else + super + end end - def execute(command, &block) - Client::Command.new(command).execute(@connection, &block) + def respond_to_missing?(name, include_private) + if groonga_command_name?(name) + true + else + super + end end private @@ -285,11 +303,8 @@ module Groonga end end - def execute_command(command_name, parameters={}, &block) - parameters = normalize_parameters(parameters) - command_class = Groonga::Command.find(command_name) - command = command_class.new(command_name, parameters) - execute(command, &block) + def groonga_command_name?(name) + /\A[a-zA-Z][a-zA-Z\d_]+\z/ === name.to_s end def normalize_parameters(parameters) @@ -299,5 +314,9 @@ module Groonga end normalized_parameters end + + def execute_command(command, &block) + Client::Command.new(command).execute(@connection, &block) + end end end Modified: test/test-command.rb (+7 -7) =================================================================== --- test/test-command.rb 2016-03-21 12:56:11 +0900 (a26e71e) +++ test/test-command.rb 2016-03-21 13:01:17 +0900 (1e2f0d3) @@ -5,7 +5,7 @@ class TestCommand < Test::Unit::TestCase def test_column_create response = Object.new - mock(@client).execute_command("column_create", :table => :Test, :name => :Body, :type => :ShortText) do + mock(@client).execute("column_create", :table => :Test, :name => :Body, :type => :ShortText) do response end @client.column_create(:table => :Test, :name => :Body, :type => :ShortText) @@ -13,7 +13,7 @@ class TestCommand < Test::Unit::TestCase def test_column_list response = Object.new - mock(@client).execute_command("column_list", :table => :Test) do + mock(@client).execute("column_list", :table => :Test) do response end @client.column_list(:table => :Test) @@ -27,7 +27,7 @@ class TestCommand < Test::Unit::TestCase } ] response = Object.new - mock(@client).execute_command("load", :table => :Test, :values => values.to_json) do + mock(@client).execute("load", :table => :Test, :values => values.to_json) do response end @client.load(:table => :Test, :values => values.to_json) @@ -35,7 +35,7 @@ class TestCommand < Test::Unit::TestCase def test_select response = Object.new - mock(@client).execute_command("select", :table => :Test) do + mock(@client).execute("select", :table => :Test) do response end @client.select(:table => :Test) @@ -43,7 +43,7 @@ class TestCommand < Test::Unit::TestCase def test_table_create response = Object.new - mock(@client).execute_command("table_create", :name => :Test) do + mock(@client).execute("table_create", :name => :Test) do response end @client.table_create(:name => :Test) @@ -51,14 +51,14 @@ class TestCommand < Test::Unit::TestCase def test_table_list response = Object.new - mock(@client).execute_command("table_list", {}) do + mock(@client).execute("table_list", {}) do response end @client.table_list end def test_table_remove - mock(@client).execute_command("table_remove", :name => "Test") + mock(@client).execute("table_remove", :name => "Test") @client.table_remove(:name => "Test") end end -------------- next part -------------- HTML����������������������������... 下载