From: Rafael Kitover Date: Tue, 26 May 2009 01:04:34 +0000 (+0000) Subject: 2 char quote_char support and s/roles/traits/ X-Git-Tag: v0.26~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Model-DBIC-Schema.git;a=commitdiff_plain;h=c34bcab69a93411aaca477595289fa51e6fb8b11 2 char quote_char support and s/roles/traits/ --- diff --git a/lib/Catalyst/Helper/Model/DBIC/Schema.pm b/lib/Catalyst/Helper/Model/DBIC/Schema.pm index 7564960..a479fdc 100644 --- a/lib/Catalyst/Helper/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Helper/Model/DBIC/Schema.pm @@ -24,7 +24,7 @@ Catalyst::Helper::Model::DBIC::Schema - Helper for DBIC Schema Models =head1 SYNOPSIS script/create.pl model CatalystModelName DBIC::Schema MyApp::SchemaClass \ - [ create=dynamic | create=static ] [ roles=role1,role2... ] \ + [ create=dynamic | create=static ] [ traits=trait1,trait2... ] \ [ Schema::Loader opts ] [ dsn user pass ] \ [ other connect_info args ] @@ -56,7 +56,7 @@ L at runtime, and will not automatically adapt itself to changes in your database structure. You can edit the generated classes by hand to refine them. -C is the list of roles to apply to the model, see +C is the list of traits to apply to the model, see L for details. C are described in L below. @@ -88,6 +88,10 @@ user and pass can be omitted for sqlite, since they are always empty AutoCommit=1 cursor_class=DBIx::Class::Cursor::Cached \ on_connect_do='["select 1", "select 2"]' quote_char='"' +If using a 2 character quote_char: + + script/myapp_create.pl ... quote_char='[]' + B In C the above example would be: @@ -127,7 +131,7 @@ in your app config, or [not recommended] in the schema itself). has helper => (is => 'ro', isa => 'Catalyst::Helper', required => 1); has create => (is => 'rw', isa => CreateOption); has args => (is => 'ro', isa => ArrayRef); -has roles => (is => 'rw', isa => ArrayRef); +has traits => (is => 'rw', isa => ArrayRef); has schema_class => (is => 'ro', isa => Str, required => 1); has loader_args => (is => 'rw', isa => HashRef); has connect_info => (is => 'rw', isa => HashRef); @@ -164,17 +168,17 @@ sub BUILD { @args = $self->_cleanup_args(\@args); - my ($roles_idx, $roles); - if (($roles_idx = firstidx { ($roles) = /^roles=(\S*)\z/ } @args) != -1) { - my @roles = split /,/ => $roles; + my ($traits_idx, $traits); + if (($traits_idx = firstidx { ($traits) = /^traits=(\S*)\z/ } @args) != -1) { + my @traits = split /,/ => $traits; - $self->roles(\@roles); + $self->traits(\@traits); - $helper->{roles} = '[' - .(join ',' => map { qq{'$_'} } ($self->roles->flatten)) + $helper->{traits} = '[' + .(join ',' => map { qq{'$_'} } ($self->traits->flatten)) .']'; - splice @args, $roles_idx, 1, (); + splice @args, $traits_idx, 1, (); } if ($args[0] && $args[0] =~ /^create=(\S*)\z/) { @@ -339,7 +343,7 @@ sub _build_helper_connect_info { if (ref $val) { $val = $self->_data_struct_to_string($val); } else { - $val = 'q{'.$val.'}'; + $val = $self->_quote($val); } $helper_connect_info{$key} = $val; @@ -350,7 +354,13 @@ sub _build_helper_connect_info { my ($key, $val) = split /=/, $_, 2; - $helper_connect_info{$key} = $self->_quote_unless_struct($val); + if ($key eq 'quote_char') { + $helper_connect_info{$key} = length($val) == 1 ? + $self->_quote($val) : + $self->_data_struct_to_string([split //, $val]); + } else { + $helper_connect_info{$key} = $self->_quote_unless_struct($val); + } } \%helper_connect_info @@ -421,7 +431,9 @@ sub _parse_connect_info { my ($key, $val) = split /=/, $_, 2; - if ($key =~ /^(?:quote_char|name_sep|limit_dialect)\z/) { + if ($key eq 'quote_char') { + $connect_info{$key} = length($val) == 1 ? $val : [split //, $val]; + } elsif ($key =~ /^(?:name_sep|limit_dialect)\z/) { $connect_info{$key} = $val; } else { $connect_info{$key} = $self->_eval($val); @@ -442,12 +454,18 @@ sub _is_struct { return $val =~ /^\s*[[{]/; } +sub _quote { + my ($self, $val) = @_; + + return 'q{'.$val.'}'; +} + sub _quote_unless_struct { my ($self, $val) = @_; - $val = 'q{'.$val.'}' if not $self->_is_struct($val); + $val = $self->_quote($val) if not $self->_is_struct($val); - $val; + return $val; } sub _eval { @@ -615,7 +633,7 @@ use base 'Catalyst::Model::DBIC::Schema'; __PACKAGE__->config( schema_class => '[% schema_class %]', - [% IF roles %]roles => [% roles %],[% END %] + [% IF traits %]traits => [% traits %],[% END %] [% IF setup_connect_info %]connect_info => { [%- FOREACH key = connect_info.keys %] [% key %] => [% connect_info.${key} %], diff --git a/lib/Catalyst/Model/DBIC/Schema.pm b/lib/Catalyst/Model/DBIC/Schema.pm index c147ac6..cb0ffb0 100644 --- a/lib/Catalyst/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Model/DBIC/Schema.pm @@ -240,7 +240,7 @@ Or using L: schema_class MyApp::Schema::FilmDB - roles Caching + traits Caching dsn dbi:Pg:dbname=mypgdb user postgres @@ -292,36 +292,36 @@ supported: } ] -=head2 roles +=head2 traits -Array of Roles to apply at BUILD time. Roles are relative to the -C< then C<> +Array of Traits to apply at BUILD time. Traits are relative to the +C< then C<> namespaces, unless prefixed with C<+> in which case they are taken to be a fully qualified name. E.g.: - roles Caching - roles +MyApp::DB::Role::Foo + traits Caching + traits +MyApp::DB::Trait::Foo This is done using L. A new instance is created at application time, so any consumed required attributes, coercions and modifiers will work. -Roles are applied before setup, schema and connection are set. +Traits are applied before setup, schema and connection are set. -C will be an anon class if any roles are applied. +C will be an anon class if any traits are applied. You cannot modify C or C, modify C instead. L and L can also be modified. -Roles that come with the distribution: +Traits that come with the distribution: =over 4 -=item L +=item L -=item L +=item L =back @@ -381,20 +381,20 @@ Used often for debugging and controlling transactions. =cut -has 'schema' => (is => 'rw', isa => 'DBIx::Class::Schema'); +has schema => (is => 'rw', isa => 'DBIx::Class::Schema'); -has 'schema_class' => ( +has schema_class => ( is => 'ro', isa => SchemaClass, coerce => 1, required => 1 ); -has 'storage_type' => (is => 'rw', isa => Str); +has storage_type => (is => 'rw', isa => Str); -has 'connect_info' => (is => 'ro', isa => ConnectInfo, coerce => 1); +has connect_info => (is => 'ro', isa => ConnectInfo, coerce => 1); -has 'model_name' => (is => 'ro', isa => Str, default => sub { +has model_name => (is => 'ro', isa => Str, default => sub { my $self = shift; my $class = ref $self; @@ -403,9 +403,9 @@ has 'model_name' => (is => 'ro', isa => Str, default => sub { $model_name }); -has 'roles' => (is => 'ro', isa => ArrayRef|Str); +has traits => (is => 'ro', isa => ArrayRef|Str); -has '_default_cursor_class' => ( +has _default_cursor_class => ( is => 'ro', isa => CursorClass, default => 'DBIx::Class::Storage::DBI::Cursor', @@ -435,9 +435,9 @@ sub BUILD { . " ".$self->connect_info->{cursor_class}.": $@"; } - $self->_plugin_ns('Role'); + $self->_plugin_ns('Trait'); - $self->load_plugins($self->roles->flatten) if $self->roles; + $self->load_plugins($self->traits->flatten) if $self->traits; $self->setup; @@ -546,10 +546,10 @@ L, L, L, L, L -Roles: +Traits: -L, -L +L, +L =head1 AUTHOR diff --git a/lib/Catalyst/Model/DBIC/Schema/Role/Caching.pm b/lib/Catalyst/Model/DBIC/Schema/Trait/Caching.pm similarity index 92% rename from lib/Catalyst/Model/DBIC/Schema/Role/Caching.pm rename to lib/Catalyst/Model/DBIC/Schema/Trait/Caching.pm index 73826e5..de74007 100644 --- a/lib/Catalyst/Model/DBIC/Schema/Role/Caching.pm +++ b/lib/Catalyst/Model/DBIC/Schema/Trait/Caching.pm @@ -1,4 +1,4 @@ -package Catalyst::Model::DBIC::Schema::Role::Caching; +package Catalyst::Model::DBIC::Schema::Trait::Caching; use Moose::Role; use Carp::Clan '^Catalyst::Model::DBIC::Schema'; @@ -9,13 +9,13 @@ use namespace::clean -except => 'meta'; =head1 NAME -Catalyst::Model::DBIC::Schema::Role::Caching - Query caching support for +Catalyst::Model::DBIC::Schema::Trait::Caching - Query caching support for Catalyst::Model::DBIC::Schema =head1 SYNOPSIS __PACKAGE__->config({ - roles => ['Caching'], + traits => ['Caching'], connect_info => ['dbi:mysql:db', 'user', 'pass'], }); @@ -52,7 +52,7 @@ Turn caching on or off, you can use: =cut -has 'caching' => (is => 'rw', isa => Int, default => 1); +has caching => (is => 'rw', isa => Int, default => 1); after setup => sub { my $self = shift; diff --git a/lib/Catalyst/Model/DBIC/Schema/Role/Replicated.pm b/lib/Catalyst/Model/DBIC/Schema/Trait/Replicated.pm similarity index 90% rename from lib/Catalyst/Model/DBIC/Schema/Role/Replicated.pm rename to lib/Catalyst/Model/DBIC/Schema/Trait/Replicated.pm index ed727e3..b73246c 100644 --- a/lib/Catalyst/Model/DBIC/Schema/Role/Replicated.pm +++ b/lib/Catalyst/Model/DBIC/Schema/Trait/Replicated.pm @@ -1,4 +1,4 @@ -package Catalyst::Model::DBIC::Schema::Role::Replicated; +package Catalyst::Model::DBIC::Schema::Trait::Replicated; use Moose::Role; use Moose::Autobox; @@ -10,13 +10,13 @@ use namespace::clean -except => 'meta'; =head1 NAME -Catalyst::Model::DBIC::Schema::Role::Replicated - Replicated storage support for +Catalyst::Model::DBIC::Schema::Trait::Replicated - Replicated storage support for L =head1 SYNOPSiS __PACKAGE__->config({ - roles => ['Replicated'] + traits => ['Replicated'] connect_info => ['dbi:mysql:master', 'user', 'pass'], replicants => [ @@ -45,12 +45,12 @@ you do from replicants. Set to C<0> to turn off reads from master. =head1 NOTE ON L VERSIONS PRIOR TO 0.08103 -This role will work, however, any C<::Storage::Replicated> options in +This trait will work, however, any C<::Storage::Replicated> options in L will be ignored, master connect_info will not be merged to replicants, and L will be used instead, with all your reads going only to one of your replicants. You'll also get some -warnings. The C role will also not work. +warnings. The C trait will also not work. Please upgrade. diff --git a/t/05testapp.t b/t/05testapp.t index 5d71ce7..3c59f2a 100644 --- a/t/05testapp.t +++ b/t/05testapp.t @@ -12,8 +12,8 @@ plan skip_all => 'Enable this optional test with $ENV{C_M_DBIC_SCHEMA_TESTAPP}' my $test_params = [ [ 'TestSchema', 'DBIC::Schema', '' ], [ 'TestSchemaDSN', 'DBIC::Schema', q{fakedsn fakeuser fakepass "{ AutoCommit => 1 }"} ], - [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static roles=Caching moniker_map="{ roles => \"ROLE\" }" constraint="^users\z" dbi:SQLite:testdb.db "" "" on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ], - [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static roles=Caching moniker_map="{ roles => \"ROLE\" }" dbi:SQLite:testdb.db on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ], + [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static traits=Caching moniker_map="{ roles => \"ROLE\" }" constraint="^users\z" dbi:SQLite:testdb.db "" "" on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ], + [ 'TestSchemaDSN', 'DBIC::Schema', q{create=static traits=Caching moniker_map="{ roles => \"ROLE\" }" dbi:SQLite:testdb.db on_connect_do="[\"select 1\", \"select 2\"]" quote_char="\""} ], ]; plan tests => (2 * @$test_params + 2); diff --git a/t/08helper.t b/t/08helper.t index c6e1e00..08951da 100644 --- a/t/08helper.t +++ b/t/08helper.t @@ -4,7 +4,7 @@ use warnings; use FindBin '$Bin'; use lib "$Bin/lib"; -use Test::More tests => 38; +use Test::More tests => 40; use Test::Exception; use Catalyst::Helper::Model::DBIC::Schema; use Catalyst::Helper; @@ -24,13 +24,13 @@ my $i; $i = instance(schema_class => 'ASchemaClass'); is $i->old_schema, 1, '->load_classes detected correctly'; -$i = instance(args => ['roles=Caching']); -is_deeply $i->roles, ['Caching'], 'one role'; -is $i->helper->{roles}, "['Caching']", 'one role as string'; +$i = instance(args => ['traits=Caching']); +is_deeply $i->traits, ['Caching'], 'one trait'; +is $i->helper->{traits}, "['Caching']", 'one trait as string'; -$i = instance(args => ['roles=Caching,Replicated']); -is_deeply $i->roles, ['Caching', 'Replicated'], 'two roles'; -is $i->helper->{roles}, "['Caching','Replicated']", 'two roles as string'; +$i = instance(args => ['traits=Caching,Replicated']); +is_deeply $i->traits, ['Caching', 'Replicated'], 'two traits'; +is $i->helper->{traits}, "['Caching','Replicated']", 'two traits as string'; $i = instance(args => [$static]); is $i->create, 'static', 'create=static'; @@ -131,6 +131,15 @@ is $i->connect_info->{user}, 'user', 'user'; is $i->connect_info->{password}, 'pass', 'password'; $i = instance(args => [ + $static, $pg, 'user', 'pass', 'quote_char=[]', $name_sep +]); + +is_deeply $i->connect_info->{quote_char}, ['[', ']'], + '2 character quote_char'; +is $i->helper->{connect_info}{quote_char}, '["[","]"]', + '2 character quote_char as string'; + +$i = instance(args => [ $static, 'components=TimeStamp', $sqlite, $on_connect_do, $quote_char, $name_sep, '{ auto_savepoint => 1, AutoCommit => 0 }' ]);