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
Instance Method Summary collapse
- #delete_cache ⇒ Object
- #find_cache ⇒ JSON
-
#initialize ⇒ 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 ⇒ CacheHandler
Returns a new instance of CacheHandler.
8 9 10 |
# File 'lib/codercompanion/cachehandler.rb', line 8 def initialize @file_cache = {} end |
Instance Attribute Details
#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
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/codercompanion/cachehandler.rb', line 44 def delete_cache if !CoderCompanion.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 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
36 37 38 39 40 41 42 |
# File 'lib/codercompanion/cachehandler.rb', line 36 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
24 25 26 27 28 29 30 31 32 |
# File 'lib/codercompanion/cachehandler.rb', line 24 def load_cache(fresh_run) if !CoderCompanion.dry_run? if fresh_run delete_cache else find_cache end end end |
#open_cache ⇒ File?
13 14 15 16 17 18 19 20 21 |
# File 'lib/codercompanion/cachehandler.rb', line 13 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
56 57 58 59 60 61 62 63 |
# File 'lib/codercompanion/cachehandler.rb', line 56 def update_cache_path(file_path) if !CoderCompanion.dry_run? if !self.file_cache self.file_cache = {} end self.file_cache[file_path] = encrypt(File.open(file_path).read) end end |
#write_cached_data ⇒ nil
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/codercompanion/cachehandler.rb', line 66 def write_cached_data if !CoderCompanion.dry_run? if !Dir.exists?(CACHE_DIR) Dir.mkdir(CACHE_DIR) end File.open(CACHE_DIR + '/cache.data', 'w+') do |f| f.write(self.file_cache.to_json) end end end |