module TerUtil::Remember

Constants

FNAME
FPATH

Public Class Methods

delete(key) click to toggle source
# File lib/remember.rb, line 56
def self.delete(key)
    write_data load_data.reject { |k| k == key }
end
load_data() click to toggle source
# File lib/remember.rb, line 21
def self.load_data
    ret = JSON.parse load_file
    raise JSON::ParserError if ret.class != Hash
    ret

rescue JSON::ParserError
    puts "The remember data file (#{FNAME}) is corrupted."
    puts "Please fix it manually or reset it."
    print "Reset (ALL DATA WILL BE LOST!)? (y/n): "
    if ['y', 'yes'].include? gets.chomp
        reset_file
        puts "File reset."
    end
end
load_file() click to toggle source
# File lib/remember.rb, line 12
def self.load_file
    if !File.exists? FNAME
        FileUtils.mkdir_p FPATH
        reset_file
    end

    File.read(FNAME)
end
read(key) click to toggle source
# File lib/remember.rb, line 42
def self.read(key)
    load_data[key]
end
reset_file() click to toggle source
# File lib/remember.rb, line 8
def self.reset_file
    File.open(FNAME, 'w') { |f| f.write "{}" }
end
write(key, val, silent=true) click to toggle source
# File lib/remember.rb, line 46
def self.write(key, val, silent=true)
    if val == '' 
        delete key
        puts "Deleted #{key}." if !silent
    else 
        write_data load_data.tap { |d| d[key] = val }
        puts "Set #{key} to #{val}." if !silent
    end
end
write_data(data) click to toggle source
# File lib/remember.rb, line 38
def self.write_data(data)
    File.open(FNAME, 'w') { |f| f.write JSON.generate(data) }
end