Class: KTools::KDB

Inherits:
Object
  • Object
show all
Defined in:
lib/ktools/kdb.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKDB

Returns a new instance of KDB.



26
27
28
29
# File 'lib/ktools/kdb.rb', line 26

def initialize
  @config_file = KTools.configuration.config_file
  @kpath = KTools.configuration.kpath
end

Instance Attribute Details

#config_fileObject

Returns the value of attribute config_file.



3
4
5
# File 'lib/ktools/kdb.rb', line 3

def config_file
  @config_file
end

#kpathObject

Returns the value of attribute kpath.



3
4
5
# File 'lib/ktools/kdb.rb', line 3

def kpath
  @kpath
end

Class Method Details

.readObject



5
6
7
8
9
10
11
# File 'lib/ktools/kdb.rb', line 5

def self.read
  config_file = self.new.config_file
  return false unless File.exist?(config_file)

  data = File.read(config_file)
  Oj.load(data)
end

.update(current, entry) ⇒ Object



22
23
24
# File 'lib/ktools/kdb.rb', line 22

def self.update(current, entry)
  self.write(current.merge(entry))
end

.write(data) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ktools/kdb.rb', line 13

def self.write(data)
  kdb = self.new
  kdb.create_path

  File.open(kdb.config_file, 'w') do |f|
    f.write(Oj.dump(data))
  end
end

Instance Method Details

#create_pathObject



31
32
33
34
# File 'lib/ktools/kdb.rb', line 31

def create_path
  return true if File.exist?(@config_file)
  FileUtils.mkdir_p(@kpath)
end