Kouhei Sutou
null+****@clear*****
Sat Jul 19 21:13:54 JST 2014
Kouhei Sutou 2014-07-19 21:13:54 +0900 (Sat, 19 Jul 2014) New Revision: ef9acd48b67840a04fb273f3f3a45a9ed0a9f3c2 https://github.com/droonga/droonga-engine/commit/ef9acd48b67840a04fb273f3f3a45a9ed0a9f3c2 Message: Add address object for Droonga address format Added files: lib/droonga/address.rb test/unit/test_address.rb Added: lib/droonga/address.rb (+65 -0) 100644 =================================================================== --- /dev/null +++ lib/droonga/address.rb 2014-07-19 21:13:54 +0900 (1e0a945) @@ -0,0 +1,65 @@ +# Copyright (C) 2014 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +module Droonga + class Address + class << self + def parse(string) + if /\A(.+):(\d+)\/([^.]+)(?:\.(.+))?\z/ =~ string + components = { + :host => $1, + :port => $2.to_i, + :tag => $3, + :name => $4 + } + new(components) + else + format = "${host_name}:${port_number}/${tag}.${name}" + message = "volume address must be <#{format}> format: <#{string}>" + raise ArgumentError, message + end + end + end + + DEFAULT_HOST = "127.0.0.1" + DEFAULT_PORT = 10031 + DEFAULT_TAG = "droonga" + + attr_reader :host + attr_reader :port + attr_reader :tag + attr_reader :name + def initialize(components={}) + @host = components[:host] || DEFAULT_HOST + @port = components[:port] || DEFAULT_PORT + @tag = components[:tag] || DEFAULT_TAG + @name = components[:name] + end + + def to_s + string = "#{@host}:#{@port}/#{@tag}" + string << ".#{@name}" if @name + string + end + + def to_a + [@host, @port, @tag, @name] + end + + def ==(other) + other.is_a?(self.class) and to_a == other.to_a + end + end +end Added: test/unit/test_address.rb (+40 -0) 100644 =================================================================== --- /dev/null +++ test/unit/test_address.rb 2014-07-19 21:13:54 +0900 (5d83df0) @@ -0,0 +1,40 @@ +# Copyright (C) 2014 Droonga Project +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +require "droonga/address" + +class AddressTest < Test::Unit::TestCase + class ParseTest < self + def parse(string) + address = Droonga::Address.parse(string) + address.to_a + end + + def test_full + assert_equal(["192.168.0.1", 2929, "droonga", "name"], + parse("192.168.0.1:2929/droonga.name")) + end + + def test_internal_name + assert_equal(["192.168.0.1", 2929, "droonga", "#1"], + parse("192.168.0.1:2929/droonga.\#1")) + end + + def test_no_name + assert_equal(["192.168.0.1", 2929, "droonga", nil], + parse("192.168.0.1:2929/droonga")) + end + end +end -------------- next part -------------- HTML����������������������������... Download