NAME

Mock::Data::Set - Generator which returns one item from a set

SYNOPSIS

use Mock::Data::Generator 'uniform_set', 'weighted_set';
$gen= uniform_set( 'a', 'b', 'c', 'd' )->compile;
$value= $gen->($mockdata);   # 25% chance of each of the items

$gen= weighted_set( 1 => 'a', 9 => 'b' )->compile;
$value= $gen->($mockdata);   # 10% chance of 'a', 90% chance of 'b'

$gen= uniform_set( 'a', [ 'b', 'c' ] )->compile;
$value= $gen->($mockdata);   # 50% chance of 'a', 25% chance of 'b', 25% chance of 'c'

DESCRIPTION

This object selects a random element from a list. All items are equal probability unless weights are specified to change the probability. The items of the list may be templates (as per "compile_template" in Mock::Data::Generator) which means they may also be arrayrefs that turn into a nested Set object.

The object overloads the method call operator, so it can act as a coderef, but you can also call "compile" to get a native coderef.

ATTRIBUTES

items

The arrayref of items which can be returned by this generator. Do not modify this array. If you need to change the list of items, assign a new array to this attribute.

weights

An optional arrayref of values, one value per element of items. The weight values are on an arbitrary scale chosen by the user, such that the sum of them adds up to 100%.

METHODS

new

Takes a list or hashref of attributes and returns them as an object.

new_uniform

$picker= $class->new_uniform(@items);

Construct a SetPicker from a list of items, where each item may be a template or other valid specification for compile_generator. Each item is given a uniform probability.

new_weighted

$picker= $class->new_weighted($item => $weight, ...);

Construct a SetPicker from a list of pairs of weight and item. Item may be a template or other valid specification for compile_generator. The

generate

$val= $picker->generate($datagen, \%args);

Return one random item from the set. This should be called with the reference to the Mock::DataGen and optional named arguments. (as for any generator)

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.00_001

COPYRIGHT AND LICENSE

This software is copyright (c) 2021 by Michael Conrad.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.