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>



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.



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)



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.



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



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



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

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

#string(*classes, size: pick_from(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.



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

def string *classes, size: pick_from(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

#zeroQuick::Sampler<0>

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



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

def zero
  const 0
end