Class: CoderCompanion::CacheHandler
- Inherits:
-
Object
- Object
- CoderCompanion::CacheHandler
- Defined in:
- lib/codercompanion/cachehandler.rb
Constant Summary collapse
- CACHE_DIR =
'./codercompanion_cache_data'
Instance Attribute Summary collapse
-
#dry_run ⇒ Object
readonly
Returns the value of attribute dry_run.
- #file_cache ⇒ Object
Instance Method Summary collapse
- #delete_cache ⇒ Object
- #find_cache ⇒ JSON
-
#initialize(dry_run = false) ⇒ CacheHandler
constructor
A new instance of CacheHandler.
- #is_file_modified?(file_path) ⇒ Boolean
- #load_cache(fresh_run) ⇒ Object
- #open_cache ⇒ File?
- #update_cache_path(file_path) ⇒ Object
- #write_cached_data ⇒ nil
Constructor Details
#initialize(dry_run = false) ⇒ CacheHandler
Returns a new instance of CacheHandler.
10 11 12 13 |
# File 'lib/codercompanion/cachehandler.rb', line 10 def initialize(dry_run=false) @file_cache = {} @dry_run = dry_run end |
Instance Attribute Details
#dry_run ⇒ Object (readonly)
Returns the value of attribute dry_run.
8 9 10 |
# File 'lib/codercompanion/cachehandler.rb', line 8 def dry_run @dry_run end |
#file_cache ⇒ Object
6 7 8 |
# File 'lib/codercompanion/cachehandler.rb', line 6 def file_cache @file_cache end |
Instance Method Details
#delete_cache ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/codercompanion/cachehandler.rb', line 46 def delete_cache return if dry_run begin File.delete(CACHE_DIR + '/cache.data') CoderCompanion::j_print CoderCompanion::warning_format("Running a fresh sesh") rescue CoderCompanion::j_print CoderCompanion::warning_format("No cache to delete. Running a fresh sesh") end end |
#find_cache ⇒ JSON
78 79 80 81 82 83 84 85 86 |
# File 'lib/codercompanion/cachehandler.rb', line 78 def find_cache begin file = open_cache self.file_cache = JSON.parse(file.read) rescue CoderCompanion::j_print CoderCompanion::warning_format("Cache not found running a fresh sesh") end nil end |
#is_file_modified?(file_path) ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/codercompanion/cachehandler.rb', line 38 def is_file_modified?(file_path) cache = encrypt(File.open(file_path).read) if self.file_cache && self.file_cache[ file_path ] && (self.file_cache[ file_path ] == cache) return false end true end |
#load_cache(fresh_run) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/codercompanion/cachehandler.rb', line 27 def load_cache(fresh_run) return if dry_run if fresh_run delete_cache else find_cache end end |
#open_cache ⇒ File?
16 17 18 19 20 21 22 23 24 |
# File 'lib/codercompanion/cachehandler.rb', line 16 def open_cache file = nil begin file = File.open(CACHE_DIR + '/cache.data') rescue CoderCompanion::warning_format("Could not find cache") end file end |
#update_cache_path(file_path) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/codercompanion/cachehandler.rb', line 57 def update_cache_path(file_path) return if dry_run if !self.file_cache self.file_cache = {} end self.file_cache[file_path] = encrypt(File.open(file_path).read) end |