Sha256: cd784a8b2860840e04891bc79caf2c69537a6a68db726235e3f8d06563a827a9

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require 'yaml/store'
# TestDiff module
module TestDiff
  # class used to build the coverage file
  class Storage
    attr_reader :folder

    def initialize(folder = 'test_diff_coverage')
      @folder = folder
    end

    def set(file, coverage_data)
      fail 'Data must be a Hash' unless coverage_data.is_a?(Hash)
      get_store(file).transaction do |store|
        store.roots.each do |key|
          store.delete(key)
        end
        coverage_data.keys.sort.each do |key|
          store[key] = coverage_data[key]
        end
      end
    end

    def get(file)
      get_store(file).transaction(true) do |store|
        coverage_data = {}
        store.roots.each do |key|
          coverage_data[key] = store[key]
        end
        coverage_data
      end
    end

    def find_for(files, sub_folder = nil)
      results = []
      root_folder = @folder
      root_folder += "/#{sub_folder}" if sub_folder
      Dir["#{root_folder}/**/*.yml"].each do |storage_file|
        find_for_storage_file(results, storage_file, files)
      end
      results
    end

    def clear
      Dir["#{@folder}/**/*.yml"].each do |storage_file|
        File.delete(storage_file)
      end
    end

    private

    def find_for_storage_file(results, storage_file, files)
      YAML::Store.new(storage_file).transaction(true) do |store|
        found_files = files & store.roots
        found_files.each do |file|
          if _active_file?(store[file])
            results << storage_file.gsub('.yml', '').gsub("#{@folder}/", '')
          end
        end
      end
    end

    def _active_file?(file)
      !file.to_s.split(',').delete_if { |s| s == '' || s == '0' }.empty?
    end

    def get_store(file)
      dir = File.dirname("#{@folder}/#{file}")
      filename = File.basename(file) + '.yml'
      FileUtils.mkdir_p(dir)
      YAML::Store.new "#{dir}/#{filename}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
test_diff-0.2.0 lib/test_diff/storage.rb
test_diff-0.1.2 lib/test_diff/storage.rb