From: Rafael Kitover Date: Sun, 27 Dec 2009 06:47:56 +0000 (+0000) Subject: move schema proxying into a trait X-Git-Tag: v0.34~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Model-DBIC-Schema.git;a=commitdiff_plain;h=d816d7bf6502a6c67912f1736705c6b55a43a518 move schema proxying into a trait --- diff --git a/Makefile.PL b/Makefile.PL index e7e5c08..eba03a1 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -1,11 +1,12 @@ use inc::Module::Install 0.91; name 'Catalyst-Model-DBIC-Schema'; +perl_version 5.008001; all_from 'lib/Catalyst/Model/DBIC/Schema.pm'; requires 'DBIx::Class' => '0.08114'; requires 'Catalyst::Runtime' => '5.80005'; -requires 'CatalystX::Component::Traits' => '0.10'; +requires 'CatalystX::Component::Traits' => '0.14'; requires 'Moose'; requires 'MooseX::Types'; diff --git a/lib/Catalyst/Model/DBIC/Schema.pm b/lib/Catalyst/Model/DBIC/Schema.pm index 2406196..45392e9 100644 --- a/lib/Catalyst/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Model/DBIC/Schema.pm @@ -1,6 +1,5 @@ package Catalyst::Model::DBIC::Schema; -use 5.008_001; use Moose; use mro 'c3'; extends 'Catalyst::Model'; @@ -15,7 +14,7 @@ use Data::Dumper; use DBIx::Class (); use Catalyst::Model::DBIC::Schema::Types - qw/ConnectInfo LoadedClass SchemaClass/; + qw/ConnectInfo LoadedClass SchemaClass Schema/; use MooseX::Types::Moose qw/ArrayRef Str ClassName Undef/; @@ -279,9 +278,10 @@ supported: Array of Traits to apply to the instance. Traits are Ls. -They are relative to the C<< MyApp::TraitFor::Model::DBIC::Schema:: >>, then the C<< -Catalyst::TraitFor::Model::DBIC::Schema:: >> namespaces, unless prefixed with C<+> -in which case they are taken to be a fully qualified name. E.g.: +They are relative to the C<< MyApp::TraitFor::Model::DBIC::Schema:: >>, then +the C<< Catalyst::TraitFor::Model::DBIC::Schema:: >> namespaces, unless +prefixed with C<+> in which case they are taken to be a fully qualified name. +E.g.: traits Caching traits +MyApp::TraitFor::Model::Foo @@ -289,6 +289,10 @@ in which case they are taken to be a fully qualified name. E.g.: A new instance is created at application time, so any consumed required attributes, coercions and modifiers will work. +By default, the L trait +is loaded. It can be disabled by specifying C<-SchemaProxy> in traits. See +L. + Traits are applied at L time using L. @@ -306,6 +310,8 @@ Traits that come with the distribution: =item L +=item L + =back =head2 storage_type @@ -404,6 +410,14 @@ Shortcut for ->schema->class Shortcut for ->schema->resultset +=head2 txn_do + +Shortcut for ->schema->txn_do + +=head2 txn_scope_guard + +Shortcut for ->schema->txn_scope_guard + =head2 storage Provides an accessor for the connected schema's storage object. @@ -411,6 +425,10 @@ Used often for debugging and controlling transactions. =cut +has '+_trait_merge' => (default => 1); + +__PACKAGE__->config->{traits} = ['SchemaProxy']; + has schema_class => ( is => 'ro', isa => SchemaClass, @@ -436,6 +454,8 @@ has _default_cursor_class => ( coerce => 1 ); +has schema => (is => 'rw', isa => Schema); + sub BUILD { my ($self, $args) = @_; my $class = $self->_original_class_name; @@ -459,41 +479,42 @@ sub BUILD { . " ".$self->connect_info->{cursor_class}.": $@"; } - $self->setup; + $self->setup($args); - $self->composed_schema($schema_class->compose_namespace($class)); + my $is_installed = defined $self->composed_schema; - my $was_mutable = $self->meta->is_mutable; - - $self->meta->make_mutable; - $self->meta->add_attribute('schema', - is => 'rw', - isa => 'DBIx::Class::Schema', - handles => $self->_delegates - ); - $self->meta->make_immutable unless $was_mutable; + $self->composed_schema($schema_class->compose_namespace($class)) + unless $is_installed; $self->schema($self->composed_schema->clone); - $self->_pass_options_to_schema($args); - $self->schema->storage_type($self->storage_type) if $self->storage_type; $self->schema->connection($self->connect_info); - $self->_install_rs_models; + $self->_install_rs_models unless $is_installed; } sub clone { shift->composed_schema->clone(@_); } sub connect { shift->composed_schema->connect(@_); } +# proxy methods, for when the SchemaProxy trait isn't loaded + +sub resultset { shift->schema->resultset(@_); } + +sub txn_do { shift->schema->txn_do(@_); } + +sub txn_scope_guard { shift->schema->txn_scope_guard(@_); } + =head2 setup Called at C time before configuration, but after L is set. To do something after configuuration use C<< after BUILD => >>. +Receives a hashref of args passed to C. + =cut sub setup { 1 } @@ -566,55 +587,6 @@ sub _build_model_name { return $model_name; } -sub _delegates { - my $self = shift; - - my $schema_meta = Class::MOP::Class->initialize($self->schema_class); - my @schema_methods = $schema_meta->get_all_method_names; - -# combine with any already added by other schemas - my @handles = eval { - @{ $self->meta->find_attribute_by_name('schema')->handles } - }; - -# now kill the attribute, otherwise add_attribute in BUILD will not do the right -# thing (it clears the handles for some reason.) May be a Moose bug. - eval { $self->meta->remove_attribute('schema') }; - - my %schema_methods; - @schema_methods{ @schema_methods, @handles } = (); - @schema_methods = keys %schema_methods; - - my @my_methods = $self->meta->get_all_method_names; - my %my_methods; - @my_methods{@my_methods} = (); - - my @delegates; - for my $method (@schema_methods) { - push @delegates, $method unless exists $my_methods{$method}; - } - - return \@delegates; -} - -sub _pass_options_to_schema { - my ($self, $args) = @_; - - my @attributes = map { - $_->init_arg || () - } $self->meta->get_all_attributes; - - my %attributes; - @attributes{@attributes} = (); - - for my $opt (keys %$args) { - if (not exists $attributes{$opt}) { - next unless $self->schema->can($opt); - $self->schema->$opt($self->{$opt}); - } - } -} - __PACKAGE__->meta->make_immutable; =head1 ENVIRONMENT @@ -676,6 +648,7 @@ Traits: L, L, +L, L =head1 AUTHOR diff --git a/lib/Catalyst/Model/DBIC/Schema/Types.pm b/lib/Catalyst/Model/DBIC/Schema/Types.pm index 0bf8c3b..a7ec807 100644 --- a/lib/Catalyst/Model/DBIC/Schema/Types.pm +++ b/lib/Catalyst/Model/DBIC/Schema/Types.pm @@ -3,6 +3,7 @@ package # hide from PAUSE use MooseX::Types -declare => [qw/ ConnectInfo ConnectInfos Replicants SchemaClass LoadedClass CreateOption + Schema /]; use Carp::Clan '^Catalyst::Model::DBIC::Schema'; @@ -25,6 +26,8 @@ subtype SchemaClass, SchemaClass->coercion(LoadedClass->coercion); +class_type Schema, { class => 'DBIx::Class::Schema' }; + subtype ConnectInfo, as HashRef, where { exists $_->{dsn} || exists $_->{dbh_maker} }, diff --git a/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm b/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm new file mode 100644 index 0000000..92bbc90 --- /dev/null +++ b/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm @@ -0,0 +1,112 @@ +package Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy; + +use namespace::autoclean; +use Moose::Role; +use Carp::Clan '^Catalyst::Model::DBIC::Schema'; + +=head1 NAME + +Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy - Proxy Schema Methods and +Options from Model + +=head1 DESCRIPTION + +Allows you to call L methods directly on the Model +instance, and passes config options to the L attributes at +C time. + +This trait is loaded by default, but can be disabled by adding C<-SchemaProxy> +to the L array. + +=cut + +after setup => sub { + my ($self, $args) = @_; + + my $was_mutable = $self->meta->is_mutable; + + $self->meta->make_mutable; + $self->meta->add_attribute('schema', + is => 'rw', + isa => 'DBIx::Class::Schema', + handles => $self->_delegates # this removes the attribute too + ); + $self->meta->make_immutable unless $was_mutable; +}; + +after BUILD => sub { + my ($self, $args) = @_; + + $self->_pass_options_to_schema($args); +}; + +sub _delegates { + my $self = shift; + + my $schema_meta = Class::MOP::Class->initialize($self->schema_class); + my @schema_methods = $schema_meta->get_all_method_names; + +# combine with any already added by other schemas + my @handles = eval { + @{ $self->meta->find_attribute_by_name('schema')->handles } + }; + +# now kill the attribute, otherwise add_attribute in BUILD will not do the right +# thing (it clears the handles for some reason.) May be a Moose bug. + eval { $self->meta->remove_attribute('schema') }; + + my %schema_methods; + @schema_methods{ @schema_methods, @handles } = (); + @schema_methods = keys %schema_methods; + + my @my_methods = $self->meta->get_all_method_names; + my %my_methods; + @my_methods{@my_methods} = (); + + my @delegates; + for my $method (@schema_methods) { + push @delegates, $method unless exists $my_methods{$method}; + } + + return \@delegates; +} + +sub _pass_options_to_schema { + my ($self, $args) = @_; + + my @attributes = map { + $_->init_arg || () + } $self->meta->get_all_attributes; + + my %attributes; + @attributes{@attributes} = (); + + for my $opt (keys %$args) { + if (not exists $attributes{$opt}) { + next unless $self->schema->can($opt); + $self->schema->$opt($self->{$opt}); + } + } +} + +=head1 SEE ALSO + +L, L + +=head1 AUTHOR + +See L and +L. + +=head1 COPYRIGHT + +See L. + +=head1 LICENSE + +This program is free software, you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut + +1; diff --git a/t/09schema_options.t b/t/09schema_options.t index b38f0e3..2f95fc7 100644 --- a/t/09schema_options.t +++ b/t/09schema_options.t @@ -20,9 +20,11 @@ lives_ok { $m->a_schema_option('pass the crack pipe') } 'delegate called'; is $m->schema->a_schema_option, 'pass the crack pipe', 'delegation works'; sub instance { - Catalyst::Model::DBIC::Schema->new({ + Catalyst::Model::DBIC::Schema->COMPONENT('MyApp', { schema_class => 'ASchemaClass', connect_info => ['dbi:SQLite:foo.db', '', ''], @_, }) } + +{ package MyApp; use Catalyst; }