Class: NchanTools::MessageStore
- Inherits:
-
Object
- Object
- NchanTools::MessageStore
- Includes:
- Enumerable
- Defined in:
- lib/nchan_tools/pubsub.rb
Instance Attribute Summary collapse
-
#msgs ⇒ Object
Returns the value of attribute msgs.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #<<(msg) ⇒ Object
- #[](i) ⇒ Object
- #clear ⇒ Object
- #each ⇒ Object
-
#initialize(opt = {}) ⇒ MessageStore
constructor
A new instance of MessageStore.
- #matches?(other_msg_store, opt = {}) ⇒ Boolean
- #messages(opt = {}) ⇒ Object
-
#remove_old(n = 1) ⇒ Object
remove n oldest messages.
- #select ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ MessageStore
Returns a new instance of MessageStore.
145 146 147 148 |
# File 'lib/nchan_tools/pubsub.rb', line 145 def initialize(opt={}) @array||=opt[:noid] clear end |
Instance Attribute Details
#msgs ⇒ Object
Returns the value of attribute msgs.
112 113 114 |
# File 'lib/nchan_tools/pubsub.rb', line 112 def msgs @msgs end |
#name ⇒ Object
Returns the value of attribute name.
112 113 114 |
# File 'lib/nchan_tools/pubsub.rb', line 112 def name @name end |
Instance Method Details
#<<(msg) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# 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) ⇒ Object
181 182 183 |
# File 'lib/nchan_tools/pubsub.rb', line 181 def [](i) @msgs[i] end |
#clear ⇒ Object
164 165 166 |
# File 'lib/nchan_tools/pubsub.rb', line 164 def clear @msgs= @array ? [] : {} end |
#each ⇒ Object
185 186 187 188 189 190 191 |
# 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 = {}) ⇒ Boolean
114 115 116 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 |
# File 'lib/nchan_tools/pubsub.rb', line 114 def matches? (other_msg_store, opt={}) = (raw: true) if MessageStore === other_msg_store = other_msg_store.(raw: true) other_name = other_msg_store.name else = other_msg_store other_name = "?" end unless .count == .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 .each_with_index do |msg, i| mymsg = [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
150 151 152 153 154 155 156 |
# File 'lib/nchan_tools/pubsub.rb', line 150 def (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
159 160 161 162 |
# File 'lib/nchan_tools/pubsub.rb', line 159 def remove_old(n=1) n.times {@msgs.shift} @msgs.count end |
#select ⇒ Object
193 194 195 196 197 198 199 200 |
# 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 ⇒ Object
168 169 170 |
# File 'lib/nchan_tools/pubsub.rb', line 168 def to_a @array ? @msgs : @msgs.values end |
#to_s ⇒ Object
171 172 173 174 175 176 177 178 179 |
# 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 |