class Attractor::CacheAdapter::JSON

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/attractor/cache.rb, line 39
def initialize
  super

  @data_directory = "tmp"
  FileUtils.mkdir_p @data_directory
  FileUtils.touch filename

  begin
    @store = ::JSON.parse(File.read(filename))
  rescue ::JSON::ParserError
    @store = {}
  end
end

Public Instance Methods

clear() click to toggle source
# File lib/attractor/cache.rb, line 73
def clear
  FileUtils.rm filename
end
filename() click to toggle source
# File lib/attractor/cache.rb, line 77
def filename
  "#{@data_directory}/attractor-cache.json"
end
persist!() click to toggle source
# File lib/attractor/cache.rb, line 69
def persist!
  File.write(filename, ::JSON.dump(@store))
end
read(file_path:) click to toggle source
# File lib/attractor/cache.rb, line 53
def read(file_path:)
  value_hash = @store[file_path]

  Value.new(**value_hash.values.first.transform_keys(&:to_sym)) unless value_hash.nil?
rescue ArgumentError => e
  puts "Couldn't rehydrate value from cache: #{e.message}"
  nil
end
write(file_path:, value:) click to toggle source
# File lib/attractor/cache.rb, line 62
def write(file_path:, value:)
  mappings = {x: :churn, y: :complexity}

  transformed_value = value.to_h.transform_keys { |k| mappings[k] || k }
  @store[file_path] = {value.current_commit => transformed_value}
end