Class: Dbwatcher::Storage::FileManager
- Inherits:
-
Object
- Object
- Dbwatcher::Storage::FileManager
- Defined in:
- lib/dbwatcher/storage/file_manager.rb
Overview
Manages file system operations for storage classes
This class provides a centralized interface for file operations including JSON serialization, directory management, and file system utilities used throughout the storage module.
Instance Attribute Summary collapse
-
#storage_path ⇒ String
readonly
The base storage path.
Instance Method Summary collapse
-
#delete_directory(dir_path) ⇒ Boolean
Deletes a directory and its contents.
-
#delete_file(file_path) ⇒ Integer
Deletes a file.
-
#ensure_directory(path) ⇒ Array?
Ensures a directory exists.
-
#file_exists?(file_path) ⇒ Boolean
Checks if a file exists.
-
#glob_files(pattern) ⇒ Array<String>
Returns files matching a glob pattern.
-
#initialize(storage_path) ⇒ FileManager
constructor
Initializes file manager with storage path.
-
#read_json(file_path) ⇒ Object
Reads and parses a JSON file.
-
#write_json(file_path, data) ⇒ Integer
Writes data to a JSON file.
Constructor Details
#initialize(storage_path) ⇒ FileManager
Initializes file manager with storage path
Creates the base storage directory if it doesn’t exist.
24 25 26 27 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 24 def initialize(storage_path) @storage_path = storage_path ensure_directories end |
Instance Attribute Details
#storage_path ⇒ String (readonly)
Returns the base storage path.
17 18 19 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 17 def storage_path @storage_path end |
Instance Method Details
#delete_directory(dir_path) ⇒ Boolean
Deletes a directory and its contents
78 79 80 81 82 83 84 85 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 78 def delete_directory(dir_path) return false unless Dir.exist?(dir_path) FileUtils.rm_rf(dir_path) true rescue Errno::ENOENT false end |
#delete_file(file_path) ⇒ Integer
Deletes a file
70 71 72 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 70 def delete_file(file_path) File.delete(file_path) end |
#ensure_directory(path) ⇒ Array?
Ensures a directory exists
Creates the directory and any necessary parent directories.
101 102 103 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 101 def ensure_directory(path) FileUtils.mkdir_p(path) end |
#file_exists?(file_path) ⇒ Boolean
Checks if a file exists
62 63 64 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 62 def file_exists?(file_path) File.exist?(file_path) end |
#glob_files(pattern) ⇒ Array<String>
Returns files matching a glob pattern
91 92 93 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 91 def glob_files(pattern) Dir.glob(pattern) end |
#read_json(file_path) ⇒ Object
Reads and parses a JSON file
Reads the specified file and parses it as JSON with symbolized keys. Returns empty array if file doesn’t exist.
52 53 54 55 56 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 52 def read_json(file_path) return default_empty_result unless File.exist?(file_path) JSON.parse(File.read(file_path), symbolize_names: true) end |
#write_json(file_path, data) ⇒ Integer
Writes data to a JSON file
Serializes the provided data as pretty-printed JSON and writes it to the specified file path.
38 39 40 41 42 |
# File 'lib/dbwatcher/storage/file_manager.rb', line 38 def write_json(file_path, data) # Ensure parent directory exists ensure_directory(File.dirname(file_path)) File.write(file_path, JSON.pretty_generate(data)) end |