From 87f2943fbac0b74beaaafce73b35ea8467820c51 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Sun, 26 Apr 2015 07:01:18 +0200 Subject: [PATCH] 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. --- lib/rubella/storage.rb | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/rubella/storage.rb b/lib/rubella/storage.rb index fc63aa4..373d7c3 100644 --- a/lib/rubella/storage.rb +++ b/lib/rubella/storage.rb @@ -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. #