class SKP::ClientData

Constants

DOTFILE_NAME

Public Class Methods

create_in_home!() click to toggle source
# File lib/skp/client_data.rb, line 32
def self.create_in_home!
  unless File.directory?(File.expand_path("~/.skp/"))
    FileUtils.mkdir(File.expand_path("~/.skp/"))
  end

  FileUtils.touch(File.expand_path("~/.skp/" + DOTFILE_NAME))
end
create_in_pwd!() click to toggle source
# File lib/skp/client_data.rb, line 28
def self.create_in_pwd!
  FileUtils.touch(File.expand_path("./" + DOTFILE_NAME))
end
delete_filestore() click to toggle source
# File lib/skp/client_data.rb, line 40
def self.delete_filestore
  return unless File.exist?(filestore_location)
  FileUtils.remove(filestore_location)
end
exists?() click to toggle source
# File lib/skp/client_data.rb, line 45
def self.exists?
  File.exist? filestore_location
end
filestore_location() click to toggle source
# File lib/skp/client_data.rb, line 49
def self.filestore_location
  if File.exist?(File.expand_path("./" + DOTFILE_NAME))
    File.expand_path("./" + DOTFILE_NAME)
  else
    File.expand_path("~/.skp/" + DOTFILE_NAME)
  end
end
new() click to toggle source
# File lib/skp/client_data.rb, line 8
def initialize
  data # access file to load
end

Public Instance Methods

[](key) click to toggle source
# File lib/skp/client_data.rb, line 12
def [](key)
  data[key]
end
[]=(key, value) click to toggle source
# File lib/skp/client_data.rb, line 16
def []=(key, value)
  data
  data[key] = value

  begin
    File.open(filestore_location, "w") { |f| f.write(YAML.dump(data)) }
  rescue
    # raise Error, "The SKP data at #{filestore_location} is not writable. \
    # Check your file permissions."
  end
end

Private Instance Methods

data() click to toggle source
# File lib/skp/client_data.rb, line 63
def data
  @data ||= begin
    begin
      YAML.safe_load(File.read(filestore_location), permitted_classes: [Time]) || {}
    rescue Errno::ENOENT
      {}
    end
  end
end
filestore_location() click to toggle source
# File lib/skp/client_data.rb, line 59
def filestore_location
  self.class.filestore_location
end