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

#string(*classes) ⇒ Object

TODO:

document character classes

This sampler honors upper_bound config variable.

The sampler will produce strings of random (between 0 and upper_bound) length made up of characters belonging to supplied named classes.

Parameters:

  • classes (Array<Symbol>)

    Character classes to pick from.



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

def string *classes
  classes = [:printable] if classes.empty?
  repertoire = DSL::CharacterClass.expand(*classes)
  feed { repertoire.sample(rand(upper_bound)).join }
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