From: Ben Tilly Date: Wed, 3 Aug 2011 23:24:25 +0000 (-0700) Subject: adds the ability to filter generated code X-Git-Tag: 0.07011~57 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-Schema-Loader.git;a=commitdiff_plain;h=dde95f2c6188ae6182da0f32320e7f1c9da0a90a adds the ability to filter generated code Adds the filter_generated_text option which takes a coderef for filtering the generated code. The coderef takes the class and code as parameters. --- diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index 49c0359..2a67684 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -77,6 +77,8 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/ class_to_table uniq_to_primary quiet + + filter_generated_text /); @@ -682,6 +684,21 @@ Automatically promotes the largest unique constraints with non-nullable columns on tables to primary keys, assuming there is only one largest unique constraint. +=head2 filter_generated_text + +An optional hook that lets you filter the generated text for various classes through +a function that change it in any way that you want. The function will receive the class +and text, and returns the new text to use instead. For instance you could add +custom comment, run C, or do anything else that you want. + +If this exists but fails to return text matching C, no file will be generated. + + filter_generated_base => sub { + my ($class, $text) = @_; + ... + return $new_text; + } + =head1 METHODS None of these methods are intended for direct invocation by regular @@ -1655,7 +1672,15 @@ sub _write_classfile { $text .= qq|$_\n| for @{$self->{_dump_storage}->{$class} || []}; - # Check and see if the dump is infact differnt + if ($self->{filter_generated_text}) { + $text = $self->{filter_generated_text}->($class, $text); + if (not $text or not $text =~ /package/) { + warn("$class skipped due to filter") if $self->debug; + return; + } + } + + # Check and see if the dump is in fact different my $compare_to; if ($old_md5) { diff --git a/t/27filter_generated.t b/t/27filter_generated.t new file mode 100644 index 0000000..22eb772 --- /dev/null +++ b/t/27filter_generated.t @@ -0,0 +1,46 @@ +use strict; +use File::Slurp qw(slurp); +use File::Path; +use Test::More tests => 4; +use Test::Exception; +use lib qw(t/lib); +use make_dbictest_db; +use dbixcsl_test_dir qw/$tdir/; + +use DBIx::Class::Schema::Loader; + +my $dump_path = "$tdir/dump"; + +my %original_class_data; + +{ + package DBICTest::Schema::1; + use base qw/ DBIx::Class::Schema::Loader /; + __PACKAGE__->loader_options( + dump_directory => $dump_path, + filter_generated_text => sub { + my ($class, $text) = @_; + $original_class_data{$class} = $text; + if ($class =~ /::1$/) { + $text = "No Gotcha!"; + } + else { + $text .= q{"Kilroy was here";}; + } + return $text; + }, + ); +} + +DBICTest::Schema::1->connect($make_dbictest_db::dsn); + +my $foo = slurp("$dump_path/DBICTest/Schema/1/Result/Foo.pm"); +ok(! -e "$dump_path/DBICTest/Schema/1.pm", + "No package means no file written"); +ok($original_class_data{"DBICTest::Schema::1"}, + "Even though we processed the missing class"); +like($foo, qr/# Created by .* THE FIRST PART/s, + "We get the whole autogenerated text"); +like($foo, qr/Kilroy was here/, "Can insert text"); + +END { rmtree($dump_path, 1, 1); }