Sha256: 172e673f24a9a1932d406b76b7a74b15a08d76af4341bc8ce37f198c03db8abc

Contents?: true

Size: 1.31 KB

Versions: 4

Compression:

Stored size: 1.31 KB

Contents

require 'digest'
require 'fileutils'
require 'renuo_bin_check/result'

module RenuoBinCheck
  class Cacher
    def initialize(name, paths)
      @name = name
      @file_names = paths.map { |path| Dir[path] }.flatten.sort
      @hash = hash_files
    end

    def exists?
      File.exist?("tmp/bin-check/#{@name}/#{@hash}")
    end

    def result
      read
    end

    def cache(result)
      FileUtils.mkdir_p "tmp/bin-check/#{@name}/#{@hash}"
      File.write "tmp/bin-check/#{@name}/#{@hash}/standard_output", result.standard_output
      File.write "tmp/bin-check/#{@name}/#{@hash}/error_output", result.error_output
      File.write "tmp/bin-check/#{@name}/#{@hash}/exit_code", result.exit_code
    end

    private

    def read
      standard_output = File.read("tmp/bin-check/#{@name}/#{@hash}/standard_output")
      error_output = File.read("tmp/bin-check/#{@name}/#{@hash}/error_output")
      exit_code = File.read("tmp/bin-check/#{@name}/#{@hash}/exit_code").to_i
      Result.new(standard_output, error_output, exit_code)
    end

    def hash_files
      Digest::MD5.hexdigest @file_names.map { |file_name| hash_file(file_name) if File.file? file_name }.to_s
    end

    #:reek:UtilityFunction
    def hash_file(file_name)
      Digest::MD5.file(file_name).hexdigest + Digest::MD5.hexdigest(file_name)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
renuo-bin-check-1.0.0.beta2 lib/renuo_bin_check/cacher.rb
renuo-bin-check-1.0.0.beta1 lib/renuo_bin_check/cacher.rb
renuo-bin-check-0.2.1 lib/renuo_bin_check/cacher.rb
renuo-bin-check-0.2.0 lib/renuo_bin_check/cacher.rb