20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/mrproper/data_block.rb', line 20
def to_proc
return @block if @block
case @spec
when Array
if @spec.size == 1
Proc.new { rand(20).times.map { DataBlock.new(@spec.first).call } }
else
Proc.new { @spec.map { |s| DataBlock.new(s).call }}
end
when Hash
if @spec.size == 1
Proc.new do
{}.tap do |h|
rand(20).times.each do
h[DataBlock.new(@spec.keys.first).call] = DataBlock.new(@spec.values.first).call
end
end
end
else
Proc.new do
{}.tap do |h|
@spec.each do |k, v|
h[DataBlock.new(k).call] = DataBlock.new(v).call
end
end
end
end
when Range
case @spec.begin
when Integer
Proc.new { @spec.begin + rand(@spec.end - @spec.begin) }
when Float
Proc.new { @spec.begin + rand * (@spec.end - @spec.begin) }
else
Proc.new { @spec }
end
when Class
if @spec == Integer
DataBlock.new(-500..500).to_proc
elsif @spec == Float
DataBlock.new(-5.0..5.0).to_proc
elsif @spec == String
Proc.new { String.random(rand(10)) }
elsif @spec == Symbol
Proc.new { DataBlock.new(String).call.to_sym }
elsif @spec == NilClass
Proc.new { nil }
end
else
Proc.new { @spec }
end
end
|