[Bast-commits] r8993 - DBIx-Class/0.08/trunk/t/row
mo at dev.catalyst.perl.org
mo at dev.catalyst.perl.org
Sat Mar 13 09:03:24 GMT 2010
Author: mo
Date: 2010-03-13 09:03:24 +0000 (Sat, 13 Mar 2010)
New Revision: 8993
Added:
DBIx-Class/0.08/trunk/t/row/inflate_result.t
Log:
test the dynamic subclassing example
Added: DBIx-Class/0.08/trunk/t/row/inflate_result.t
===================================================================
--- DBIx-Class/0.08/trunk/t/row/inflate_result.t (rev 0)
+++ DBIx-Class/0.08/trunk/t/row/inflate_result.t 2010-03-13 09:03:24 UTC (rev 8993)
@@ -0,0 +1,111 @@
+package My::Schema::Result::User;
+
+use strict;
+use warnings;
+use base qw/DBIx::Class::Core/;
+
+### Define what our admin class is, for ensure_class_loaded()
+my $admin_class = __PACKAGE__ . '::Admin';
+
+__PACKAGE__->table('users');
+
+__PACKAGE__->add_columns(
+ qw/user_id email password
+ firstname lastname active
+ admin/
+);
+
+__PACKAGE__->set_primary_key('user_id');
+
+sub inflate_result {
+ my $self = shift;
+ my $ret = $self->next::method(@_);
+ if ( $ret->admin ) { ### If this is an admin, rebless for extra functions
+ $self->ensure_class_loaded($admin_class);
+ bless $ret, $admin_class;
+ }
+ return $ret;
+}
+
+sub hello {
+ return "I am a regular user.";
+}
+
+package My::Schema::Result::User::Admin;
+
+use strict;
+use warnings;
+use base qw/My::Schema::Result::User/;
+
+# This line is important
+__PACKAGE__->table('users');
+
+sub hello {
+ return "I am an admin.";
+}
+
+sub do_admin_stuff {
+ return "I am doing admin stuff";
+}
+
+package My::Schema;
+
+use base qw/DBIx::Class::Schema/;
+
+My::Schema->register_class( Admin => 'My::Schema::Result::User::Admin' );
+My::Schema->register_class( User => 'My::Schema::Result::User' );
+
+1;
+
+package main;
+
+use lib qw(t/lib);
+use DBICTest;
+
+use Test::More;
+
+my $user_data = {
+ email => 'someguy at place.com',
+ password => 'pass1',
+ admin => 0
+};
+
+my $admin_data = {
+ email => 'someadmin at adminplace.com',
+ password => 'pass2',
+ admin => 1
+};
+
+ok( my $schema = My::Schema->connection('dbi:SQLite:dbname=:memory:') );
+
+ok(
+ $schema->storage->dbh->do(
+"create table users (user_id, email, password, firstname, lastname, active, admin)"
+ )
+);
+
+TODO: {
+ local $TODO = 'New objects should also be inflated';
+ my $user = $schema->resultset('User')->create($user_data);
+ my $admin = $schema->resultset('User')->create($admin_data);
+
+ is( ref $user, 'My::Schema::Result::User' );
+ is( ref $admin, 'My::Schema::Result::User::Admin' );
+
+}
+
+my $user = $schema->resultset('User')->single($user_data);
+my $admin = $schema->resultset('User')->single($admin_data);
+
+is( ref $user, 'My::Schema::Result::User' );
+is( ref $admin, 'My::Schema::Result::User::Admin' );
+
+is( $user->password, 'pass1' );
+is( $admin->password, 'pass2' );
+is( $user->hello, 'I am a regular user.' );
+is( $admin->hello, 'I am an admin.' );
+
+ok( !$user->can('do_admin_stuff') );
+ok( $admin->can('do_admin_stuff') );
+
+done_testing;
More information about the Bast-commits
mailing list