Class: Irc::QueueRing

Inherits:
Object show all
Defined in:
lib/rbot/ircsocket.rb

Instance Method Summary collapse

Constructor Details

#initializeQueueRing

A QueueRing is implemented as an array with elements in the form

chan, [message1, message2, …

Note that the channel chan has no actual bearing with the channels to which messages will be sent



78
79
80
81
# File 'lib/rbot/ircsocket.rb', line 78

def initialize
  @storage = Array.new
  @last_idx = -1
end

Instance Method Details

#clearObject



83
84
85
86
# File 'lib/rbot/ircsocket.rb', line 83

def clear
  @storage.clear
  @last_idx = -1
end

#empty?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/rbot/ircsocket.rb', line 97

def empty?
  @storage.empty?
end

#lengthObject Also known as: size



88
89
90
91
92
93
94
# File 'lib/rbot/ircsocket.rb', line 88

def length
  len = 0
  @storage.each {|c|
    len += c[1].size
  }
  return len
end

#nextObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbot/ircsocket.rb', line 112

def next
  if empty?
    warning "trying to access empty ring"
    return nil
  end
  save_idx = @last_idx
  @last_idx = (@last_idx + 1) % @storage.size
  mess = @storage[@last_idx][1].first
  @last_idx = save_idx
  return mess
end

#push(mess, chan) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/rbot/ircsocket.rb', line 101

def push(mess, chan)
  cmess = @storage.assoc(chan)
  if cmess
    idx = @storage.index(cmess)
    cmess[1] << mess
    @storage[idx] = cmess
  else
    @storage << [chan, [mess]]
  end
end

#shiftObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/rbot/ircsocket.rb', line 124

def shift
  if empty?
    warning "trying to access empty ring"
    return nil
  end
  @last_idx = (@last_idx + 1) % @storage.size
  mess = @storage[@last_idx][1].shift
  @storage.delete(@storage[@last_idx]) if @storage[@last_idx][1] == []
  return mess
end