Class: CoderCompanion::CacheHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/codercompanion/cachehandler.rb

Constant Summary collapse

CACHE_DIR =
'./codercompanion_cache_data'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCacheHandler

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_cacheObject

Returns:

  • (Object)


6
7
8
# File 'lib/codercompanion/cachehandler.rb', line 6

def file_cache
  @file_cache
end

Instance Method Details

#delete_cacheObject



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_cacheJSON

Returns:

  • (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

Parameters:

  • file_path (String)

Returns:

  • (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

Parameters:

  • fresh_run (Boolean)

    should we use cache for this run?



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_cacheFile?

Returns:

  • (File, nil)


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

Parameters:

  • file_path (String)


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_datanil

Returns:

  • (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