Class: Aidp::Storage::JsonStorage
- Inherits:
-
Object
- Object
- Aidp::Storage::JsonStorage
- Includes:
- RescueLogging
- Defined in:
- lib/aidp/storage/json_storage.rb
Overview
Simple JSON file storage for structured data
Instance Method Summary collapse
-
#delete(filename) ⇒ Object
Delete file.
-
#exists?(filename) ⇒ Boolean
Check if file exists.
-
#initialize(base_dir = ".aidp") ⇒ JsonStorage
constructor
A new instance of JsonStorage.
-
#list ⇒ Object
List all JSON files.
-
#load(filename) ⇒ Object
Load data from JSON file.
-
#metadata(filename) ⇒ Object
Get file metadata.
-
#store(filename, data) ⇒ Object
Store data as JSON file.
-
#update(filename, data) ⇒ Object
Update existing data.
Methods included from RescueLogging
__log_rescue_impl, log_rescue, #log_rescue
Constructor Details
#initialize(base_dir = ".aidp") ⇒ JsonStorage
Returns a new instance of JsonStorage.
13 14 15 16 |
# File 'lib/aidp/storage/json_storage.rb', line 13 def initialize(base_dir = ".aidp") @base_dir = sanitize_base_dir(base_dir) ensure_directory_exists end |
Instance Method Details
#delete(filename) ⇒ Object
Delete file
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/aidp/storage/json_storage.rb', line 100 def delete(filename) file_path = get_file_path(filename) return {success: true, message: "File does not exist"} unless File.exist?(file_path) File.delete(file_path) {success: true, message: "File deleted"} rescue => error log_rescue(error, component: "json_storage", action: "delete", fallback: {success: false}, filename: filename, path: file_path) {success: false, error: error.} end |
#exists?(filename) ⇒ Boolean
Check if file exists
95 96 97 |
# File 'lib/aidp/storage/json_storage.rb', line 95 def exists?(filename) File.exist?(get_file_path(filename)) end |
#list ⇒ Object
List all JSON files
117 118 119 120 121 122 123 |
# File 'lib/aidp/storage/json_storage.rb', line 117 def list return [] unless Dir.exist?(@base_dir) Dir.glob(File.join(@base_dir, "**", "*.json")).map do |file| File.basename(file, ".json") end end |
#load(filename) ⇒ Object
Load data from JSON file
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/aidp/storage/json_storage.rb', line 48 def load(filename) file_path = get_file_path(filename) return nil unless File.exist?(file_path) content = File.read(file_path) json_data = JSON.parse(content) json_data["data"] rescue => error log_rescue(error, component: "json_storage", action: "load", fallback: nil, filename: filename, path: (defined?(file_path) ? file_path : nil)) nil end |
#metadata(filename) ⇒ Object
Get file metadata
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/aidp/storage/json_storage.rb', line 126 def (filename) file_path = get_file_path(filename) return nil unless File.exist?(file_path) content = File.read(file_path) json_data = JSON.parse(content) { filename: filename, file_path: file_path, created_at: json_data["created_at"], updated_at: json_data["updated_at"], size: File.size(file_path) } rescue => error log_rescue(error, component: "json_storage", action: "metadata", fallback: nil, filename: filename, path: (defined?(file_path) ? file_path : nil)) nil end |
#store(filename, data) ⇒ Object
Store data as JSON file
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aidp/storage/json_storage.rb', line 19 def store(filename, data) file_path = get_file_path(filename) FileUtils.mkdir_p(File.dirname(file_path)) json_data = { "data" => data, "created_at" => Time.now.iso8601, "updated_at" => Time.now.iso8601 } File.write(file_path, JSON.pretty_generate(json_data)) { filename: filename, file_path: file_path, stored_at: Time.now, success: true } rescue => error log_rescue(error, component: "json_storage", action: "store", fallback: {success: false}, filename: filename, path: file_path) {filename: filename, error: error., success: false} end |
#update(filename, data) ⇒ Object
Update existing data
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/aidp/storage/json_storage.rb', line 66 def update(filename, data) existing = load(filename) return store(filename, data) unless existing file_path = get_file_path(filename) json_data = JSON.parse(File.read(file_path)) json_data["data"] = data json_data["updated_at"] = Time.now.iso8601 File.write(file_path, JSON.pretty_generate(json_data)) { filename: filename, file_path: file_path, updated_at: Time.now, success: true } rescue => error log_rescue(error, component: "json_storage", action: "update", fallback: {success: false}, filename: filename, path: (defined?(file_path) ? file_path : nil)) {filename: filename, error: error., success: false} end |