[Groonga-commit] ranguba/groonga-client at 52cd8b8 [master] Add Client#request for using request object

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Mar 1 14:52:55 JST 2017


Kouhei Sutou	2017-03-01 14:52:55 +0900 (Wed, 01 Mar 2017)

  New Revision: 52cd8b82438ef7360c4c84026225687eeb8de6cf
  https://github.com/ranguba/groonga-client/commit/52cd8b82438ef7360c4c84026225687eeb8de6cf

  Message:
    Add Client#request for using request object

  Modified files:
    lib/groonga/client.rb
    lib/groonga/client/request.rb
    lib/groonga/client/request/select.rb
    test/test-client.rb

  Modified: lib/groonga/client.rb (+22 -0)
===================================================================
--- lib/groonga/client.rb    2017-03-01 14:39:00 +0900 (46a33e5)
+++ lib/groonga/client.rb    2017-03-01 14:52:55 +0900 (d9c93d4)
@@ -176,6 +176,28 @@ module Groonga
       execute_command(command, &block)
     end
 
+    def request(name)
+      command_name_module = Module.new do
+        define_method :command_name do
+          name
+        end
+      end
+
+      client = self
+      open_client_module = Module.new do
+        define_method :open_client do |&block|
+          block.call(client)
+        end
+      end
+
+      extensions = [
+        command_name_module,
+        open_client_module,
+      ]
+      request_class = Request.find(name)
+      request_class.new(nil, extensions)
+    end
+
     def method_missing(name, *args, &block)
       if groonga_command_name?(name)
         execute(name, *args, &block)

  Modified: lib/groonga/client/request.rb (+17 -0)
===================================================================
--- lib/groonga/client/request.rb    2017-03-01 14:39:00 +0900 (2676bcb)
+++ lib/groonga/client/request.rb    2017-03-01 14:52:55 +0900 (e08fe79)
@@ -14,6 +14,23 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
+module Groonga
+  class Client
+    module Request
+      @@requests = {}
+      class << self
+        def register(request_class)
+          @@requests[request_class.command_name] = request_class
+        end
+
+        def find(name)
+          @@requests[name.to_s] || Generic
+        end
+      end
+    end
+  end
+end
+
 require "groonga/client/script-syntax"
 
 require "groonga/client/request/error"

  Modified: lib/groonga/client/request/select.rb (+2 -0)
===================================================================
--- lib/groonga/client/request/select.rb    2017-03-01 14:39:00 +0900 (d94a963)
+++ lib/groonga/client/request/select.rb    2017-03-01 14:52:55 +0900 (a17d6b5)
@@ -26,6 +26,8 @@ module Groonga
           end
         end
 
+        Request.register(self)
+
         def initialize(table_or_parameters, extensions=[])
           if table_or_parameters.respond_to?(:to_parameters)
             parameters = table_or_parameters

  Modified: test/test-client.rb (+11 -1)
===================================================================
--- test/test-client.rb    2017-03-01 14:39:00 +0900 (5311687)
+++ test/test-client.rb    2017-03-01 14:52:55 +0900 (c065447)
@@ -1,5 +1,5 @@
 # Copyright (C) 2013  Haruka Yoshihara <yoshihara �� clear-code.com>
-# Copyright (C) 2013-2016  Kouhei Sutou <kou �� clear-code.com>
+# Copyright (C) 2013-2017  Kouhei Sutou <kou �� clear-code.com>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -300,6 +300,15 @@ JSON
     end
   end
 
+  module RequestTests
+    def test_generic
+      expected_response = {"key" => "value"}
+      stub_response(expected_response.to_json)
+      response = client.request(:status).response
+      assert_equal(expected_response, response.body)
+    end
+  end
+
   module Tests
     include Utils
     include Assertions
@@ -310,6 +319,7 @@ JSON
     include OpenTests
     include LoadTests
     include DefaultOptionsTests
+    include RequestTests
   end
 
   class TestGQTP < self
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index