Method: Hash.random

Defined in:
lib/propr/random/hash.rb

.random(options = {}, m = Propr::Random) ⇒ Object

Examples:

Hash.random do
  m.bind(Integer.random){|k| m.bind(String.random){|v| m.unit([k,v]) }}
end
Hash.random do
  m.sequence([Integer.random, String.random])
end


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/propr/random/hash.rb', line 21

def random(options = {}, m = Propr::Random)
  min  = options[:min] || 0
  max  = options[:max] || 10
  pair = yield

  # @todo: Be sure we created enough *unique* keys
  #
  #   Hash.random(min: 10) do
  #     # key space could have at most 6 elements
  #     m.sequence([Integer.random(min: 0, max: 5), String.random])
  #   end
  #
  m.bind(Integer.random(options.merge(min: min, max: max))) do |size|
    m.bind(m.sequence([pair]*size)) do |pairs|
      m.unit(Hash[pairs])
    end
  end
end