Class: IRC::Models::Channel

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Channel

Creates a new channel object with a name and an optional topic, that belongs to a network.

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/irc/models/channel.rb', line 49

def initialize(attributes)
  
  raise ArgumentError.new("Can't create a new channel object. The :network attribute is missing.") if attributes[:network] == nil
  @network = attributes[:network]
  
  raise ArgumentError.new("Can't create a new user object. The :name attribute is missing.") if attributes[:name] == nil
  @name = attributes[:name].strip
  
  self.last_ban = attributes[:last_ban]
  self.last_join = attributes[:last_join]
  self.last_join_failure = attributes[:last_join_failure]
  self.last_join_failure_reason = attributes[:last_join_failure_reason]
  self.last_kick = attributes[:last_kick]
  self.last_kick_reason = attributes[:last_kick_reason]
  self.last_part = attributes[:last_part]
  self.password = attributes[:password]
  self.topic = attributes[:topic]
  self.topic_author = attributes[:topic_author]
  self.last_topic_change = attributes[:last_topic_change]
  
  @users = Hash.new
  
end

Instance Attribute Details

#last_banObject

Returns the value of attribute last_ban.



35
36
37
# File 'lib/irc/models/channel.rb', line 35

def last_ban
  @last_ban
end

#last_joinObject

Returns the value of attribute last_join.



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

def last_join
  @last_join
end

#last_join_failureObject

Returns the value of attribute last_join_failure.



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

def last_join_failure
  @last_join_failure
end

#last_join_failure_reasonObject

Returns the value of attribute last_join_failure_reason.



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

def last_join_failure_reason
  @last_join_failure_reason
end

#last_kickObject

Returns the value of attribute last_kick.



39
40
41
# File 'lib/irc/models/channel.rb', line 39

def last_kick
  @last_kick
end

#last_kick_reasonObject

Returns the value of attribute last_kick_reason.



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

def last_kick_reason
  @last_kick_reason
end

#last_partObject

Returns the value of attribute last_part.



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

def last_part
  @last_part
end

#last_topic_changeObject

Returns the value of attribute last_topic_change.



45
46
47
# File 'lib/irc/models/channel.rb', line 45

def last_topic_change
  @last_topic_change
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/irc/models/channel.rb', line 33

def name
  @name
end

#networkObject (readonly)

Returns the value of attribute network.



32
33
34
# File 'lib/irc/models/channel.rb', line 32

def network
  @network
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#topicObject

Returns the value of attribute topic.



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

def topic
  @topic
end

#topic_authorObject

Returns the value of attribute topic_author.



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

def topic_author
  @topic_author
end

Class Method Details

.is_valid?(channel_name) ⇒ Boolean

Returns true

Returns:

  • (Boolean)


133
134
135
# File 'lib/irc/models/channel.rb', line 133

def self.is_valid?(channel_name)
  return Regexp.new('\s*#|&.*').match(channel_name) != nil
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
# File 'lib/irc/models/channel.rb', line 77

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

#add_user(user) ⇒ Object



73
74
75
# File 'lib/irc/models/channel.rb', line 73

def add_user(user)
  @users[user] = user
end

#eql?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


83
84
85
# File 'lib/irc/models/channel.rb', line 83

def eql?(other)
  return other.instance_of?(Channel) && @network == other.network && @name == other.name    
end

#hashObject



87
88
89
# File 'lib/irc/models/channel.rb', line 87

def hash
  return key.hash
end

#keyObject



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

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

#merge(other) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/irc/models/channel.rb', line 95

def merge(other)
  
  #      @network = other.network
  @name = other.name
  
  self.last_ban = other.last_ban
  
  self.last_join = other.last_join    
  self.last_join_failure = other.last_join_failure
  self.last_join_failure_reason = other.last_join_failure_reason
  
  self.last_kick = other.last_kick
  self.last_kick_reason = other.last_kick_reason    
  
  self.last_part = other.last_part    
  
  self.password = other.password
  
  self.topic = other.topic
  self.topic_author = other.topic_author
  self.last_topic_change = other.last_topic_change
  
  other.users.each do |other_user|
    user = network.lookup_or_create_user(other_user.nick, other_user., other_user.hostname)
    add_user(other_user)
  end
  
end

#remove_user(user) ⇒ Object



124
125
126
# File 'lib/irc/models/channel.rb', line 124

def remove_user(user)
  return @users.delete(user)
end

#to_sObject

Returns the name of the channel.



138
139
140
# File 'lib/irc/models/channel.rb', line 138

def to_s
  return @name
end

#to_strObject

Returns the name of the channel.



143
144
145
# File 'lib/irc/models/channel.rb', line 143

def to_str
  return to_s
end

#to_xmlObject



147
148
149
150
151
152
# File 'lib/irc/models/channel.rb', line 147

def to_xml
  channel = REXML::Element.new("Channel")
  channel.add_element("Name").add_text(REXML::CData.new(name))
  channel.add_element("Password").add_text(REXML::CData.new(password)) if password != nil
  return channel
end

#usersObject



128
129
130
# File 'lib/irc/models/channel.rb', line 128

def users
  return @users.values
end