Class: MessageStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nchan_tools/pubsub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opt = {}) ⇒ MessageStore



148
149
150
151
# File 'lib/nchan_tools/pubsub.rb', line 148

def initialize(opt={})
  @array||=opt[:noid]
  clear
end

Instance Attribute Details

#msgsObject

Returns the value of attribute msgs.



115
116
117
# File 'lib/nchan_tools/pubsub.rb', line 115

def msgs
  @msgs
end

#nameObject

Returns the value of attribute name.



115
116
117
# File 'lib/nchan_tools/pubsub.rb', line 115

def name
  @name
end

Instance Method Details

#<<(msg) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/nchan_tools/pubsub.rb', line 205

def <<(msg)
  if @array
    @msgs << msg
  else
    if (cur_msg=@msgs[msg.unique_id])
      #puts "Different messages with same id: #{msg.id}, \"#{msg.to_s}\" then \"#{cur_msg.to_s}\"" unless cur_msg.message == msg.message
      cur_msg.times_seen+=1
      cur_msg.times_seen
    else
      @msgs[msg.unique_id]=msg
      1
    end
  end
end

#[](i) ⇒ Object



184
185
186
# File 'lib/nchan_tools/pubsub.rb', line 184

def [](i)
  @msgs[i]
end

#clearObject



167
168
169
# File 'lib/nchan_tools/pubsub.rb', line 167

def clear
  @msgs= @array ? [] : {}
end

#eachObject



188
189
190
191
192
193
194
# File 'lib/nchan_tools/pubsub.rb', line 188

def each
  if @array
    @msgs.each {|msg| yield msg }
  else
    @msgs.each {|key, msg| yield msg }
  end
end

#matches?(other_msg_store, opt = {}) ⇒ Boolean



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nchan_tools/pubsub.rb', line 117

def matches? (other_msg_store, opt={})
  my_messages = messages(raw: true)
  if MessageStore === other_msg_store
    other_messages = other_msg_store.messages(raw: true)
    other_name = other_msg_store.name
  else
    other_messages = other_msg_store
    other_name = "?"
  end
  unless my_messages.count == other_messages.count 
    err =  "Message count doesn't match:\r\n"
    err << "#{self.name}: #{my_messages.count}\r\n"
    err << "#{self.to_s}\r\n"
    
    err << "#{other_name}: #{other_messages.count}\r\n"
    err << "#{other_msg_store.to_s}"
    return false, err
  end
  other_messages.each_with_index do |msg, i|
    mymsg = my_messages[i]
#      puts "#{msg}, #{msg.class}"
    return false, "Message #{i} doesn't match. (#{self.name} |#{mymsg.length}|, #{other_name} |#{msg.length}|) " unless mymsg == msg
    [:content_type, :id, :eventsource_event].each do |field|
      if opt[field] or opt[:all]
        return false, "Message #{i} #{field} doesn't match. ('#{mymsg.send field}', '#{msg.send field}')" unless mymsg.send(field) == msg.send(field)
      end
    end
  end
  true
end

#messages(opt = {}) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/nchan_tools/pubsub.rb', line 153

def messages(opt={})
  if opt[:raw]
    self.to_a
  else
    self.to_a.map{|m|m.to_s}
  end
end

#remove_old(n = 1) ⇒ Object

remove n oldest messages



162
163
164
165
# File 'lib/nchan_tools/pubsub.rb', line 162

def remove_old(n=1)
  n.times {@msgs.shift}
  @msgs.count
end

#selectObject



196
197
198
199
200
201
202
203
# File 'lib/nchan_tools/pubsub.rb', line 196

def select
  cpy = self.class.new(noid: @array ? true : nil)
  cpy.name = self.name
  self.each do |msg|
    cpy << msg if yield msg
  end
  cpy
end

#to_aObject



171
172
173
# File 'lib/nchan_tools/pubsub.rb', line 171

def to_a
  @array ? @msgs : @msgs.values
end

#to_sObject



174
175
176
177
178
179
180
181
182
# File 'lib/nchan_tools/pubsub.rb', line 174

def to_s
  buf=""
  each do |msg|
    m = msg.to_s
    m = m.length > 20 ? "#{m[0...20]}..." : m
    buf<< "<#{msg.id}> \"#{m}\" (count: #{msg.times_seen})\r\n"
  end
  buf
end