Class: PromptManager::Storage::FileSystemAdapter
- Inherits:
-
Object
- Object
- PromptManager::Storage::FileSystemAdapter
- Defined in:
- lib/prompt_manager/storage/file_system_adapter.rb
Constant Summary collapse
- SEARCH_PROC =
placeholder
nil
- PARAMS_EXTENSION =
'.json'.freeze
- PROMPT_EXTENSION =
'.txt'.freeze
- PROMPT_ID_FORMAT =
/^[a-zA-Z0-9\-\/_]+$/
Class Attribute Summary collapse
-
.params_extension ⇒ Object
Returns the value of attribute params_extension.
-
.prompt_extension ⇒ Object
Returns the value of attribute prompt_extension.
-
.prompts_dir ⇒ Object
Returns the value of attribute prompts_dir.
-
.search_proc ⇒ Object
Returns the value of attribute search_proc.
Class Method Summary collapse
- .config ⇒ Object
-
.list(prompt_id = nil) ⇒ Object
Ignore the incoming prompt_id.
- .path(prompt_id) ⇒ Object
Instance Method Summary collapse
-
#delete(id:) ⇒ Object
Delete prompted text and parameter values files.
- #get(id:) ⇒ Object
-
#initialize ⇒ FileSystemAdapter
constructor
A new instance of FileSystemAdapter.
-
#list ⇒ Object
Return an Array of prompt IDs.
-
#parameter_values(prompt_id) ⇒ Object
Retrieve parameter values by its id.
- #params_extension ⇒ Object
-
#path(id) ⇒ Object
Returns a Pathname object for a prompt ID text file However, it is possible that the file does not exist.
- #prompt_extension ⇒ Object
-
#prompt_text(prompt_id) ⇒ Object
Retrieve prompt text by its id.
-
#prompts_dir ⇒ Object
Instance.
-
#save(id:, text: "", parameters: {}) ⇒ Object
Save prompt text and parameter values to corresponding files.
- #search(for_what) ⇒ Object
- #search_proc ⇒ Object
Constructor Details
#initialize ⇒ FileSystemAdapter
Returns a new instance of FileSystemAdapter.
145 146 147 148 149 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 145 def initialize # NOTE: validate because main program may have made # changes outside of the config block self.class.send(:validate_configuration) # send gets around private designations of a method end |
Class Attribute Details
.params_extension ⇒ Object
Returns the value of attribute params_extension.
29 30 31 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 29 def params_extension @params_extension end |
.prompt_extension ⇒ Object
Returns the value of attribute prompt_extension.
29 30 31 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 29 def prompt_extension @prompt_extension end |
.prompts_dir ⇒ Object
Returns the value of attribute prompts_dir.
29 30 31 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 29 def prompts_dir @prompts_dir end |
.search_proc ⇒ Object
Returns the value of attribute search_proc.
29 30 31 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 29 def search_proc @search_proc end |
Class Method Details
.config ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 32 def config if block_given? yield self validate_configuration else raise ArgumentError, "No block given to config" end self end |
.list(prompt_id = nil) ⇒ Object
Ignore the incoming prompt_id
47 48 49 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 47 def list(prompt_id = nil) new.list end |
.path(prompt_id) ⇒ Object
52 53 54 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 52 def path(prompt_id) new.path(prompt_id) end |
Instance Method Details
#delete(id:) ⇒ Object
Delete prompted text and parameter values files
200 201 202 203 204 205 206 207 208 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 200 def delete(id:) validate_id(id) prompt_filepath = file_path(id, prompt_extension) params_filepath = file_path(id, params_extension) delete_with_error_handling(prompt_filepath) delete_with_error_handling(params_filepath) end |
#get(id:) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 152 def get(id:) validate_id(id) verify_id(id) { id: id, text: prompt_text(id), parameters: parameter_values(id) } end |
#list ⇒ Object
Return an Array of prompt IDs
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 223 def list(*) prompt_ids = [] Pathname.glob(prompts_dir.join("**/*#{prompt_extension}")).each do |file_path| prompt_id = file_path.relative_path_from(prompts_dir).to_s.gsub(prompt_extension, '') prompt_ids << prompt_id end prompt_ids end |
#parameter_values(prompt_id) ⇒ Object
Retrieve parameter values by its id
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 171 def parameter_values(prompt_id) params_path = file_path(prompt_id, params_extension) if params_path.exist? parms_content = read_file(params_path) deserialize(parms_content) else {} end end |
#params_extension ⇒ Object
142 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 142 def params_extension = self.class.params_extension |
#path(id) ⇒ Object
Returns a Pathname object for a prompt ID text file However, it is possible that the file does not exist.
237 238 239 240 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 237 def path(id) validate_id(id) file_path(id, prompt_extension) end |
#prompt_extension ⇒ Object
141 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 141 def prompt_extension = self.class.prompt_extension |
#prompt_text(prompt_id) ⇒ Object
Retrieve prompt text by its id
165 166 167 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 165 def prompt_text(prompt_id) read_file(file_path(prompt_id, prompt_extension)) end |
#prompts_dir ⇒ Object
Instance
139 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 139 def prompts_dir = self.class.prompts_dir |
#save(id:, text: "", parameters: {}) ⇒ Object
Save prompt text and parameter values to corresponding files
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 184 def save( id:, text: "", parameters: {} ) validate_id(id) prompt_filepath = file_path(id, prompt_extension) params_filepath = file_path(id, params_extension) write_with_error_handling(prompt_filepath, text) write_with_error_handling(params_filepath, serialize(parameters)) end |
#search(for_what) ⇒ Object
211 212 213 214 215 216 217 218 219 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 211 def search(for_what) search_term = for_what.downcase if search_proc.is_a? Proc search_proc.call(search_term) else search_prompts(search_term) end end |
#search_proc ⇒ Object
140 |
# File 'lib/prompt_manager/storage/file_system_adapter.rb', line 140 def search_proc = self.class.search_proc |