Method: Hash.random_vals

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

.random_vals(hash, m = Propr::Random) ⇒ Object

Examples:

Hash.random_vals \
  name:   String.random,
  count:  Integer.random(min: 0),
  weight: Float.random


46
47
48
49
50
51
52
53
54
# File 'lib/propr/random/hash.rb', line 46

def random_vals(hash, m = Propr::Random)
  # Convert hash of key => generator to a list of pair-generators,
  # where the pairs correspond to the original set of [key, value]
  pairs = hash.map{|k,v| m.bind(v){|v| m.unit([k, v]) }}

  m.bind(m.sequence(pairs)) do |pairs|
    m.unit(Hash[pairs])
  end
end