Class: Droonga::NodeName

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/node_name.rb

Constant Summary collapse

DEFAULT_HOST =
Socket.gethostname
DEFAULT_PORT =
10031
DEFAULT_TAG =
"droonga"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components = {}) ⇒ NodeName

Returns a new instance of NodeName.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/droonga/node_name.rb', line 56

def initialize(components={})
  if components.is_a?(self.class)
    node_name = components
    components = {
      :host => node_name.host,
      :port => node_name.port,
      :tag  => node_name.tag,
    }
  end

  @host = components[:host] || DEFAULT_HOST
  @port = components[:port] || DEFAULT_PORT
  @tag  = components[:tag]  || DEFAULT_TAG
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



52
53
54
# File 'lib/droonga/node_name.rb', line 52

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



53
54
55
# File 'lib/droonga/node_name.rb', line 53

def port
  @port
end

#tagObject (readonly)

Returns the value of attribute tag.



54
55
56
# File 'lib/droonga/node_name.rb', line 54

def tag
  @tag
end

Class Method Details

.parse(string) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/droonga/node_name.rb', line 20

def parse(string)
  return string if string.is_a?(self)

  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

.valid?(string) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/droonga/node_name.rb', line 37

def valid?(string)
  begin
    parse(string)
    true
  rescue ArgumentError
    false
  end
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
86
87
88
# File 'lib/droonga/node_name.rb', line 83

def ==(other)
  if other.is_a?(String)
    return to_s == other
  end
  other.is_a?(self.class) and to_a == other.to_a
end

#nodeObject



75
76
77
# File 'lib/droonga/node_name.rb', line 75

def node
  "#{@host}:#{@port}/#{@tag}"
end

#to_aObject



79
80
81
# File 'lib/droonga/node_name.rb', line 79

def to_a
  [@host, @port, @tag]
end

#to_sObject



71
72
73
# File 'lib/droonga/node_name.rb', line 71

def to_s
  node
end