0
0
Fork 0

Prefill Storage with zero values

Only if Storage already has some data.
Disable empty and prefill feature if given lenght is 0.
This commit is contained in:
Stormwind 2015-04-25 16:31:32 +02:00
parent 01c1f97750
commit 2bb29d9e8d
1 changed files with 19 additions and 5 deletions

View File

@ -10,23 +10,37 @@ module Rubella
attr_reader :data
attr_reader :length
def initialize data, length = nil
@data = data
def initialize data, length = 0
@data = data
self.length = length
end
# Defines the length of the Storage.
#
# Be careful, if your Storage has more entries, tha the new length, the
# oldest entries will immediately be dropped.
# oldest entries will immediately be dropped. If your Storage has less
# entries, it will be filled up with empty entries.
# Happens only if data has entries.
#
# Setting the length to 0 will disable this feature
#
# @param length Integer The size of the storage
# @return Integer The new size
#
def length= length
@length = length
# Drop entries, if more than new length
if @length.kind_of? Integer
# Use length only, if length is valid
if @length != 0 and @data.length != 0
# Drop entries, if more than new length
while @data.length > @length
@data.pop
end
# Prefill with empty content, if less than new length
dummy_data = Array.new(@data[0].length, 0)
while @data.length < @length
@data.unshift dummy_data
end
end
@length