Class: MessageStore
- Inherits:
-
Object
- Object
- 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.
148 149 150 151 |
# File 'lib/nchan_tools/pubsub.rb', line 148 def initialize(opt={}) @array||=opt[:noid] clear end |
Instance Attribute Details
#msgs ⇒ Object
Returns the value of attribute msgs.
115 116 117 |
# File 'lib/nchan_tools/pubsub.rb', line 115 def msgs @msgs end |
#name ⇒ Object
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 |
#clear ⇒ Object
167 168 169 |
# File 'lib/nchan_tools/pubsub.rb', line 167 def clear @msgs= @array ? [] : {} end |
#each ⇒ Object
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={}) = (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}: #{.count}\r\n" err << "#{self.to_s}\r\n" err << "#{other_name}: #{.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
153 154 155 156 157 158 159 |
# File 'lib/nchan_tools/pubsub.rb', line 153 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
162 163 164 165 |
# File 'lib/nchan_tools/pubsub.rb', line 162 def remove_old(n=1) n.times {@msgs.shift} @msgs.count end |
#select ⇒ Object
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_a ⇒ Object
171 172 173 |
# File 'lib/nchan_tools/pubsub.rb', line 171 def to_a @array ? @msgs : @msgs.values end |
#to_s ⇒ Object
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 |