Sha256: ffcd0260a4381b1b580871b6372d1e216f4d4d3a70c02efc209b0193e5d59280

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require "fileutils"
require "psych"

module Attractor
  class Cache
    class << self
      def read(file_path:)
        adapter.read(file_path: file_path)
      end

      def write(file_path:, value:)
        adapter.write(file_path: file_path, value: value)
      end

      private

      def adapter
        @@adapter ||= CacheAdapter::JSON.instance
      end
    end
  end

  module CacheAdapter
    class Base
      include Singleton
    end

    class JSON < Base
      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

      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

      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}
        File.write(filename, ::JSON.dump(@store))
      end

      def filename
        "#{@data_directory}/attractor-cache.json"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attractor-2.3.0 lib/attractor/cache.rb~
attractor-2.2.0 lib/attractor/cache.rb