[Groonga-commit] ranguba/groonga-client at 8b7826b [master] Add Groonga::Client.default_options and Groonga::Client.default_options=

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Mar 21 16:29:35 JST 2016


Kouhei Sutou	2016-03-21 16:29:35 +0900 (Mon, 21 Mar 2016)

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

  Message:
    Add Groonga::Client.default_options and Groonga::Client.default_options=

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

  Modified: lib/groonga/client.rb (+18 -1)
===================================================================
--- lib/groonga/client.rb    2016-03-21 13:07:11 +0900 (aedb2aa)
+++ lib/groonga/client.rb    2016-03-21 16:29:35 +0900 (b47f98f)
@@ -28,6 +28,23 @@ require "groonga/client/script-syntax"
 module Groonga
   class Client
     class << self
+      @@deafult_options = {}
+
+      # @return [Hash] The default options for {Groonga::Client.new}.
+      #
+      # @since 0.2.0
+      def default_options
+        @@deafult_options
+      end
+
+      # @param [Hash] options The new default options for
+      #   {Groonga::Client.new}.
+      #
+      # @since 0.2.0
+      def default_options=(options)
+        @@deafult_options = options
+      end
+
       # @!macro [new] initialize_options
       #   @param [Hash] options The options.
       #   @option options [String, URI::Generic, URI::HTTP, URI::HTTPS]
@@ -71,7 +88,7 @@ module Groonga
 
     # @macro initialize_options
     def initialize(options={})
-      options = options.dup
+      options = self.class.default_options.merge(options)
       url = options[:url] || build_url(options)
       url = URL.parse(url) unless url.is_a?(URI::Generic)
       options[:url] = url

  Modified: test/test-client.rb (+30 -3)
===================================================================
--- test/test-client.rb    2016-03-21 13:07:11 +0900 (d22ef8b)
+++ test/test-client.rb    2016-03-21 16:29:35 +0900 (68f0781)
@@ -47,7 +47,7 @@ class TestClient < Test::Unit::TestCase
       @client ||= open_client
     end
 
-    def open_client(&block)
+    def open_options
       url = "#{@protocol}://"
       if @user and @password
         url << URI.encode_www_form_component(@user)
@@ -56,8 +56,11 @@ class TestClient < Test::Unit::TestCase
         url << "@"
       end
       url << "#{@address}:#{@port}"
-      options = {:url => URI.parse(url)}
-      Groonga::Client.open(options, &block)
+      {:url => URI.parse(url)}
+    end
+
+    def open_client(&block)
+      Groonga::Client.open(open_options, &block)
     end
 
     def stub_response(body, output_type=:json)
@@ -274,6 +277,29 @@ JSON
     end
   end
 
+  module DefaultOptionsTests
+    def test_default_options
+      change_default_options(open_options) do
+        expected_response = {"key" => "value"}
+        stub_response(expected_response.to_json)
+        response = Groonga::Client.open do |client|
+          client.status
+        end
+        assert_equal(expected_response, response.body)
+      end
+    end
+
+    def change_default_options(options)
+      default_options = Groonga::Client.default_options
+      begin
+        Groonga::Client.default_options = options
+        yield
+      ensure
+        Groonga::Client.default_options = default_options
+      end
+    end
+  end
+
   module Tests
     include Utils
     include Assertions
@@ -283,6 +309,7 @@ JSON
     include ParametersTests
     include OpenTests
     include LoadTests
+    include DefaultOptionsTests
   end
 
   class TestGQTP < self
-------------- next part --------------
HTML����������������������������...
Download 



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