Class: Forall::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/forall/input.rb

Direct Known Subclasses

All, Some

Defined Under Namespace

Classes: All, Some

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(value = nil, &block) ⇒ Object

Parameters:

  • value (Proc | Enumerable) (defaults to: nil)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/forall/input.rb', line 7

def build(value = nil, &block)
  if Input === value
    value
  elsif Enumerable === value
    # This includes Range
    All.new(value)
  elsif Proc === value
    Some.new(value)
  elsif block_given?
    Some.new(block)
  else
    raise TypeError, "argument must be a Proc or Enumerable"
  end
end

.exhaustive(value = nil, &block) ⇒ Object

Parameters:

  • value (Enumerable) (defaults to: nil)


23
24
25
26
27
28
29
# File 'lib/forall/input.rb', line 23

def exhaustive(value = nil, &block)
  if Enumerable === value
    All.new(value)
  else
    raise TypeError, "argument must be Enumerable"
  end
end

.sampled(value = nil, &block) ⇒ Object

Parameters:

  • value (Proc | Array | Enumerator | ... | Enumerable) (defaults to: nil)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/forall/input.rb', line 32

def sampled(value = nil, &block)
  if block_given?
    Some.new(block)
  elsif Proc === value
    Some.new(value)
  elsif Range === value or value.respond_to?(:sample)
    Some.new(lambda{|rnd| rnd.sample(value) })
  elsif value.respond_to?(:to_a)
    array = value.to_a
    Some.new(lambda{|rnd| rnd.sample(array) })
  else
    raise TypeError, "argument must be a Proc or respond_to?(:sample) or respond_to?(:to_a)"
  end
end

Instance Method Details

#shrink(value = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/forall/input.rb', line 48

def shrink(value = nil, &block)
  if block_given?
    @shrink = block
    self
  else
    @shrink
  end
end