Class: Randrizer::Types::Optional

Inherits:
BaseType
  • Object
show all
Defined in:
lib/randrizer/types/optional.rb

Constant Summary collapse

PRESENCE_MAYBE =
0.5

Instance Method Summary collapse

Constructor Details

#initialize(inner_type:, presence_prob: PRESENCE_MAYBE) ⇒ Optional

Returns a new instance of Optional.



11
12
13
14
# File 'lib/randrizer/types/optional.rb', line 11

def initialize(inner_type:, presence_prob: PRESENCE_MAYBE)
  @inner_type = inner_type
  @presence_prob = presence_prob
end

Instance Method Details

#evalObject



21
22
23
24
25
# File 'lib/randrizer/types/optional.rb', line 21

def eval
  return SKIP if rand > @presence_prob

  @inner_type.eval
end

#validate!Object



16
17
18
19
# File 'lib/randrizer/types/optional.rb', line 16

def validate!
  raise ValidationError("presence_prob must be < 1.0") if @presence_prob > 1.0
  raise ValidationError("presence_prob must be > 0.0") if @presence_prob < 0.0
end