Class: IRC::Models::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/models/server.rb

Constant Summary collapse

DEFAULT_PORT =
6667

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Server

Creates a new server object with a hostname, an optional port and optinal password, that belongs to a network.

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/irc/models/server.rb', line 46

def initialize(attributes)
  
  raise ArgumentError.new("Can't create a new server object. The :network attribute is missing.") if attributes[:network] == nil
  @network = attributes[:network]
  
  raise ArgumentError.new("Can't create a new server object. The :hostname attribute is missing.") if attributes[:hostname] == nil
  @hostname = attributes[:hostname].strip
  
  @port = attributes[:port] || DEFAULT_PORT
  
  self.password = attributes[:password]
  self.last_connect = attributes[:last_connect]
  self.last_disconnect = attributes[:last_disconnect]
  
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



37
38
39
# File 'lib/irc/models/server.rb', line 37

def hostname
  @hostname
end

#last_connectObject

Returns the value of attribute last_connect.



41
42
43
# File 'lib/irc/models/server.rb', line 41

def last_connect
  @last_connect
end

#last_disconnectObject

Returns the value of attribute last_disconnect.



42
43
44
# File 'lib/irc/models/server.rb', line 42

def last_disconnect
  @last_disconnect
end

#networkObject (readonly)

Returns the value of attribute network.



36
37
38
# File 'lib/irc/models/server.rb', line 36

def network
  @network
end

#passwordObject

Returns the value of attribute password.



40
41
42
# File 'lib/irc/models/server.rb', line 40

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



38
39
40
# File 'lib/irc/models/server.rb', line 38

def port
  @port
end

Instance Method Details

#==(other) ⇒ Object



62
63
64
# File 'lib/irc/models/server.rb', line 62

def ==(other)
  return eql?(other)
end

#eql?(other) ⇒ Boolean

Returns true if the other Server object is equal to the current.

Returns:

  • (Boolean)


68
69
70
# File 'lib/irc/models/server.rb', line 68

def eql?(other)
  return other.instance_of?(Server) && network == other.network && hostname == other.hostname && port == other.port    
end

#hashObject



72
73
74
# File 'lib/irc/models/server.rb', line 72

def hash
  return key.hash
end

#keyObject



76
77
78
# File 'lib/irc/models/server.rb', line 76

def key
  return "#{network.name}|#{hostname}|#{port}".downcase
end

#merge(other) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/irc/models/server.rb', line 80

def merge(other)
  
  #      @network = other.network
  @hostname = other.hostname
  @port = other.port
  
  self.password = other.password
  self.last_connect = other.last_connect
  self.last_disconnect = other.last_disconnect
  
end

#to_sObject

Returns the name of the server. If the port is different from the default IRC port the port number is also appended.



94
95
96
# File 'lib/irc/models/server.rb', line 94

def to_s
  return hostname
end

#to_strObject

Returns the name of the server. If the port is different from the default IRC port the port number is also appended.



100
101
102
# File 'lib/irc/models/server.rb', line 100

def to_str
  return to_s
end

#to_xmlObject



104
105
106
107
108
109
110
# File 'lib/irc/models/server.rb', line 104

def to_xml
  server = REXML::Element.new("Server")
  server.add_element("Hostname").add_text(REXML::CData.new(hostname))
  server.add_element("Port").add_text(port.to_s)
  server.add_element("Password").add_text(REXML::CData.new(password)) if password != nil      
  return server
end