Sha256: be99a3079c99eb3e82c39f4ec1bf4417c24b3d284610221a739b6cd68b25f01d

Contents?: true

Size: 718 Bytes

Versions: 1

Compression:

Stored size: 718 Bytes

Contents

module BloodContracts
  class Statistics
    extend Dry::Initializer
    param :iterations
    option :storage, default: -> { Hash.new(0) }

    def store(rule)
      storage[rule] += 1
    end

    def to_h
      Hash[storage.map { |rule_name, times| [rule_name, rule_stats(times)] }]
    end

    def to_s
      to_h.map do |name, occasions|
        " - '#{name}' happened #{occasions.times} time(s) "\
        "(#{(occasions.percent * 100).round(2)}% of the time)"
      end.join("; \n")
    end

    def found_unexpected_behavior?
      storage.key?(Storage::UNDEFINED_RULE)
    end

    private

    def rule_stats(times)
      Hashie::Mash.new(times: times, percent: (times.to_f / iterations))
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blood_contracts-0.2.0 lib/blood_contracts/statistics.rb