0
0
Fork 0

Storage responds to length

Now the storage will return the current length.
And not the set length anymore. But if there are datasets in the storge it
will be as long as the setted length.
It also can return the length of its datasets.
This commit is contained in:
Stormwind 2015-04-26 07:01:18 +02:00
parent f561b2c769
commit 87f2943fba
1 changed files with 25 additions and 5 deletions

View File

@ -8,7 +8,7 @@ module Rubella
#
class Storage
attr_reader :data
attr_reader :length
# :length
def initialize data, length = 0
@data = data
@ -31,14 +31,14 @@ module Rubella
@length = length
# Use length only, if length is valid
if @length != 0 and @data.length != 0
if @length != 0 and self.length != 0
# Drop entries, if more than new length
while @data.length > @length
while self.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
dummy_data = Array.new(self.dataset_length, 0)
while self.length < @length
@data.unshift dummy_data
end
end
@ -46,6 +46,26 @@ module Rubella
@length
end
# Returns the current length of the storage.
# (How many datasets it holds.)
#
# @return Integer length
#
def length
@data.length
end
# Returns the length of a dataset.
# Will return 0, if no datasets in storage.
#
# @return Integer length of one dataset
#
def dataset_length
return 0 if self.length == 0
@data[0].length
end
# Adds the data from the given storage to the own data and return this as a
# new Storage. Does not modify one of the storages.
#