Class: Hookit::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/hookit/db.rb

Constant Summary collapse

DEFAULT_PATH =
'/var/db/hookit/db.json'

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ DB

Returns a new instance of DB.



10
11
12
# File 'lib/hookit/db.rb', line 10

def initialize(path=nil)
  @path = path || DEFAULT_PATH
end

Instance Method Details

#dataObject



32
33
34
# File 'lib/hookit/db.rb', line 32

def data
  @data ||= load
end

#fetch(key) ⇒ Object



14
15
16
# File 'lib/hookit/db.rb', line 14

def fetch(key)
  data[key]
end

#loadObject



23
24
25
# File 'lib/hookit/db.rb', line 23

def load
  ::MultiJson.load(::File.read(@path)) rescue {}
end

#put(key, value) ⇒ Object



18
19
20
21
# File 'lib/hookit/db.rb', line 18

def put(key, value)
  data[key] = value
  save
end

#saveObject



27
28
29
30
# File 'lib/hookit/db.rb', line 27

def save
  ::FileUtils.mkdir_p(File.dirname(@path))
  ::File.write(@path, ::MultiJson.dump(data))
end