[Groonga-commit] droonga/droonga-engine at 05789a8 [master] Define class to represent node name

Back to archive index

YUKI Piro Hiroshi null+****@clear*****
Tue Mar 24 23:32:24 JST 2015


YUKI "Piro" Hiroshi	2015-03-24 23:32:24 +0900 (Tue, 24 Mar 2015)

  New Revision: 05789a84b177cfb3fb3ec285ecd662682c1eb44b
  https://github.com/droonga/droonga-engine/commit/05789a84b177cfb3fb3ec285ecd662682c1eb44b

  Message:
    Define class to represent node name

  Added files:
    lib/droonga/node_name.rb

  Added: lib/droonga/node_name.rb (+67 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/node_name.rb    2015-03-24 23:32:24 +0900 (654adf8)
@@ -0,0 +1,67 @@
+# Copyright (C) 2015
+# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "droonga/address"
+
+module Droonga
+  class NodeName
+    class << self
+      def parse(string)
+        if /\A(.+):(\d+)\/([^.]+)\z/ =~ string
+          components = {
+            :host => $1,
+            :port => $2.to_i,
+            :tag  => $3,
+          }
+          new(components)
+        else
+          format = "${host_name}:${port_number}/${tag}"
+          message = "node name must be <#{format}> format: <#{string}>"
+          raise ArgumentError, message
+        end
+      end
+
+      def valid?(string)
+        begin
+          parse(string)
+          true
+        rescue ArgumentError
+          false
+        end
+      end
+    end
+
+    attr_reader :host
+    attr_reader :port
+    attr_reader :tag
+
+    def initialize(components={})
+      @host = components[:host] || Address::DEFAULT_HOST
+      @port = components[:port] || Address::DEFAULT_PORT
+      @tag  = components[:tag]  || Address::DEFAULT_TAG
+    end
+
+    def to_s
+      "#{@host}:#{@port}/#{@tag}"
+    end
+
+    def to_a
+      [@host, @port, @tag]
+    end
+
+    def ==(other)
+      other.is_a?(self.class) and to_a == other.to_a
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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