[Groonga-commit] ranguba/groonga-client at f899d06 [master] schema response: add convenience []

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Wed Oct 25 15:54:05 JST 2017


Kouhei Sutou	2017-10-25 15:54:05 +0900 (Wed, 25 Oct 2017)

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

  Message:
    schema response: add convenience []

  Modified files:
    lib/groonga/client/response/schema.rb
    test/response/test-schema.rb

  Modified: lib/groonga/client/response/schema.rb (+19 -2)
===================================================================
--- lib/groonga/client/response/schema.rb    2017-10-25 15:38:12 +0900 (81eca7d)
+++ lib/groonga/client/response/schema.rb    2017-10-25 15:54:05 +0900 (f85583f)
@@ -96,8 +96,25 @@ module Groonga
           @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

  Modified: test/response/test-schema.rb (+133 -0)
===================================================================
--- test/response/test-schema.rb    2017-10-25 15:38:12 +0900 (df87ea2)
+++ test/response/test-schema.rb    2017-10-25 15:54:05 +0900 (dea3daa)
@@ -40,6 +40,139 @@ class TestResponseSchema < Test::Unit::TestCase
       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 = {
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171025/0f1c2bde/attachment-0001.htm 



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