Module: Gen

Defined in:
lib/gen.rb,
lib/gen/test.rb,
lib/gen/test/version.rb

Defined Under Namespace

Modules: Test Classes: Generator

Class Method Summary collapse

Class Method Details

.generate(generator) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/gen.rb', line 38

def generate(generator)
  if generator.respond_to?(:generate)
    generator.generate
  elsif generator.respond_to?(:call)
    STDERR.puts "WARN: Objects that implement `call` are no longer automatically generators you must implement `generate`"
    generator
  else
    generator
  end
end

.generator?(val) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/gen.rb', line 13

def generator?(val)
  val.respond_to?(:generate)
end

.one_of(generators) ⇒ Object



34
35
36
# File 'lib/gen.rb', line 34

def one_of(generators)
  generate(generators.to_a.sample)
end

.sample(gen, n = 10) ⇒ Object



17
18
19
# File 'lib/gen.rb', line 17

def sample(gen, n = 10)
  (0..n).map { generate(gen) }
end

.where(gen, pred, max_tries = 10) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gen.rb', line 21

def where(gen, pred, max_tries = 10)
  Generator.new do
    tries = 0
    until (val = generate(gen)) and pred.call(val)
      tries += 1
      if tries >= max_tries
        raise "We've tried generating a value from #{gen.inspect} with #{pred.inspect} #{max_tries} times and failed"
      end
    end
    val
  end
end