Class: DiscordStore::Codec::Batch
- Inherits:
-
Object
- Object
- DiscordStore::Codec::Batch
- Defined in:
- lib/discord_store/codec.rb
Overview
Accumulates records until the next one would overflow a message.
Instance Method Summary collapse
- #add(record, envelope) ⇒ Object
- #any? ⇒ Boolean
-
#drain ⇒ Array
The accumulated pairs, resetting the batch.
-
#fits?(envelope) ⇒ Boolean
Whether
envelopestill fits alongside what is held. -
#initialize(budget) ⇒ Batch
constructor
A new instance of Batch.
Constructor Details
#initialize(budget) ⇒ Batch
Returns a new instance of Batch.
136 137 138 139 140 |
# File 'lib/discord_store/codec.rb', line 136 def initialize(budget) @budget = budget @pairs = [] @size = 0 end |
Instance Method Details
#add(record, envelope) ⇒ Object
151 152 153 154 |
# File 'lib/discord_store/codec.rb', line 151 def add(record, envelope) @size += envelope.length + (@pairs.empty? ? 0 : SEPARATOR.length) @pairs << [record, envelope] end |
#any? ⇒ Boolean
142 |
# File 'lib/discord_store/codec.rb', line 142 def any? = @pairs.any? |
#drain ⇒ Array
Returns the accumulated pairs, resetting the batch.
157 158 159 160 161 162 |
# File 'lib/discord_store/codec.rb', line 157 def drain pairs = @pairs @pairs = [] @size = 0 pairs end |
#fits?(envelope) ⇒ Boolean
Returns whether envelope still fits alongside what is held.
145 146 147 148 149 |
# File 'lib/discord_store/codec.rb', line 145 def fits?(envelope) return true if @pairs.empty? @size + envelope.length + SEPARATOR.length <= @budget end |