Top Level Namespace
Defined Under Namespace
Classes: Item
Instance Method Summary collapse
- #delete_all_storage ⇒ Object
- #delete_storage(key) ⇒ Object
- #load_storage ⇒ Object
- #path ⇒ Object
- #read_storage(key) ⇒ Object
- #write_file ⇒ Object
- #write_storage(key, value) ⇒ Object
Instance Method Details
#delete_all_storage ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/script_storage.rb', line 77 def delete_all_storage File.open(path) do |file| file_data = file.read.split("\n__END__\n")[0] File.write(path, file_data) end end |
#delete_storage(key) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/script_storage.rb', line 70 def delete_storage(key) return unless @data.include?(key) @data.delete(key) write_file end |
#load_storage ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/script_storage.rb', line 23 def load_storage File.open(path) do |file| file_content = file.read break if file_content.split("\n__END__\n").length == 1 file_data = file_content.split("\n__END__\n").last file_data.scan(/([A-z]*)(.*[()]*):(.*)/).each do |raw| type = raw[1].strip[1..-2] type = '' if type.nil? case type when 'array' value = [] raw[2].strip.split(',').each do |v| value.append(v.strip) end @data.append(Item.new(raw[0].strip, value)) when 'integer' @data.append(Item.new(raw[0].strip, raw[2].strip.to_i)) else @data.append(Item.new(raw[0].strip, raw[2].strip)) end end end end |
#path ⇒ Object
7 8 9 |
# File 'lib/script_storage.rb', line 7 def path "#{`pwd`.strip}/#{$PROGRAM_NAME}" end |
#read_storage(key) ⇒ Object
53 54 55 56 |
# File 'lib/script_storage.rb', line 53 def read_storage(key) array = @data.select { |item| item.key == key } array.first&.value end |
#write_file ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/script_storage.rb', line 11 def write_file File.open(path) do |file| file_data = "#{file.read.split("\n__END__\n")[0]}\n__END__\n" @data.each do |v| file_data += "#{v.show}\n" end File.write(path, file_data) end end |
#write_storage(key, value) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/script_storage.rb', line 58 def write_storage(key, value) if @data.include?(key) @data.each do |v| v.value = value if v.key == key end else @data.append(Item.new(key, value)) end write_file end |