Module: Quick::Sampler::DSL::SimpleValues

Included in:
Quick::Sampler::DSL
Defined in:
lib/quick/sampler/dsl/simple_values.rb

Overview

Samplers of simple values to form the basis of the sampled data structure.

Note from the future

In the future simple values are sampled from other excellent Gems from behind a composable Quick Sampler API. In the mean time this is possible at the cost of readablity:

Examples:

Faker integration


Quick::Sampler.compile description: "email address" do
  feed { Faker::Internet.email }
end

Instance Method Summary collapse

Instance Method Details

#booleanQuick::Sampler<Boolean>

Returns a sampler of true and false values.

Returns:



74
75
76
# File 'lib/quick/sampler/dsl/simple_values.rb', line 74

def boolean
  pick_from([true, false])
end

#const(const) ⇒ Quick::Sampler<Anything>

Degenerate constant sampler. Will probably be superseeded by a cleaner smarter syntax as I get a better hang of it.

Parameters:

  • const (Anything)

    the value to keep on sampling

Returns:



24
25
26
# File 'lib/quick/sampler/dsl/simple_values.rb', line 24

def const const
  feed { const }
end

#fixnumQuick::Sampler<Fixnum>

Samples random fixnums (smaller integers that can be handled quickly by the CPU itself)

Returns:



43
44
45
# File 'lib/quick/sampler/dsl/simple_values.rb', line 43

def fixnum
  pick_from(FixnumRange)
end

#integerQuick::Sampler<Fixnum>

A sampler of integers prefering smaller ones

It will however sample a large one (from the Fixnum range) occasionally.

Returns:



62
63
64
65
66
67
68
# File 'lib/quick/sampler/dsl/simple_values.rb', line 62

def integer
  one_of_weighted(fixnum => 5,
                  pick_from(-1_000_000_000..1_000_000_000) => 7,
                  pick_from(-1000..1000) => 9,
                  pick_from(-100..100) => 11,
                  pick_from(-20..20) => 17)
end

#negative_fixnumQuick::Sampler<Fixnum> Also known as: negative_integer

Returns a sampler of negative fixnums.

Returns:



48
49
50
# File 'lib/quick/sampler/dsl/simple_values.rb', line 48

def negative_fixnum
  pick_from(FixnumRange.min..-1)
end

#positive_fixnumQuick::Sampler<Fixnum> Also known as: positive_integer

Returns a sampler of positive fixnums.

Returns:



53
54
55
# File 'lib/quick/sampler/dsl/simple_values.rb', line 53

def positive_fixnum
  pick_from(1..FixnumRange.max)
end

#probabilityQuick::Sampler<Float>

"Probability" sampler

Returns:

  • (Quick::Sampler<Float>)

    a sampler emitting a float in range 0.0..1.0



99
100
101
# File 'lib/quick/sampler/dsl/simple_values.rb', line 99

def probability
  pick_from(0..1.0)
end

#string(*classes, size: 1..10) ⇒ Object

TODO:

document character classes

The sampler will produce strings of whose length is controlled by size: argument made up of characters belonging to supplied named classes.

Parameters:

  • *classes (Array<Symbol>)

    Character classes to pick from.

  • size: (Integer, Range, Quick::Sampler<Integer>) (defaults to: 1..10)

    Length of the string to generate



88
89
90
91
92
93
# File 'lib/quick/sampler/dsl/simple_values.rb', line 88

def string *classes, size: 1..10
  classes = [:printable] if classes.empty?
  repertoire = DSL::CharacterClass.expand(*classes)
  size = pick_from(size) if Range === size
  send_to( send_to(repertoire, :sample, size), :join )
end

#weighted_truth(weight) ⇒ Quick::Sampler<Boolean>

Weighted truth sampler

Parameters:

  • weight (Float)

    probably of emitting true

Returns:

  • (Quick::Sampler<Boolean>)

    a sampler emitting true with probability weight or false with probability (1.0 - weight)



109
110
111
# File 'lib/quick/sampler/dsl/simple_values.rb', line 109

def weighted_truth weight
  one_of_weighted true => weight, false => (1 - weight)
end

#zeroQuick::Sampler<0>

Degenerate sampler of zeros. Like #const this will probably go away soon.

Returns:



32
33
34
# File 'lib/quick/sampler/dsl/simple_values.rb', line 32

def zero
  const 0
end