NAME

Data::Maker::Field - a Moose role that is consumed by all Data::Maker field classes; the ones included with Data::Maker and the ones that you write yourself to extend Data::Maker's capabilities.

SYNOPSIS

use Data::Maker;
use Data::Maker::Field::Person::LastName;
use MyField;

my $maker = Data::Maker->new(
  record_count => 10_000,
  delimiter => "\t",
  fields => [
    { 
      name => 'lastname', 
      class => 'Data::Maker::Field::Person::LastName'
    },
    { 
      name => 'ssn', 
      class => 'Data::Maker::Field::Format',
      args => {
        format => '\d\d\d-\d\d-\d\d\d\d'
      }
    },
    { 
      name => 'myfield', 
      class => 'MyField'
    },
  ]
);

DESCRIPTION

To write your own Data::Maker field class, create a Moose class that consumes the Data::Maker::Field role.

 package MyField;
 use Moose;
 with 'Data::Maker::Field';

 has some_attribute => ( is => 'rw' ); 

 sub generate_value {
   my ($this, $maker) = @_;
   # amazing code here...
   return $amazing_value;
 }

 1;

You must provide a generate_value method, which is the method that will be called to generate the value of this field for each record.

Any Moose attribute that you define (some_flag in the above example) can then be passed in as an argument in your field definition and will be available as an object method inside your generate_value method (or any other class method, for that matter):

# define the field in your Data::Maker constructor:

my $maker = Data::Maker->new(
  record_count => 10,
  fields => [ 
    {
      name => 'myfield',
      class => 'MyField',
      args => { 
        some_attribute => 'blah' 
      }
    }
  ]
);

# And then later, in generate_value()...

sub generate_value {
  my ($this, $maker) = @_;
  # $this->some_attribute now return "blah"
  # amazing code here...
  return $amazing_value;
}

ATTRIBUTES

The following public Moose attributes are supported (the data type of each attribute is also listed)

name

The name of the field. This is used to refer to this field from other fields, and can also be used as a method to the Data::Maker::Record object to retrieve the value for this field.

class

The name of the class to be used for this field

args (HashRef)

The hash reference of arguments to be passed to this field

formatted (CodeRef)

A code reference that will be executed on the value after it is generated, but before it is returned