Class: Lescopr::Filesystem::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lescopr/filesystem/config_manager.rb

Overview

Thread-safe reader/writer for .lescopr.json

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ ConfigManager

Returns a new instance of ConfigManager.



11
12
13
14
# File 'lib/lescopr/filesystem/config_manager.rb', line 11

def initialize(path: nil)
  @path  = path || File.join(Dir.pwd, CONFIG_FILE)
  @mutex = Mutex.new
end

Instance Method Details

#delete!Object



38
39
40
# File 'lib/lescopr/filesystem/config_manager.rb', line 38

def delete!
  @mutex.synchronize { File.delete(@path) if File.exist?(@path) }
end

#exists?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/lescopr/filesystem/config_manager.rb', line 34

def exists?
  File.exist?(@path)
end

#loadHash?

Returns:

  • (Hash, nil)


17
18
19
20
21
22
23
24
25
# File 'lib/lescopr/filesystem/config_manager.rb', line 17

def load
  @mutex.synchronize do
    return nil unless File.exist?(@path)

    JSON.parse(File.read(@path), symbolize_names: true)
  rescue JSON::ParserError, Errno::ENOENT
    nil
  end
end

#save(data) ⇒ Object

Parameters:

  • data (Hash)


28
29
30
31
32
# File 'lib/lescopr/filesystem/config_manager.rb', line 28

def save(data)
  @mutex.synchronize do
    File.write(@path, JSON.pretty_generate(data))
  end
end