[Groonga-commit] ranguba/groonga-client-rails at 6271ee0 [master] Support sortby

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Sun Mar 27 23:48:48 JST 2016


Kouhei Sutou	2016-03-27 23:48:48 +0900 (Sun, 27 Mar 2016)

  New Revision: 6271ee0507761b98469d8cb56ff9bdf3b8f0c75b
  https://github.com/ranguba/groonga-client-rails/commit/6271ee0507761b98469d8cb56ff9bdf3b8f0c75b

  Message:
    Support sortby

  Added files:
    test/unit/searcher/select/sortby_parameter_test.rb
  Modified files:
    lib/groonga/client/searcher/select/request.rb

  Modified: lib/groonga/client/searcher/select/request.rb (+31 -0)
===================================================================
--- lib/groonga/client/searcher/select/request.rb    2016-03-27 23:43:23 +0900 (bae2088)
+++ lib/groonga/client/searcher/select/request.rb    2016-03-27 23:48:48 +0900 (90192b3)
@@ -57,6 +57,12 @@ module Groonga
                           OutputColumnsParameter.new(value))
           end
 
+          def sortby(value)
+            add_parameter(OverwriteMerger,
+                          SortbyParameter.new(value))
+          end
+          alias_method :sort, :sortby
+
           private
           def create_request(parameters)
             self.class.new(parameters)
@@ -213,6 +219,31 @@ module Groonga
             end
           end
         end
+
+        # @private
+        class SortbyParameter
+          def initialize(sortby)
+            @sortby = sortby
+          end
+
+          def to_parameters
+            if****@sortb*****?
+              {}
+            else
+              case @sortby
+              when ::Array
+                sortby =****@sortb*****(&:to_s).join(", ")
+              when Symbol
+                sortby =****@sortb*****_s
+              else
+                sortby = @sortby
+              end
+              {
+                sortby: sortby,
+              }
+            end
+          end
+        end
       end
     end
   end

  Added: test/unit/searcher/select/sortby_parameter_test.rb (+63 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/searcher/select/sortby_parameter_test.rb    2016-03-27 23:48:48 +0900 (014f77e)
@@ -0,0 +1,63 @@
+# 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 "test_helper"
+
+class SearcherSelectSortbyParameterTest < Test::Unit::TestCase
+  def sortby_parameter(sortby)
+    Groonga::Client::Searcher::Select::SortbyParameter.new(sortby)
+  end
+
+  def to_parameters(sortby)
+    sortby_parameter(sortby).to_parameters
+  end
+
+  def test_nil
+    assert_equal({},
+                 to_parameters(nil))
+  end
+
+  def test_string
+    assert_equal({
+                   :sortby => "-_score, _id",
+                 },
+                 to_parameters("-_score, _id"))
+  end
+
+  def test_empty_string
+    assert_equal({},
+                 to_parameters(""))
+  end
+
+  def test_symbol
+    assert_equal({
+                   :sortby => "_score",
+                 },
+                 to_parameters(:_score))
+  end
+
+  def test_array
+    assert_equal({
+                   :sortby => "-_score, _id",
+                 },
+                 to_parameters(["-_score", :_id]))
+  end
+
+  def test_empty_array
+    assert_equal({},
+                 to_parameters([]))
+  end
+end
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る