@@ -2,8 +2,9 @@ module Amnesia
22 class Storage
33 attr_reader :filename
44
5- def initialize ( filename )
5+ def initialize ( filename , items : nil )
66 @filename = filename
7+ populate_data ( items ) unless items . nil? || items . empty?
78 end
89
910 def size
@@ -16,6 +17,14 @@ def set(key, value)
1617 File . write ( filename , entry , mode : 'a+' )
1718 end
1819
20+ def all
21+ File . readlines ( filename ) . map do |record |
22+ key , value = record . chomp . split ( ',' )
23+
24+ [ key , value ]
25+ end
26+ end
27+
1928 def delete ( key )
2029 set ( key , '' )
2130 end
@@ -27,19 +36,15 @@ def get(key, index_entry: nil)
2736 end
2837
2938 def parse_record ( raw_record )
30- value = raw_record_value ( raw_record )
31-
32- return nil if value . nil? || value . empty?
33-
34- value
39+ raw_record_value ( raw_record )
3540 end
3641
3742 def raw_record_value ( raw_record )
3843 ( raw_record || '' ) . chomp . split ( ',' , 2 ) [ 1 ]
3944 end
4045
41- def create_db_file
42- File . write ( filename , '' ) unless file_exists?
46+ def create_db_file ( content = '' )
47+ File . write ( filename , content ) unless file_exists?
4348 end
4449
4550 def file_exists?
@@ -48,6 +53,12 @@ def file_exists?
4853
4954 private
5055
56+ def populate_data ( items )
57+ data_block = items . map { |( key , value ) | "#{ key } ,#{ value } \n " } . join ( '' )
58+
59+ create_db_file ( data_block )
60+ end
61+
5162 def record_from_scan ( key )
5263 lines = File . readlines ( filename )
5364
0 commit comments