Class: FiberAudit::Runtime::Sampler

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/runtime/sampler.rb

Constant Summary collapse

RANDOM_SOURCE =
-> { Random.rand }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy:, random: RANDOM_SOURCE) ⇒ Sampler

Returns a new instance of Sampler.



12
13
14
15
16
17
18
19
# File 'lib/fiber_audit/runtime/sampler.rb', line 12

def initialize(policy:, random: RANDOM_SOURCE)
  raise RuntimeContractError, 'policy must be a FiberAudit::Runtime::Policy' unless policy.is_a?(Policy)
  raise RuntimeContractError, 'random source must respond to call' unless random.respond_to?(:call)

  @policy = policy
  @random = random
  freeze
end

Instance Attribute Details

#policyObject (readonly)

Returns the value of attribute policy.



10
11
12
# File 'lib/fiber_audit/runtime/sampler.rb', line 10

def policy
  @policy
end

Instance Method Details

#sample?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/fiber_audit/runtime/sampler.rb', line 21

def sample?
  policy.sample?(draw: @random.call)
end