From: mikew Date: Fri, 11 Oct 2013 15:22:46 +0000 (-0400) Subject: - Add predump_hook and prepopulate_hook X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d556533cd1ec364f1425552d562ad77191651f35;p=dbsrgits%2FDBIx-Class-Fixtures.git - Add predump_hook and prepopulate_hook - Add documentation for these options --- diff --git a/lib/DBIx/Class/Fixtures.pm b/lib/DBIx/Class/Fixtures.pm index 87ff12c..2290490 100644 --- a/lib/DBIx/Class/Fixtures.pm +++ b/lib/DBIx/Class/Fixtures.pm @@ -567,12 +567,47 @@ directory. For example: /home/me/app/fixtures/artist/3.fix /home/me/app/fixtures/producer/5.fix -schema and directory are required attributes. also, one of config or all must -be specified. +C and C are required attributes. Also, one of C or C must +be specified. The attributes HashRef can have the following parameters: -Lastly, the C parameter can be a Perl HashRef instead of a file name. -If this form is used your HashRef should conform to the structure rules defined -for the JSON representations. +=over + +=item all + +A boolean which defaults to false. If true, dump everything that is in the schema. + +=item config + +Filename or HashRef. One of the C or C attributes must be set. + +If the HashRef form is used your HashRef should conform to the structure rules +defined for the JSON representations. + +=item schema + +DBIx::Class::Schema object for the data you want to dump + +=item directory + +directory to store the dumped objects + +=item predump_hook + +A code reference that will be called for each row returned. It will be provided +the Result Source and the row as a HashRef. The row can be modified before being +written to the fixture files. For example: + + $fixture->dump({ + ..., + predump_hook => sub { + my ($source, $data) = @_; + if ($source->name eq "ResultSource_X") { + $data->{'sensitive_row'} = 'redacted'; + } + } + ); + +=back =cut @@ -594,6 +629,10 @@ sub dump { return DBIx::Class::Exception->throw("'excludes' param only works when using the 'all' param"); } + if($params->{predump_hook} && ref($params->{predump_hook} ne "CODE")) { + return DBIx::Class::Exception->throw('predump_hook param should be a coderef'); + } + my $schema = $params->{schema}; my $config; if ($params->{config}) { @@ -702,6 +741,7 @@ sub dump { } $source_options{set_dir} = $tmp_output_dir; + $source_options{predump_hook} = $params->{predump_hook} if (exists($params->{predump_hook})); $self->dump_rs($rs, \%source_options ); } @@ -902,6 +942,7 @@ sub dump_object { } # do the actual dumping + $params->{predump_hook}->($src, \%ds) if ( exists($params->{predump_hook}) ); my $serialized = Dump(\%ds)->Out(); $file->openw->print($serialized); } @@ -1229,6 +1270,11 @@ If you wish to deal with cleaning the schema yourself, then pass in a C attribute containing the connected schema you wish to operate on and set the C attribute. +If you wish to fix-up data upon populate, you can provide populate a +C coderef that will be passed the ResultSource and the +row as a HashRef. This is exactly like C, only called during +C instead of C. + =cut sub populate { @@ -1266,6 +1312,9 @@ sub populate { DBIx::Class::Exception->throw('you must set the ddl and connection_details params'); } + if ($params->{prepopulate_hook} && ref($params->{prepopulate_hook}) ne "CODE") { + DBIx::Class::Exception->throw('prepopulate_hook must be a coderef'); + } return 1 if $params->{no_populate}; @@ -1384,6 +1433,8 @@ sub populate { $class->restore($key, $content, $args); } } + + $params->{prepopulate_hook}->($rs->result_source, $HASH1) if (exists($params->{prepopulate_hook})); if ( $params->{use_create} ) { $rs->create( $HASH1 ); } else {