Class: Dbwatcher::Storage::FileManager

Inherits:
Object
  • Object
show all
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.

Examples:

manager = FileManager.new("/path/to/storage")
manager.write_json("data.json", { key: "value" })
data = manager.read_json("data.json")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path) ⇒ FileManager

Initializes file manager with storage path

Creates the base storage directory if it doesn’t exist.

Parameters:

  • storage_path (String)

    base path for file operations



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_pathString (readonly)

Returns the base storage path.

Returns:

  • (String)

    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

Parameters:

  • dir_path (String)

    path to directory to delete

Returns:

  • (Boolean)

    true if directory was deleted, false if it didn’t exist



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

Parameters:

  • file_path (String)

    path to file to delete

Returns:

  • (Integer)

    number of files deleted (1 or 0)



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.

Parameters:

  • path (String)

    directory path to create

Returns:

  • (Array, nil)

    array of created directories or nil if already exists



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

Parameters:

  • file_path (String)

    path to check

Returns:

  • (Boolean)

    true if 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

Parameters:

  • pattern (String)

    glob pattern to match

Returns:

  • (Array<String>)

    array of matching file paths



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.

Parameters:

  • file_path (String)

    path to the JSON file

Returns:

  • (Object)

    parsed JSON data with symbolized keys

Raises:

  • (JSON::ParserError)

    if file contains invalid JSON



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.

Parameters:

  • file_path (String)

    path to write the JSON file

  • data (Object)

    data to serialize as JSON

Returns:

  • (Integer)

    number of bytes written

Raises:

  • (JSON::GeneratorError)

    if data cannot be serialized



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