Class: VagrantGitHooks::Util::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-git-hooks/util/manifest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Manifest



11
12
13
# File 'lib/vagrant-git-hooks/util/manifest.rb', line 11

def initialize(path)
  @path = path.to_s
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/vagrant-git-hooks/util/manifest.rb', line 9

def path
  @path
end

Instance Method Details

#clearObject



40
41
42
43
44
45
# File 'lib/vagrant-git-hooks/util/manifest.rb', line 40

def clear
  File.delete(@path) if File.exist?(@path)
  true
rescue StandardError
  false
end

#loadHash

Loads the hook manifest from disk.

Invalid or missing manifest data is treated as empty so hook commands can recover from interrupted runs.



21
22
23
24
25
26
27
28
# File 'lib/vagrant-git-hooks/util/manifest.rb', line 21

def load
  return empty unless File.exist?(@path)

  data = JSON.parse(File.read(@path))
  data.is_a?(Hash) ? normalize(data) : empty
rescue StandardError
  empty
end

#save(data) ⇒ Hash

Saves normalized hook manifest data.



34
35
36
37
38
# File 'lib/vagrant-git-hooks/util/manifest.rb', line 34

def save(data)
  FileUtils.mkdir_p(File.dirname(@path))
  File.write(@path, JSON.pretty_generate(normalize(data)))
  data
end