class Wutang::Persistence

Attributes

crypto[R]
path[R]

Public Class Methods

new(path, passphrase) click to toggle source
# File lib/wutang/persistence.rb, line 5
def initialize(path, passphrase)
  @path   = File.expand_path(path)
  @crypto = Encryption.new(passphrase)
  Dir.mkdir(@path) unless File.exists?(@path)
end

Private Class Methods

generate_filename() click to toggle source
# File lib/wutang/persistence.rb, line 38
def self.generate_filename
  "#{SecureRandom.uuid}"
end

Public Instance Methods

all() click to toggle source
# File lib/wutang/persistence.rb, line 25
def all
  files.map do |file|
    content = read(file)
    Entry.new(content, file)
  end
end
read(filename) click to toggle source
# File lib/wutang/persistence.rb, line 11
def read(filename)
  data      = IO.read("#{path}/#{filename}.json")
  plaintext = crypto.decrypt(data)

  JSON.parse(plaintext, symbolize_names: true)
end
write(filename, content) click to toggle source
# File lib/wutang/persistence.rb, line 18
def write(filename, content)
  File.open("#{path}/#{filename}.json", "w") do |f|
    ciphertext = crypto.encrypt(content.to_json)
    f.write ciphertext
  end
end

Private Instance Methods

files() click to toggle source
# File lib/wutang/persistence.rb, line 34
def files
  Dir["#{path}/*.json"].map { |f| File.basename(f, '.json') }
end