Class: GDKBox::Store
- Inherits:
-
Object
- Object
- GDKBox::Store
- Defined in:
- lib/gdkbox/store.rb
Overview
Persists box metadata as one JSON file per box under the boxes directory.
Instance Method Summary collapse
- #all ⇒ Object
- #delete(name) ⇒ Object
- #exists?(name) ⇒ Boolean
-
#initialize(config:) ⇒ Store
constructor
A new instance of Store.
- #load(name) ⇒ Object
- #path(name) ⇒ Object
- #save(box) ⇒ Object
-
#used_ports ⇒ Object
Every host port already claimed by an existing box.
Constructor Details
#initialize(config:) ⇒ Store
Returns a new instance of Store.
8 9 10 |
# File 'lib/gdkbox/store.rb', line 8 def initialize(config:) @config = config end |
Instance Method Details
#all ⇒ Object
32 33 34 35 36 |
# File 'lib/gdkbox/store.rb', line 32 def all Dir.glob(File.join(@config.boxes_dir, "*.json")).map do |file| JSON.parse(File.read(file)) end end |
#delete(name) ⇒ Object
38 39 40 |
# File 'lib/gdkbox/store.rb', line 38 def delete(name) File.delete(path(name)) if exists?(name) end |
#exists?(name) ⇒ Boolean
16 17 18 |
# File 'lib/gdkbox/store.rb', line 16 def exists?(name) File.exist?(path(name)) end |
#load(name) ⇒ Object
26 27 28 29 30 |
# File 'lib/gdkbox/store.rb', line 26 def load(name) return nil unless exists?(name) JSON.parse(File.read(path(name))) end |
#path(name) ⇒ Object
12 13 14 |
# File 'lib/gdkbox/store.rb', line 12 def path(name) File.join(@config.boxes_dir, "#{name}.json") end |
#save(box) ⇒ Object
20 21 22 23 24 |
# File 'lib/gdkbox/store.rb', line 20 def save(box) @config.ensure_dirs! File.write(path(box["name"]), "#{JSON.pretty_generate(box)}\n") box end |
#used_ports ⇒ Object
Every host port already claimed by an existing box.
43 44 45 |
# File 'lib/gdkbox/store.rb', line 43 def used_ports all.flat_map { |box| [box["ssh_port"], box["web_port"]] }.compact end |