NAME

Mock::Data::Generator - Utilities and optional base class for authoring generators

DESCRIPTION

This package provides a set of utility methods for writing generators, and an optional abstract base class. (a Generator does not need to inherit from this class)

GENERATORS

The most basic Mock::Data generator is a simple coderef of the form

sub ( $mockdata, \%arguments, @arguments ) { ... }

which returns a literal data item, usually a scalar. A generator can also be any object which has a "generate" method. Using an object provides more flexibility to handle cases where a user wants to combine generators.

METHODS

generate

my $data= $generator->generate($mockdata, \%named_params, @pos_params);

Like the coderef, this takes an instance of Mock::Data as the first non-self argument, followed by a hashref of named parameters, followed by arbitrary positional parameters after that.

compile

my $callable= $generator->compile(@default_params);

Return a plain coderef that most optimally performs the generation for the @default_params. This implementation just wraps $self->generate(@defaults) in a coderef. Subclasses may provide more useful optimization.

The returned coderef takes one argument, of a Mock::Data instance.

combine_generator

my $new_generator= $generator->combine_generator( $peer );

The default way to combine two generators is to create a new generator that selects each child generator 50% of the time. For generators that define a collection of possible data, it may be preferred to merge the collections in a manner different than a plain 50% split. This method allows for that custom behavior.

clone

A generator that wants to perform special behavior when the Mock::Data instance gets cloned can implement this method. I can't think of any reason a generator should ever need this, since the "generator_state" in Mock::Data gets cloned. Lack of the method indicates the generator doesn't need this feature.

AUTHOR

Michael Conrad <mike@nrdvana.net>

VERSION

version 0.04

COPYRIGHT AND LICENSE

This software is copyright (c) 2024 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.