Class: ERBLint::Cache
- Inherits:
-
Object
- Object
- ERBLint::Cache
- Defined in:
- lib/erb_lint/cache.rb
Constant Summary collapse
- CACHE_DIRECTORY =
".erb-lint-cache"
Instance Method Summary collapse
- #cache_dir_exists? ⇒ Boolean
- #clear ⇒ Object
- #close ⇒ Object
- #get(filename, file_content) ⇒ Object
-
#initialize(config, cache_dir = nil) ⇒ Cache
constructor
A new instance of Cache.
- #prune_cache ⇒ Object
- #set(filename, file_content, offenses_as_json) ⇒ Object
Constructor Details
#initialize(config, cache_dir = nil) ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 11 12 13 |
# File 'lib/erb_lint/cache.rb', line 7 def initialize(config, cache_dir = nil) @config = config @cache_dir = cache_dir || CACHE_DIRECTORY @hits = [] @new_results = [] puts "Cache mode is on" end |
Instance Method Details
#cache_dir_exists? ⇒ Boolean
59 60 61 |
# File 'lib/erb_lint/cache.rb', line 59 def cache_dir_exists? File.directory?(@cache_dir) end |
#clear ⇒ Object
63 64 65 66 67 68 |
# File 'lib/erb_lint/cache.rb', line 63 def clear return unless cache_dir_exists? puts "Clearing cache by deleting cache directory" FileUtils.rm_r(@cache_dir) end |
#close ⇒ Object
41 42 43 |
# File 'lib/erb_lint/cache.rb', line 41 def close prune_cache end |
#get(filename, file_content) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/erb_lint/cache.rb', line 15 def get(filename, file_content) file_checksum = checksum(filename, file_content) begin cache_file_contents_as_offenses = JSON.parse( File.read(File.join(@cache_dir, file_checksum)) ).map do |offense_hash| ERBLint::CachedOffense.new(offense_hash) end rescue Errno::ENOENT return false end @hits.push(file_checksum) cache_file_contents_as_offenses end |
#prune_cache ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/erb_lint/cache.rb', line 45 def prune_cache if hits.empty? puts "Cache being created for the first time, skipping prune" return end cache_files = Dir.new(@cache_dir).children cache_files.each do |cache_file| next if hits.include?(cache_file) || new_results.include?(cache_file) File.delete(File.join(@cache_dir, cache_file)) end end |
#set(filename, file_content, offenses_as_json) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/erb_lint/cache.rb', line 30 def set(filename, file_content, offenses_as_json) file_checksum = checksum(filename, file_content) @new_results.push(file_checksum) FileUtils.mkdir_p(@cache_dir) File.open(File.join(@cache_dir, file_checksum), "wb") do |f| f.write(offenses_as_json) end end |