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

#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_runObject (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_cacheObject

Returns:

  • (Object)


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

def file_cache
  @file_cache
end

Instance Method Details

#delete_cacheObject



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_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)


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

Parameters:

  • fresh_run (Boolean)

    should we use cache for this run?



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

Returns:

  • (File, nil)


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

Parameters:

  • file_path (String)


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

#write_cached_datanil

Returns:

  • (nil)


67
68
69
70
71
72
73
74
75
# File 'lib/codercompanion/cachehandler.rb', line 67

def write_cached_data
    return if 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