Skip to content

Commit

Permalink
Extract format as class
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Sep 13, 2013
1 parent 35d6161 commit 00d5610
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 35 deletions.
39 changes: 4 additions & 35 deletions lib/groonga/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,42 +79,11 @@ def output_type
end

def to_uri_format
path = "/d/#{@name}"
arguments = @arguments.dup
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 = @arguments.sort_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
Expand Down
53 changes: 53 additions & 0 deletions lib/groonga/command/format/command.rb
Original file line number Diff line number Diff line change
@@ -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 = @arguments.sort_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
50 changes: 50 additions & 0 deletions lib/groonga/command/format/uri.rb
Original file line number Diff line number Diff line change
@@ -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 = @arguments.dup
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

0 comments on commit 00d5610

Please sign in to comment.