[Groonga-commit] ranguba/groonga-client-rails at 71b9c31 [master] Create request class for select command

Back to archive index

Kouhei Sutou null+****@clear*****
Sun Mar 27 18:29:33 JST 2016


Kouhei Sutou	2016-03-27 18:29:33 +0900 (Sun, 27 Mar 2016)

  New Revision: 71b9c31c7827f3491c483b4c61bc746b5c410970
  https://github.com/ranguba/groonga-client-rails/commit/71b9c31c7827f3491c483b4c61bc746b5c410970

  Message:
    Create request class for select command

  Added files:
    lib/groonga/client/searcher/raw_request.rb
    lib/groonga/client/searcher/select.rb
    lib/groonga/client/searcher/select/request.rb
    lib/groonga/client/searcher/select/result_set.rb
  Removed files:
    lib/groonga/client/searcher/request.rb
    lib/groonga/client/searcher/result_set.rb
  Modified files:
    lib/groonga/client/searcher.rb
  Renamed files:
    test/unit/searcher/select/match_columns_parameter_test.rb
      (from test/unit/request_test.rb)

  Modified: lib/groonga/client/searcher.rb (+6 -3)
===================================================================
--- lib/groonga/client/searcher.rb    2016-03-27 18:27:19 +0900 (7f097c1)
+++ lib/groonga/client/searcher.rb    2016-03-27 18:29:33 +0900 (470e5fd)
@@ -14,8 +14,7 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require "groonga/client/searcher/request"
-require "groonga/client/searcher/result_set"
+require "groonga/client/searcher/select"
 require "groonga/client/searcher/schema"
 require "groonga/client/searcher/schema_synchronizer"
 require "groonga/client/searcher/source"
@@ -139,6 +138,10 @@ module Groonga
       end
 
       def search
+        select
+      end
+
+      def select
         schema = self.class.schema
         full_text_searchable_column_names = []
         schema.columns.each do |name, column|
@@ -146,7 +149,7 @@ module Groonga
             full_text_searchable_column_names << name
           end
         end
-        TableRequest.new(schema.table).
+        Select::Request.new(schema.table).
           match_columns(full_text_searchable_column_names)
       end
 

  Added: lib/groonga/client/searcher/raw_request.rb (+83 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/searcher/raw_request.rb    2016-03-27 18:29:33 +0900 (6ba1db9)
@@ -0,0 +1,83 @@
+# Copyright (C) 2016  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
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# 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
+    class Searcher
+      class RawRequest
+        def initialize(command_name, parameters=nil)
+          @command_name = command_name
+          @parameters = parameters
+        end
+
+        def response
+          @reponse ||= Client.open do |client|
+            client.execute(@command_name, to_parameters)
+          end
+        end
+
+        def parameter(name, value)
+          add_parameter(OverwriteParameters,
+                        RequestParameter.new(name, value))
+        end
+
+        def to_parameters
+          if****@param*****?
+            {}
+          else
+            @parameters.to_parameters
+          end
+        end
+
+        private
+        def add_parameter(merger_class, parameter)
+          merger = merger_class.new(@parameters, parameter)
+          create_request(merger)
+        end
+
+        def create_request(parameters)
+          self.class.new(@command_name, parameters)
+        end
+      end
+
+      class RequestParameter
+        def initialize(name, value)
+          @name = name
+          @value = value
+        end
+
+        def to_parameters
+          {
+            @name => @value,
+          }
+        end
+      end
+
+      class ParameterMerger
+        def initialize(parameters1, parameters2)
+          @parameters1 = parameters1
+          @parameters2 = parameters2
+        end
+      end
+
+      class OverwriteMerger < ParameterMerger
+        def to_parameters
+          @parameters1.to_parameters.merge(@parameters2.to_parameters)
+        end
+      end
+    end
+  end
+end

  Deleted: lib/groonga/client/searcher/request.rb (+0 -123) 100644
===================================================================
--- lib/groonga/client/searcher/request.rb    2016-03-27 18:27:19 +0900 (8968b7a)
+++ /dev/null
@@ -1,123 +0,0 @@
-# Copyright (C) 2016  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
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-
-require "active_support/core_ext/object/blank"
-
-module Groonga
-  class Client
-    class Searcher
-      class Request
-        def result_set
-          @result_set ||= Client.open do |client|
-            ResultSet.new(client.select(to_parameters))
-          end
-        end
-
-        def match_columns(value)
-          OverwriteRequest.new(self,
-                               MatchColumnsRequest.new(value))
-        end
-
-        def query(value)
-          QueryMergeRequest.new(self,
-                                QueryRequest.new(value))
-        end
-      end
-
-      class TableRequest < Request
-        def initialize(table)
-          @table = table
-        end
-
-        def to_parameters
-          {
-            table: @table,
-          }
-        end
-      end
-
-      # @private
-      class OverwriteRequest < Request
-        def initialize(request1, request2)
-          @request1 = request1
-          @request2 = request2
-        end
-
-        def to_parameters
-          @request1.to_parameters.merge(@request2.to_parameters)
-        end
-      end
-
-      # @private
-      class QueryMergeRequest < Request
-        def initialize(request1, request2)
-          @request1 = request1
-          @request2 = request2
-        end
-
-        def to_parameters
-          params1 =****@reque*****_parameters
-          params2 =****@reque*****_parameters
-          params = params1.merge(params2)
-          query1 = params1[:query]
-          query2 = params2[:query]
-          if query1.present? and query2.present?
-            params[:query] = "(#{query1}) (#{query2})"
-          else
-            params[:query] = (query1 || query2)
-          end
-          params
-        end
-      end
-
-      class MatchColumnsRequest < Request
-        def initialize(match_columns)
-          @match_columns = match_columns
-        end
-
-        def to_parameters
-          if @match_columns.blank?
-            {}
-          else
-            case @match_columns
-            when ::Array
-              match_columns = @match_columns.join(", ")
-            when Symbol
-              match_columns = @match_columns.to_s
-            else
-              match_columns = @match_columns
-            end
-            {
-              match_columns: match_columns,
-            }
-          end
-        end
-      end
-
-      class QueryRequest < Request
-        def initialize(query)
-          @query = query
-        end
-
-        def to_parameters
-          {
-            query: @query,
-          }
-        end
-      end
-    end
-  end
-end

  Deleted: lib/groonga/client/searcher/result_set.rb (+0 -74) 100644
===================================================================
--- lib/groonga/client/searcher/result_set.rb    2016-03-27 18:27:19 +0900 (26a7ea8)
+++ /dev/null
@@ -1,74 +0,0 @@
-# Copyright (C) 2016  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
-# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# 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
-    class Searcher
-      class ResultSet
-        def initialize(response)
-          @response = response
-        end
-
-        def n_hits
-          @response.n_hits
-        end
-        # For Kaminari
-        alias_method :total_count, :n_hits
-
-        # For Kaminari
-        def limit_value
-          (@response.command[:limit] || 10).to_i
-        end
-
-        # For Kaminari
-        def offset_value
-          (@response.command[:offset] || 0).to_i
-        end
-
-        def records
-          @response.records
-        end
-
-        def sources
-          @sources ||= fetch_sources
-        end
-
-        def drilldowns
-          @response.drilldowns
-        end
-
-        private
-        def fetch_sources
-          source_ids = {}
-          records.collect do |record|
-            model_name, id = record["_key"].split(/-/, 2)
-            source_ids[model_name] ||= []
-            source_ids[model_name] << id
-          end
-          sources = {}
-          source_ids.each do |model_name, ids|
-            model_name.constantize.find(ids).each_with_index do |model, i|
-              sources["#{model_name}-#{ids[i]}"] = model
-            end
-          end
-          records.collect do |record|
-            sources[record["_key"]]
-          end
-        end
-      end
-    end
-  end
-end

  Added: lib/groonga/client/searcher/select.rb (+18 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/searcher/select.rb    2016-03-27 18:29:33 +0900 (c6e2de7)
@@ -0,0 +1,18 @@
+# Copyright (C) 2016  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
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "groonga/client/searcher/select/request"
+require "groonga/client/searcher/select/result_set"

  Added: lib/groonga/client/searcher/select/request.rb (+100 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/searcher/select/request.rb    2016-03-27 18:29:33 +0900 (4e4edb0)
@@ -0,0 +1,100 @@
+# Copyright (C) 2016  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
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "active_support/core_ext/object/blank"
+
+require "groonga/client/searcher/raw_request"
+
+module Groonga
+  class Client
+    class Searcher
+      module Select
+        class Request < RawRequest
+          def initialize(table_or_parameters)
+            if table_or_parameters.respond_to?(:to_parameters)
+              parameters = table_or_parameters
+            else
+              table_name = table_or_parameters
+              parameters = RequestParameter.new(:table, table_name)
+            end
+            super("select", parameters)
+          end
+
+          def result_set
+            @result_set ||= ResultSet.new(response)
+          end
+
+          def match_columns(value)
+            add_parameter(OverwriteMerger,
+                          MatchColumnsParameter.new(value))
+          end
+
+          def query(value)
+            add_parameter(QueryMerger,
+                          RequestParameter.new(:query, value))
+          end
+
+          private
+          def create_request(parameters)
+            self.class.new(parameters)
+          end
+        end
+
+        # @private
+        class QueryMerger < ParameterMerger
+          def to_parameters
+            params1 =****@param*****_parameters
+            params2 =****@param*****_parameters
+            params = params1.merge(params2)
+            query1 = params1[:query]
+            query2 = params2[:query]
+            if query1.present? and query2.present?
+              params[:query] = "(#{query1}) (#{query2})"
+            else
+              params[:query] = (query1 || query2)
+            end
+            params
+          end
+        end
+
+        # @private
+        class MatchColumnsParameter
+          def initialize(match_columns)
+            @match_columns = match_columns
+          end
+
+          def to_parameters
+            if @match_columns.blank?
+              {}
+            else
+              case @match_columns
+              when ::Array
+                match_columns = @match_columns.join(", ")
+              when Symbol
+                match_columns = @match_columns.to_s
+              else
+                match_columns = @match_columns
+              end
+              {
+                match_columns: match_columns,
+              }
+            end
+          end
+        end
+      end
+    end
+  end
+end

  Added: lib/groonga/client/searcher/select/result_set.rb (+76 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/client/searcher/select/result_set.rb    2016-03-27 18:29:33 +0900 (5105514)
@@ -0,0 +1,76 @@
+# Copyright (C) 2016  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
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# 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
+    class Searcher
+      module Select
+        class ResultSet
+          def initialize(response)
+            @response = response
+          end
+
+          def n_hits
+            @response.n_hits
+          end
+          # For Kaminari
+          alias_method :total_count, :n_hits
+
+          # For Kaminari
+          def limit_value
+            (@response.command[:limit] || 10).to_i
+          end
+
+          # For Kaminari
+          def offset_value
+            (@response.command[:offset] || 0).to_i
+          end
+
+          def records
+            @response.records
+          end
+
+          def sources
+            @sources ||= fetch_sources
+          end
+
+          def drilldowns
+            @response.drilldowns
+          end
+
+          private
+          def fetch_sources
+            source_ids = {}
+            records.collect do |record|
+              model_name, id = record["_key"].split(/-/, 2)
+              source_ids[model_name] ||= []
+              source_ids[model_name] << id
+            end
+            sources = {}
+            source_ids.each do |model_name, ids|
+              model_name.constantize.find(ids).each_with_index do |model, i|
+                sources["#{model_name}-#{ids[i]}"] = model
+              end
+            end
+            records.collect do |record|
+              sources[record["_key"]]
+            end
+          end
+        end
+      end
+    end
+  end
+end

  Renamed: test/unit/searcher/select/match_columns_parameter_test.rb (+10 -10) 68%
===================================================================
--- test/unit/request_test.rb    2016-03-27 18:27:19 +0900 (50dd40a)
+++ test/unit/searcher/select/match_columns_parameter_test.rb    2016-03-27 18:29:33 +0900 (03ae32f)
@@ -14,46 +14,46 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-require_relative "test_helper"
+require "test_helper"
 
-class MatchColumnsRequestTest < Test::Unit::TestCase
-  def match_columns_request(match_columns)
-    Groonga::Client::Searcher::MatchColumnsRequest.new(match_columns)
+class SearcherSelectMatchColumnsParameterTest < Test::Unit::TestCase
+  def match_columns_parameter(match_columns)
+    Groonga::Client::Searcher::Select::MatchColumnsParameter.new(match_columns)
   end
 
   def test_nil
     assert_equal({},
-                 match_columns_request(nil).to_parameters)
+                 match_columns_parameter(nil).to_parameters)
   end
 
   def test_string
     assert_equal({
                    :match_columns => "title",
                  },
-                 match_columns_request("title").to_parameters)
+                 match_columns_parameter("title").to_parameters)
   end
 
   def test_empty_string
     assert_equal({},
-                 match_columns_request("").to_parameters)
+                 match_columns_parameter("").to_parameters)
   end
 
   def test_symbol
     assert_equal({
                    :match_columns => "title",
                  },
-                 match_columns_request(:title).to_parameters)
+                 match_columns_parameter(:title).to_parameters)
   end
 
   def test_array
     assert_equal({
                    :match_columns => "title, body",
                  },
-                 match_columns_request(["title", "body"]).to_parameters)
+                 match_columns_parameter(["title", "body"]).to_parameters)
   end
 
   def test_empty_array
     assert_equal({},
-                 match_columns_request([]).to_parameters)
+                 match_columns_parameter([]).to_parameters)
   end
 end
-------------- next part --------------
HTML����������������������������...
Télécharger 



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