[Catalyst-commits] r13815 - in Catalyst-Model-DBIC-Schema/trunk: .
lib/Catalyst/Helper/Model/DBIC t
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Wed Dec 8 13:34:30 GMT 2010
Author: caelum
Date: 2010-12-08 13:34:30 +0000 (Wed, 08 Dec 2010)
New Revision: 13815
Modified:
Catalyst-Model-DBIC-Schema/trunk/Changes
Catalyst-Model-DBIC-Schema/trunk/lib/Catalyst/Helper/Model/DBIC/Schema.pm
Catalyst-Model-DBIC-Schema/trunk/t/05testapp.t
Log:
make use_moose detection more robust
Modified: Catalyst-Model-DBIC-Schema/trunk/Changes
===================================================================
--- Catalyst-Model-DBIC-Schema/trunk/Changes 2010-12-08 12:06:55 UTC (rev 13814)
+++ Catalyst-Model-DBIC-Schema/trunk/Changes 2010-12-08 13:34:30 UTC (rev 13815)
@@ -1,5 +1,7 @@
Revision history for Perl extension Catalyst::Model::DBIC::Schema
+ - make use_moose detection more robust
+
0.45 Wed Dec 8 12:05:58 UTC 2010
- fix bug where non-result files were picked up for Moose check
Modified: Catalyst-Model-DBIC-Schema/trunk/lib/Catalyst/Helper/Model/DBIC/Schema.pm
===================================================================
--- Catalyst-Model-DBIC-Schema/trunk/lib/Catalyst/Helper/Model/DBIC/Schema.pm 2010-12-08 12:06:55 UTC (rev 13814)
+++ Catalyst-Model-DBIC-Schema/trunk/lib/Catalyst/Helper/Model/DBIC/Schema.pm 2010-12-08 13:34:30 UTC (rev 13815)
@@ -17,6 +17,7 @@
use Scalar::Util 'looks_like_number';
use File::Find 'finddepth';
use Try::Tiny;
+use Cwd 'getcwd';
=head1 NAME
@@ -146,6 +147,7 @@
has connect_info => (is => 'rw', isa => HashRef);
has old_schema => (is => 'rw', isa => Bool, lazy_build => 1);
has is_moose_schema => (is => 'rw', isa => Bool, lazy_build => 1);
+has result_namespace => (is => 'rw', isa => Str, lazy_build => 1);
has components => (is => 'rw', isa => ArrayRef);
=head1 METHODS
@@ -389,31 +391,25 @@
sub _build_old_schema {
my $self = shift;
- my @schema_pm = split '::', $self->schema_class;
- $schema_pm[-1] .= '.pm';
- my $schema_file =
- File::Spec->catfile($self->helper->{base}, 'lib', @schema_pm);
-
- if (-f $schema_file) {
- my $schema_code = do { local (@ARGV, $/) = $schema_file; <> };
- return 1 if $schema_code =~ /->load_classes/;
- }
-
- 0;
+ return $self->result_namespace eq '' ? 1 : 0;
}
sub _build_is_moose_schema {
my $self = shift;
my @schema_parts = split '::', $self->schema_class;
- my $schema_dir =
- File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts);
+ my $result_dir = File::Spec->catfile(
+ $self->helper->{base}, 'lib', @schema_parts, $self->result_namespace
+ );
+
# assume yes for new schemas
- return 1 if not -d $schema_dir;
+ return 1 if not -d $result_dir;
my $uses_moose = 1;
+ my $cwd = getcwd;
+
try {
finddepth(sub {
return if $File::Find::name !~ /\.pm\z/;
@@ -427,12 +423,47 @@
$uses_moose = 0 if $code !~ /\nuse Moose;\n/;
die;
- }, $schema_dir);
+ }, $result_dir);
};
+ chdir $cwd;
+
return $uses_moose;
}
+sub _build_result_namespace {
+ my $self = shift;
+
+ my @schema_parts = split '::', $self->schema_class;
+ my $schema_pm =
+ File::Spec->catfile($self->helper->{base}, 'lib', @schema_parts) . '.pm';
+
+ if (not -f $schema_pm) {
+ try { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
+
+ return 'Result' if $@;
+
+ return DBIx::Class::Schema::Loader->VERSION('0.05') ? 'Result' : '';
+ }
+
+ open my $fh, '<', $schema_pm or die "Could not open $schema_pm: $!";
+ my $code = do { local $/; <$fh> };
+ close $fh;
+
+ my ($result_namespace) = $code =~ /result_namespace => '([^']+)'/;
+
+ if (not $result_namespace) {
+ if ($code =~ /->load_classes/) {
+ $result_namespace = '';
+ }
+ else {
+ $result_namespace = 'Result';
+ }
+ }
+
+ return $result_namespace;
+}
+
sub _data_struct_to_string {
my ($self, $data) = @_;
@@ -557,7 +588,7 @@
my $schema_dir = File::Spec->catfile($helper->{base}, 'lib');
- eval { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
+ try { Class::MOP::load_class('DBIx::Class::Schema::Loader') };
die "Cannot load DBIx::Class::Schema::Loader: $@" if $@;
DBIx::Class::Schema::Loader->import(
Modified: Catalyst-Model-DBIC-Schema/trunk/t/05testapp.t
===================================================================
--- Catalyst-Model-DBIC-Schema/trunk/t/05testapp.t 2010-12-08 12:06:55 UTC (rev 13814)
+++ Catalyst-Model-DBIC-Schema/trunk/t/05testapp.t 2010-12-08 13:34:30 UTC (rev 13815)
@@ -113,6 +113,43 @@
}
}
+# Test that a moose schema is not detected as a non-moose schema due to an
+# errant file.
+{
+ cleanup_schema();
+
+ system($^X, "-I$blib_dir", $creator, 'model',
+ 'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
+ 'create=static', 'dbi:SQLite:testdb.db'
+ );
+
+ mkdir "$schema_dir/.svn";
+ open my $fh, '>', "$schema_dir/.svn/foo"
+ or die "Could not open $schema_dir/.svn/foo for writing: $!";
+ print $fh "gargle\n";
+ close $fh;
+
+ mkdir "$schema_dir/Result/.svn";
+ open $fh, '>', "$schema_dir/Result/.svn/foo"
+ or die "Could not open $schema_dir/Result/.svn/foo for writing: $!";
+ print $fh "hlagh\n";
+ close $fh;
+
+ system($^X, "-I$blib_dir", $creator, 'model',
+ 'TestSchemaDSN', 'DBIC::Schema', 'TestSchemaDSN',
+ 'create=static', 'dbi:SQLite:testdb.db'
+ );
+
+ for my $file (result_files()) {
+ my $code = code_for($file);
+
+ like $code, qr/use Moose;\n/,
+ 'use_moose detection not confused by version control files';
+ like $code, qr/__PACKAGE__->meta->make_immutable;\n/,
+ 'use_moose detection not confused by version control files';
+ }
+}
+
done_testing;
sub rm_rf {
More information about the Catalyst-commits
mailing list