Class: BooticCli::Store

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

Constant Summary collapse

VERSION =
1
DEFAULT_NAMESPACE =
'production'.freeze
DIRNAME =
'.bootic'.freeze
FILE_NAME =
'store.pstore'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_dir: ENV['HOME'], dir: DIRNAME, namespace: DEFAULT_NAMESPACE) ⇒ Store

Returns a new instance of Store.



12
13
14
15
16
# File 'lib/bootic_cli/store.rb', line 12

def initialize(base_dir: ENV['HOME'], dir: DIRNAME, namespace: DEFAULT_NAMESPACE)
  @base_dir = File.join(base_dir, dir)
  @namespace = namespace
  FileUtils.mkdir_p @base_dir
end

Instance Method Details

#[](k) ⇒ Object



24
25
26
27
# File 'lib/bootic_cli/store.rb', line 24

def [](k)
  hash = store[namespace] || {}
  hash[k]
end

#[]=(k, v) ⇒ Object



18
19
20
21
22
# File 'lib/bootic_cli/store.rb', line 18

def []=(k, v)
  hash = store[namespace] || {}
  hash[k] = v
  store[namespace] = hash
end

#eraseObject



33
34
35
# File 'lib/bootic_cli/store.rb', line 33

def erase
  FileUtils.rm_rf base_dir
end

#needs_upgrade?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/bootic_cli/store.rb', line 37

def needs_upgrade?
  transaction do
    store['version'].to_i < VERSION
  end
end

#transaction(&block) ⇒ Object



29
30
31
# File 'lib/bootic_cli/store.rb', line 29

def transaction(&block)
  store.transaction(&block)
end

#upgrade!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bootic_cli/store.rb', line 43

def upgrade!
  return unless needs_upgrade?

  transaction do
    current_values = {}
    store.roots.each do |r|
      v = store[r]
      store.delete(r)
      current_values[r] = v
    end

    current_values.each do |k, v|
      self[k] = v
    end

    store['version'] = VERSION
  end
end