Class: Lescopr::Filesystem::ConfigManager
- Inherits:
-
Object
- Object
- Lescopr::Filesystem::ConfigManager
- Defined in:
- lib/lescopr/filesystem/config_manager.rb
Overview
Thread-safe reader/writer for .lescopr.json
Instance Method Summary collapse
- #delete! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(path: nil) ⇒ ConfigManager
constructor
A new instance of ConfigManager.
- #load ⇒ Hash?
- #save(data) ⇒ Object
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
34 35 36 |
# File 'lib/lescopr/filesystem/config_manager.rb', line 34 def exists? File.exist?(@path) end |
#load ⇒ Hash?
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
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 |