Sha256: 6572b9145f43bd64ec4882b136f31665392d97ab762b753b63842a2da8f7c657

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Strict
  class Configuration
    attr_reader :random, :sample_rate

    def initialize(random: nil, sample_rate: nil)
      self.random = random || Random.new
      self.sample_rate = sample_rate || 1
    end

    def random=(random)
      case random
      when Random::Formatter
        @random = random
      else
        raise Strict::Error, "Expected a Random::Formatter, got: #{random.inspect}."
      end
    end

    def sample_rate=(rate)
      case rate
      when 0..1
        @sample_rate = rate
      else
        raise Strict::Error, "Expected a sample rate between 0 and 1 (inclusive), got: #{rate.inspect}. " \
                             "A rate of 0 will disable strict validation. " \
                             "A rate of 1 will validate 100% of the time. " \
                             "A rate of 0.25 will validate roughly 25% of the time."
      end
    end

    def validate?
      sample_rate >= 1 || (sample_rate > 0 && random.rand < sample_rate) # rubocop:disable Style/NumericPredicate
    end

    def to_h
      {
        random: random,
        sample_rate: sample_rate
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
strict-1.5.0 lib/strict/configuration.rb