Class: Hookit::DB
- Inherits:
-
Object
- Object
- Hookit::DB
- Defined in:
- lib/hookit/db.rb
Constant Summary collapse
- DEFAULT_PATH =
'/var/db/hookit/db.json'
Instance Method Summary collapse
- #data ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize(path = nil) ⇒ DB
constructor
A new instance of DB.
- #load ⇒ Object
- #put(key, value) ⇒ Object
- #save ⇒ Object
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
#data ⇒ Object
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 |
#load ⇒ Object
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 |
#save ⇒ Object
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 |