Class: GDKBox::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/gdkbox/store.rb

Overview

Persists box metadata as one JSON file per box under the boxes directory.

Instance Method Summary collapse

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

#allObject



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

Returns:

  • (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_portsObject

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