Class: IRC::Models::Network

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Network

Returns a new instance of Network.

Raises:

  • (ArgumentError)


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

def initialize(attributes)
  
  raise ArgumentError.new("Can't create a new network object. The :name attribute is missing.") if attributes[:name] == nil
  @name = attributes[:name].strip
  
  self.description = attributes[:description]
  self.last_connect = attributes[:last_connect]
  self.last_disconnect = attributes[:last_disconnect]
  self.last_observation_start = attributes[:last_observation_start]
  self.last_observation_stop = attributes[:last_observation_stop]
  
  @bots = Hash.new
  @channels = Hash.new
  @servers = Hash.new
  @users = Hash.new      
  
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#last_connectObject

Returns the value of attribute last_connect.



41
42
43
# File 'lib/irc/models/network.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/network.rb', line 42

def last_disconnect
  @last_disconnect
end

#last_observation_startObject

Returns the value of attribute last_observation_start.



43
44
45
# File 'lib/irc/models/network.rb', line 43

def last_observation_start
  @last_observation_start
end

#last_observation_stopObject

Returns the value of attribute last_observation_stop.



44
45
46
# File 'lib/irc/models/network.rb', line 44

def last_observation_stop
  @last_observation_stop
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



244
245
246
# File 'lib/irc/models/network.rb', line 244

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

#add_bot(bot) ⇒ Object

Adds a bot to the network and returns the bot.



65
66
67
68
# File 'lib/irc/models/network.rb', line 65

def add_bot(bot)
  add_user(bot)
  return @bots[bot.key] = bot
end

#add_channel(channel) ⇒ Object

Adds a channel to the network and returns the channel.



71
72
73
# File 'lib/irc/models/network.rb', line 71

def add_channel(channel)
  return @channels[channel.key] = channel
end

#add_server(server) ⇒ Object

Adds a server to the network and returns the server.



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

def add_server(server)
  return @servers[server.key] = server
end

#add_user(user) ⇒ Object

Adds an user to the network and returns the user.



81
82
83
# File 'lib/irc/models/network.rb', line 81

def add_user(user)
  return @users[user.key] = user
end

#botsObject

Returns all bots that are associated with this network.



86
87
88
# File 'lib/irc/models/network.rb', line 86

def bots
  return @bots.values
end

#channelsObject

Returns all channels that are associated with this network.



91
92
93
# File 'lib/irc/models/network.rb', line 91

def channels
  return @channels.values
end

#create_bot(nick, login = nil, hostname = nil) ⇒ Object

Creates a bot, adds it to the network and returns the new bot.



96
97
98
# File 'lib/irc/models/network.rb', line 96

def create_bot(nick,  = nil, hostname = nil) 
  return add_bot(Bot.new(:network => self, :nick => nick, :login => , :hostname => hostname))
end

#create_channel(name, password = nil) ⇒ Object

Creates a channel, adds it to the network and returns the new channel.



101
102
103
# File 'lib/irc/models/network.rb', line 101

def create_channel(name , password = nil) 
  return add_channel(Channel.new(:network => self, :name => name, :password => password))
end

#create_server(hostname, port = Server::DEFAULT_PORT) ⇒ Object

Creates a server, adds it to the network and returns the new server.



106
107
108
# File 'lib/irc/models/network.rb', line 106

def create_server(hostname, port = Server::DEFAULT_PORT) 
  return add_server(Server.new(:network => self, :hostname => hostname, :port => port))
end

#create_user(nick, login = nil, hostname = nil) ⇒ Object

Creates a user, adds it to the network and returns the new user.



111
112
113
# File 'lib/irc/models/network.rb', line 111

def create_user(nick,  = nil, hostname = nil) 
  return add_user(User.new(:network => self, :nick => nick, :login => , :hostname => hostname))
end

#eql?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


116
117
118
# File 'lib/irc/models/network.rb', line 116

def eql?(other)
  return other.instance_of?(Network) && name == other.name    
end

#hashObject



126
127
128
# File 'lib/irc/models/network.rb', line 126

def hash
  return key.hash
end

#keyObject



130
131
132
# File 'lib/irc/models/network.rb', line 130

def key
  return name.downcase
end

#lookup_bot(nick) ⇒ Object

Returns a bot object by its nick name or nil if there is no such bot associated with the network.



153
154
155
# File 'lib/irc/models/network.rb', line 153

def lookup_bot(nick) 
  return @bots[Bot.new(:network => self, :nick => nick).key]
end

#lookup_channel(name) ⇒ Object

Returns a channel object by its name or nil if there is no such channel associated with the network.



159
160
161
# File 'lib/irc/models/network.rb', line 159

def lookup_channel(name) 
  return @channels[Channel.new(:network => self, :name => name).key]
end

#lookup_or_create_bot(nick, login = nil, hostname = nil) ⇒ Object

Looks up or creates a new bot object and adds that bot to the internal hash table of bots.



122
123
124
# File 'lib/irc/models/network.rb', line 122

def lookup_or_create_bot(nick,  = nil, hostname = nil) 
  return lookup_bot(nick) || create_bot(nick, , hostname)
end

#lookup_or_create_channel(name, password = nil) ⇒ Object

Looks up or creates a new channel. The method returns the channel that was found or created.



136
137
138
# File 'lib/irc/models/network.rb', line 136

def lookup_or_create_channel(name, password = nil) 
  return lookup_channel(name) || create_channel(name, password)
end

#lookup_or_create_server(hostname, port = Server::DEFAULT_PORT) ⇒ Object

Looks up or creates a new server. The method returns the server



141
142
143
# File 'lib/irc/models/network.rb', line 141

def lookup_or_create_server(hostname, port = Server::DEFAULT_PORT) 
  return lookup_server(hostname, port) || create_server(hostname, port)
end

#lookup_or_create_user(nick, login = nil, hostname = nil) ⇒ Object

Looks up or creates a new user object and adds that user to the internal hash table of users.



147
148
149
# File 'lib/irc/models/network.rb', line 147

def lookup_or_create_user(nick,  = nil, hostname = nil) 
  return lookup_user(nick) || create_user(nick, , hostname)
end

#lookup_server(hostname, port = Server::DEFAULT_PORT) ⇒ Object

Returns a server object by its name and port number or nil if there is no such server associated with the network.



165
166
167
# File 'lib/irc/models/network.rb', line 165

def lookup_server(hostname, port = Server::DEFAULT_PORT) 
  return @servers[Server.new(:network => self, :hostname => hostname, :port => port).key]
end

#lookup_user(nick) ⇒ Object

Returns a user object by its nick name or nil if there is no such user associated with the network.



171
172
173
# File 'lib/irc/models/network.rb', line 171

def lookup_user(nick) 
  return @users[User.new(:network => self, :nick => nick).key] || lookup_bot(nick)
end

#merge(other) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/irc/models/network.rb', line 175

def merge(other)
  
  @name = other.name
  self.description = other.description
  
  self.last_connect = other.last_connect
  self.last_disconnect = other.last_disconnect
  
  self.last_observation_start = other.last_observation_start
  self.last_observation_stop = other.last_observation_stop
  
  #      other.bots.each do |bot|
  #        add_bot(bot)
  #        add_user(bot)        
  #      end
  
  other.channels.each do |other_channel|
    channel = lookup_or_create_channel(other_channel.name)
    channel.merge(other_channel)
  end
  
  other.servers.each do |other_server|
    server = lookup_or_create_server(other_server.hostname, other_server.port)
    server.merge(other_server)
  end
  #      
  #      other.users do.each |user|
  #        add_user(user)
  #      end
  
end

#serversObject

Returns all servers that are associated with this network.



208
209
210
# File 'lib/irc/models/network.rb', line 208

def servers
  return @servers.values
end

#to_sObject

Returns the name of the network converted to upper case.



218
219
220
# File 'lib/irc/models/network.rb', line 218

def to_s
  return name.upcase
end

#to_strObject

Returns the name of the network converted to upper case.



223
224
225
# File 'lib/irc/models/network.rb', line 223

def to_str
  return to_s
end

#to_xmlObject

Returns a XML representation of the network.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/irc/models/network.rb', line 228

def to_xml
  
  network = REXML::Element.new("Network")
  network.add_element("Name").add_text(REXML::CData.new(name))
  network.add_element("Description").add_text(REXML::CData.new(description)) if description != nil
  
  channels_element = network.add_element("Channels")
  channels.each { |channel| channels_element.add_element(channel.to_xml) }
  
  servers_element = network.add_element("Servers")
  servers.each { |server| servers_element.add_element(server.to_xml) }
  
  return network
  
end

#usersObject

Returns all users that are associated with this network.



213
214
215
# File 'lib/irc/models/network.rb', line 213

def users
  return (@users.values + bots).uniq
end