[Catalyst-commits] r12483 - in
Catalyst-Model-DBIC-Schema/branches/upgrade: .
lib/Catalyst/Helper/Model/DBIC lib/Catalyst/Model/DBIC
lib/Catalyst/Model/DBIC/Schema
lib/Catalyst/TraitFor/Model/DBIC/Schema t
caelum at dev.catalyst.perl.org
caelum at dev.catalyst.perl.org
Sun Dec 27 08:57:51 GMT 2009
Author: caelum
Date: 2009-12-27 08:57:51 +0000 (Sun, 27 Dec 2009)
New Revision: 12483
Added:
Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm
Modified:
Catalyst-Model-DBIC-Schema/branches/upgrade/
Catalyst-Model-DBIC-Schema/branches/upgrade/Changes
Catalyst-Model-DBIC-Schema/branches/upgrade/Makefile.PL
Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Helper/Model/DBIC/Schema.pm
Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema.pm
Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema/Types.pm
Catalyst-Model-DBIC-Schema/branches/upgrade/t/09schema_options.t
Log:
r22300 at hlagh (orig r12480): caelum | 2009-12-27 01:47:56 -0500
move schema proxying into a trait
r22301 at hlagh (orig r12481): caelum | 2009-12-27 03:11:49 -0500
deprecate schema proxying, release
r22302 at hlagh (orig r12482): caelum | 2009-12-27 03:51:28 -0500
update Changes
Property changes on: Catalyst-Model-DBIC-Schema/branches/upgrade
___________________________________________________________________
Name: svk:merge
+ 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Model-DBIC-Schema/trunk:12482
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Model-DBIC-Schema:10999
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/Changes
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/Changes 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/Changes 2009-12-27 08:57:51 UTC (rev 12483)
@@ -1,5 +1,8 @@
Revision history for Perl extension Catalyst::Model::DBIC::Schema
+0.34 Sun Dec 27 03:49:34 EST 2009
+ - move schema proxying into SchemaProxy trait which is off by default
+
0.33 Sat Dec 26 08:04:49 EST 2009
- fix test failure in 05testapp.t when env var not set (RT#53101)
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/Makefile.PL
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/Makefile.PL 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/Makefile.PL 2009-12-27 08:57:51 UTC (rev 12483)
@@ -1,11 +1,22 @@
use inc::Module::Install 0.91;
name 'Catalyst-Model-DBIC-Schema';
+perl_version 5.008001;
all_from 'lib/Catalyst/Model/DBIC/Schema.pm';
+warn <<EOF;
+****************************** !!! WARNING !!! *******************************
+* *
+* Automatic proxying to the Schema class has been DEPRECATED. *
+* *
+* See "METHOD PROXYING" in perldoc Catalyst::Model::DBIC::Schema. *
+* *
+*******************************************************************************
+EOF
+
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';
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Helper/Model/DBIC/Schema.pm
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Helper/Model/DBIC/Schema.pm 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Helper/Model/DBIC/Schema.pm 2009-12-27 08:57:51 UTC (rev 12483)
@@ -4,7 +4,7 @@
use Moose;
no warnings 'uninitialized';
-our $VERSION = '0.33';
+our $VERSION = '0.34';
$VERSION = eval $VERSION;
use Carp;
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema/Types.pm
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema/Types.pm 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema/Types.pm 2009-12-27 08:57:51 UTC (rev 12483)
@@ -3,6 +3,7 @@
use MooseX::Types -declare => [qw/
ConnectInfo ConnectInfos Replicants SchemaClass LoadedClass CreateOption
+ Schema
/];
use Carp::Clan '^Catalyst::Model::DBIC::Schema';
@@ -25,6 +26,8 @@
SchemaClass->coercion(LoadedClass->coercion);
+class_type Schema, { class => 'DBIx::Class::Schema' };
+
subtype ConnectInfo,
as HashRef,
where { exists $_->{dsn} || exists $_->{dbh_maker} },
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema.pm
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema.pm 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/Model/DBIC/Schema.pm 2009-12-27 08:57:51 UTC (rev 12483)
@@ -1,12 +1,11 @@
package Catalyst::Model::DBIC::Schema;
-use 5.008_001;
use Moose;
use mro 'c3';
extends 'Catalyst::Model';
with 'CatalystX::Component::Traits';
-our $VERSION = '0.33';
+our $VERSION = '0.34';
$VERSION = eval $VERSION;
use namespace::autoclean;
@@ -15,7 +14,7 @@
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/;
@@ -61,11 +60,6 @@
my $db_model = $c->model('FilmDB'); # a Catalyst::Model
my $dbic = $c->model('FilmDB')->schema; # the actual DBIC object
-The Model proxies to the C<Schema> instance so you can do:
-
- my $rs = $db_model->resultset('Actor'); # ... or ...
- my $rs = $dbic ->resultset('Actor'); # same!
-
There is also a shortcut, which returns a L<DBIx::Class::ResultSet> directly,
instead of a L<Catalyst::Model>:
@@ -125,7 +119,7 @@
to your DBIC Schema. It should be clearly noted that the model object returned
by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or resultset object,
but merely a wrapper proving L<methods|/METHODS> to access the underlying
-schema (but also proxies other methods to the underlying schema.)
+schema.
In addition to this model class, a shortcut class is generated for each
source in the schema, allowing easy and direct access to a resultset of the
@@ -167,8 +161,6 @@
=head1 CONFIG PARAMETERS
-Any options in your config not listed here are passed to your schema.
-
=head2 schema_class
This is the classname of your L<DBIx::Class::Schema> Schema. It needs
@@ -279,9 +271,10 @@
Array of Traits to apply to the instance. Traits are L<Moose::Role>s.
-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
@@ -306,6 +299,8 @@
=item L<Catalyst::TraitFor::Model::DBIC::Schema::Replicated>
+=item L<Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy>
+
=back
=head2 storage_type
@@ -355,15 +350,6 @@
=head1 METHODS
-Methods not listed here are delegated to the connected schema used by the model
-instance, so the following are equivalent:
-
- $c->model('DB')->schema->my_accessor('foo');
- # or
- $c->model('DB')->my_accessor('foo');
-
-Methods on the model take precedence over schema methods.
-
=head2 new
Instantiates the Model based on the above-documented ->config parameters.
@@ -404,6 +390,14 @@
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.
@@ -436,6 +430,8 @@
coerce => 1
);
+has schema => (is => 'rw', isa => Schema);
+
sub BUILD {
my ($self, $args) = @_;
my $class = $self->_original_class_name;
@@ -449,7 +445,7 @@
die "Either ->config->{connect_info} must be defined for $class"
. " or $schema_class must have connect info defined on it."
. " Here's what we got:\n"
- . Dumper($self);
+ . Dumper($args);
}
}
@@ -459,41 +455,43 @@
. " ".$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->composed_schema($schema_class->compose_namespace($class))
+ unless $is_installed;
- $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->schema($self->composed_schema->clone)
+ unless $self->schema;
- $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(@_); }
+# some proxy methods, see also SchemaProxy
+
+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<BUILD> time before configuration, but after L</connect_info> is
set. To do something after configuuration use C<< after BUILD => >>.
+Receives a hashref of args passed to C<BUILD>.
+
=cut
sub setup { 1 }
@@ -566,55 +564,6 @@
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
@@ -659,6 +608,14 @@
}
});
+=head1 METHOD PROXYING
+
+The automatic proxying to the underlying L<DBIx::Class::Schema> has been
+removed as of version C<0.34>, to enable this feature add C<SchemaProxy> to
+L</traits>.
+
+See L<Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy>.
+
=head1 SEE ALSO
General Catalyst Stuff:
@@ -676,6 +633,7 @@
L<Catalyst::TraitFor::Model::DBIC::Schema::Caching>,
L<Catalyst::TraitFor::Model::DBIC::Schema::Replicated>,
+L<Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy>,
L<Catalyst::TraitFor::Model::DBIC::Schema::QueryLog>
=head1 AUTHOR
Added: Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm (rev 0)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/lib/Catalyst/TraitFor/Model/DBIC/Schema/SchemaProxy.pm 2009-12-27 08:57:51 UTC (rev 12483)
@@ -0,0 +1,117 @@
+package Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy;
+
+use namespace::autoclean;
+use Moose::Role;
+use Carp::Clan '^Catalyst::Model::DBIC::Schema';
+use Catalyst::Model::DBIC::Schema::Types 'Schema';
+
+=head1 NAME
+
+Catalyst::TraitFor::Model::DBIC::Schema::SchemaProxy - Proxy Schema Methods and
+Options from Model
+
+=head1 DESCRIPTION
+
+Allows you to call your L<DBIx::Class::Schema> methods directly on the Model
+instance, and passes config options to the C<Schema> attributes at C<BUILD>
+time.
+
+Methods and attributes local to your C<Model> take precedence over C<Schema>
+methods and attributes.
+
+=cut
+
+after setup => sub {
+ my ($self, $args) = @_;
+
+ my $schema = $self->schema;
+
+ my $was_mutable = $self->meta->is_mutable;
+
+ $self->meta->make_mutable;
+ $self->meta->add_attribute('schema',
+ is => 'rw',
+ isa => Schema,
+ handles => $self->_delegates # this removes the attribute too
+ );
+ $self->meta->make_immutable unless $was_mutable;
+
+ $self->schema($schema) if $schema;
+};
+
+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<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
+
+=head1 AUTHOR
+
+See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
+L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
+
+=head1 COPYRIGHT
+
+See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
+
+=head1 LICENSE
+
+This program is free software, you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1;
Modified: Catalyst-Model-DBIC-Schema/branches/upgrade/t/09schema_options.t
===================================================================
--- Catalyst-Model-DBIC-Schema/branches/upgrade/t/09schema_options.t 2009-12-27 08:51:28 UTC (rev 12482)
+++ Catalyst-Model-DBIC-Schema/branches/upgrade/t/09schema_options.t 2009-12-27 08:57:51 UTC (rev 12483)
@@ -20,9 +20,12 @@
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', {
+ traits => 'SchemaProxy',
schema_class => 'ASchemaClass',
connect_info => ['dbi:SQLite:foo.db', '', ''],
@_,
})
}
+
+{ package MyApp; use Catalyst; }
More information about the Catalyst-commits
mailing list