X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper%2FModel%2FDBIC%2FSchema.pm;h=0a5b49073e0b6de8a57e584b5597512c191f7407;hb=bf4bdab65638c0d4aad4e4d21d5d8e8149dc0329;hp=eac33c46b4ea7ee27771b9d4d08e8370a69ffc20;hpb=7dfd616a3bf5e7552ef2544eec096167c6e4308b;p=catagits%2FCatalyst-Model-DBIC-Schema.git diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm index eac33c4..0a5b490 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -4,7 +4,7 @@ use namespace::autoclean; use Moose; no warnings 'uninitialized'; -our $VERSION = '0.27'; +our $VERSION = '0.45'; $VERSION = eval $VERSION; use Carp; @@ -15,6 +15,8 @@ use MooseX::Types::Moose qw/Str HashRef Bool ArrayRef/; use Catalyst::Model::DBIC::Schema::Types 'CreateOption'; use List::MoreUtils 'firstidx'; use Scalar::Util 'looks_like_number'; +use File::Find 'finddepth'; +use Try::Tiny; =head1 NAME @@ -107,6 +109,14 @@ Same, but with extra Schema::Loader args (separate multiple values by commas): exclude='^(wibble|wobble)$' moniker_map='{ foo => "FOO" }' \ dbi:Pg:dbname=foodb myuname mypass +Coderefs are also supported: + + script/myapp_create.pl model CatalystModelName DBIC::Schema \ + MyApp::SchemaClass create=static \ + inflect_singular='sub { $_[0] =~ /\A(.+?)(_id)?\z/; $1 }' \ + moniker_map='sub { join(q{}, map ucfirst, split(/[\W_]+/, lc $_[0])); }' \ + dbi:mysql:foodb myuname mypass + See L for a list of options Create a dynamic DBIx::Class::Schema::Loader-based Schema, @@ -135,6 +145,7 @@ has schema_class => (is => 'ro', isa => Str, required => 1); has loader_args => (is => 'rw', isa => HashRef); 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 components => (is => 'rw', isa => ArrayRef); =head1 METHODS @@ -188,21 +199,21 @@ sub BUILD { $self->_parse_loader_args(\@args); $helper->{loader_args} = $self->_build_helper_loader_args; + } + } - my $dbi_dsn_part; - if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) { - die + my $dbi_dsn_part; + if (first { ($dbi_dsn_part) = /^(dbi):/i } @args) { + die qq{DSN must start with 'dbi:' not '$dbi_dsn_part' (case matters!)} - if $dbi_dsn_part ne 'dbi'; + if $dbi_dsn_part ne 'dbi'; - $helper->{setup_connect_info} = 1; + $helper->{setup_connect_info} = 1; - $helper->{connect_info} = - $self->_build_helper_connect_info(\@args); + $helper->{connect_info} = + $self->_build_helper_connect_info(\@args); - $self->_parse_connect_info(\@args); - } - } + $self->_parse_connect_info(\@args); } $helper->{generator} = ref $self; @@ -241,8 +252,10 @@ sub _parse_loader_args { if $@; } - my @components = - $self->_build_loader_components(delete $loader_args{components}); + my @components = $self->_build_loader_components( + delete $loader_args{components}, + $loader_args{use_namespaces}, + ); $self->components(\@components); @@ -255,13 +268,15 @@ sub _parse_loader_args { %result = ( relationships => 1, - (%loader_args ? %loader_args : ()), + use_moose => $self->is_moose_schema ? 1 : 0, + col_collision_map => 'column_%s', (!$self->old_schema ? ( use_namespaces => 1 ) : ()), (@components ? ( components => \@components - ) : ()) + ) : ()), + (%loader_args ? %loader_args : ()), ); $self->loader_args(\%result); @@ -308,9 +323,10 @@ sub _build_helper_loader_args { } sub _build_loader_components { - my ($self, $components) = @_; + my ($self, $components, $use_namespaces) = @_; - my @components = $self->old_schema ? () : ('InflateColumn::DateTime'); + my @components = $self->old_schema && (not $use_namespaces) ? () + : ('InflateColumn::DateTime'); if ($components) { $components = [ $components ] if !ref $components; @@ -386,6 +402,37 @@ sub _build_old_schema { 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); + + # assume yes for new schemas + return 1 if not -d $schema_dir; + + my $uses_moose = 1; + + try { + finddepth(sub { + return if $File::Find::name !~ /\.pm\z/; + + open my $fh, '<', $File::Find::name + or die "Could not open $File::Find::name: $!"; + + my $code = do { local $/; <$fh> }; + close $fh; + + $uses_moose = 0 if $code !~ /\nuse Moose;\n/; + + die; + }, $schema_dir); + }; + + return $uses_moose; +} + sub _data_struct_to_string { my ($self, $data) = @_; @@ -455,7 +502,7 @@ sub _parse_connect_info { sub _is_struct { my ($self, $val) = @_; - return $val =~ /^\s*[[{]/; + return $val =~ /^\s*(?:sub|[[{])/; } sub _quote { @@ -477,7 +524,7 @@ sub _eval { return $code if looks_like_number $code; - return $code if $code =~ m{^[\w;:/]*\z}; + return $code if not $self->_is_struct($code); return eval "{no strict; $code}"; } @@ -568,11 +615,12 @@ L, L =head1 AUTHOR -Brandon L Black, C +See L and +L. -Contributors: +=head1 COPYRIGHT -Rafael Kitover, C +See L. =head1 LICENSE