class NchanTools::MessageStore

Attributes

msgs[RW]
name[RW]

Public Class Methods

new(opt={}) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 145
def initialize(opt={})
  @array||=opt[:noid]
  clear
end

Public Instance Methods

<<(msg) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 202
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) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 181
def [](i)
  @msgs[i]
end
clear() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 164
def clear
  @msgs= @array ? [] : {}
end
each() { |msg| ... } click to toggle source
# File lib/nchan_tools/pubsub.rb, line 185
def each
  if @array
    @msgs.each {|msg| yield msg }
  else
    @msgs.each {|key, msg| yield msg }
  end
end
matches?(other_msg_store, opt={}) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 114
  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={}) click to toggle source
# File lib/nchan_tools/pubsub.rb, line 150
def messages(opt={})
  if opt[:raw]
    self.to_a
  else
    self.to_a.map{|m|m.to_s}
  end
end
remove_old(n=1) click to toggle source

remove n oldest messages

# File lib/nchan_tools/pubsub.rb, line 159
def remove_old(n=1)
  n.times {@msgs.shift}
  @msgs.count
end
select() { |msg| ... } click to toggle source
# File lib/nchan_tools/pubsub.rb, line 193
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_a() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 168
def to_a
  @array ? @msgs : @msgs.values
end
to_s() click to toggle source
# File lib/nchan_tools/pubsub.rb, line 171
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