[Groonga-commit] groonga/groonga-command at 00d5610 [fix-travis-ci-error] Extract format as class

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Jan 13 17:20:57 JST 2016


Kouhei Sutou	2013-09-13 11:07:15 +0900 (Fri, 13 Sep 2013)

  New Revision: 00d5610d3a86c05a964030e0bf4c36e4ac583780
  https://github.com/groonga/groonga-command/commit/00d5610d3a86c05a964030e0bf4c36e4ac583780

  Message:
    Extract format as class

  Added files:
    lib/groonga/command/format/command.rb
    lib/groonga/command/format/uri.rb
  Modified files:
    lib/groonga/command/base.rb

  Modified: lib/groonga/command/base.rb (+4 -35)
===================================================================
--- lib/groonga/command/base.rb    2013-09-13 10:52:08 +0900 (6fc4e68)
+++ lib/groonga/command/base.rb    2013-09-13 11:07:15 +0900 (f7043af)
@@ -16,8 +16,8 @@
 # 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 "English"
-require "cgi"
+require "groonga/command/format/uri"
+require "groonga/command/format/command"
 
 module Groonga
   module Command
@@ -79,42 +79,11 @@ module Groonga
       end
 
       def to_uri_format
-        path = "/d/#{@name}"
-        arguments =****@argum*****
-        output_type = arguments.delete(:output_type)
-        path << ".#{output_type}" if output_type
-        unless arguments.empty?
-          sorted_arguments = arguments.sort_by do |name, _|
-            name.to_s
-          end
-          uri_arguments = sorted_arguments.collect do |name, value|
-            "#{CGI.escape(name.to_s)}=#{CGI.escape(value)}"
-          end
-          path << "?"
-          path << uri_arguments.join("&")
-        end
-        path
+        Format::URI.new(@name, @arguments).path
       end
 
       def to_command_format
-        command_line = [@name]
-        sorted_arguments =****@argum*****_by do |name, _|
-          name.to_s
-        end
-        sorted_arguments.each do |name, value|
-          escaped_value = value.gsub(/[\n"\\]/) do
-            special_character = $MATCH
-            case special_character
-            when "\n"
-              "\\n"
-            else
-              "\\#{special_character}"
-            end
-          end
-          command_line << "--#{name}"
-          command_line << "\"#{escaped_value}\""
-        end
-        command_line.join(" ")
+        Format::Command.new(@name, @arguments).command_line
       end
 
       private

  Added: lib/groonga/command/format/command.rb (+53 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/command/format/command.rb    2013-09-13 11:07:15 +0900 (fee2ff0)
@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2012-2013  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 "English"
+
+module Groonga
+  module Command
+    module Format
+      class Command
+        def initialize(name, arguments)
+          @name = name
+          @arguments = arguments
+        end
+
+        def command_line
+          components = [@name]
+          sorted_arguments =****@argum*****_by do |name, _|
+            name.to_s
+          end
+          sorted_arguments.each do |name, value|
+            escaped_value = value.gsub(/[\n"\\]/) do
+              special_character = $MATCH
+              case special_character
+              when "\n"
+                "\\n"
+              else
+                "\\#{special_character}"
+              end
+            end
+            components << "--#{name}"
+            components << "\"#{escaped_value}\""
+          end
+          components.join(" ")
+        end
+      end
+    end
+  end
+end

  Added: lib/groonga/command/format/uri.rb (+50 -0) 100644
===================================================================
--- /dev/null
+++ lib/groonga/command/format/uri.rb    2013-09-13 11:07:15 +0900 (ca14a1d)
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2012-2013  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 "cgi"
+
+module Groonga
+  module Command
+    module Format
+      class URI
+        def initialize(name, arguments)
+          @name = name
+          @arguments = arguments
+        end
+
+        def path
+          path = "/d/#{@name}"
+          arguments =****@argum*****
+          output_type = arguments.delete(:output_type)
+          path << ".#{output_type}" if output_type
+          unless arguments.empty?
+            sorted_arguments = arguments.sort_by do |name, _|
+              name.to_s
+            end
+            uri_arguments = sorted_arguments.collect do |name, value|
+              "#{CGI.escape(name.to_s)}=#{CGI.escape(value)}"
+            end
+            path << "?"
+            path << uri_arguments.join("&")
+          end
+          path
+        end
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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