Sha256: 363d454f1240bb56ead942e3211b2592e250a8a9ef92bb82a7da8f0398354431

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Coverband
  module Adapters
    class RedisStore < Base
      BASE_KEY = 'coverband2'

      def initialize(redis, opts = {})
        @redis = redis
        @ttl             = opts[:ttl]
        @redis_namespace = opts[:redis_namespace]
      end

      def clear!
        @redis.smembers(base_key).each { |key| @redis.del("#{base_key}.#{key}") }
        @redis.del(base_key)
      end

      def base_key
        @base_key ||= [BASE_KEY, @redis_namespace].compact.join('.')
      end

      def save_report(report)
        store_array(base_key, report.keys)

        report.each do |file, lines|
          store_map("#{base_key}.#{file}", lines)
        end
      end

      def coverage
        data = {}
        redis.smembers(base_key).each do |key|
          data[key] = covered_lines_for_file(key)
        end
        data
      end

      def covered_files
        redis.smembers(base_key)
      end

      def covered_lines_for_file(file)
        @redis.hgetall("#{base_key}.#{file}")
      end

      private

      attr_reader :redis

      def store_map(key, values)
        unless values.empty?
          existing = redis.hgetall(key)
          # in redis all keys are strings
          values = Hash[values.map { |k, val| [k.to_s, val] }]
          values.merge!(existing) { |_k, old_v, new_v| old_v.to_i + new_v.to_i }
          redis.mapped_hmset(key, values)
          redis.expire(key, @ttl) if @ttl
        end
      end

      def store_array(key, values)
        redis.sadd(key, values) unless values.empty?
        redis.expire(key, @ttl) if @ttl
        values
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
coverband-2.0.2 lib/coverband/adapters/redis_store.rb
coverband-2.0.2.alpha2 lib/coverband/adapters/redis_store.rb
coverband-2.0.2.alpha lib/coverband/adapters/redis_store.rb
coverband-2.0.1 lib/coverband/adapters/redis_store.rb
coverband-2.0.1.alpha lib/coverband/adapters/redis_store.rb