[Catalyst-commits] r7690 - in CatalystX-CRUD/CatalystX-CRUD/trunk:
lib/CatalystX/CRUD lib/CatalystX/CRUD/ModelAdapter
lib/CatalystX/CRUD/Test t t/lib/MyApp/Controller t/lib/MyApp/Model
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Tue May 6 19:53:34 BST 2008
Author: karpet
Date: 2008-05-06 19:53:33 +0100 (Tue, 06 May 2008)
New Revision: 7690
Removed:
CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileAdapter.pm
Modified:
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/t/01-file.t
CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/FileAdapter.pm
Log:
finish fleshing out the Adapter API and add tests
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter/File.pm 2008-05-06 18:53:33 UTC (rev 7690)
@@ -1,12 +1,7 @@
package CatalystX::CRUD::ModelAdapter::File;
use strict;
use warnings;
-use base qw(
- CatalystX::CRUD::ModelAdapter
- CatalystX::CRUD::Model::File
- CatalystX::CRUD::Model::Utils
-);
-use Class::C3;
+use base qw( CatalystX::CRUD::ModelAdapter );
our $VERSION = '0.26';
@@ -37,9 +32,6 @@
=cut
-# must implement the following methods
-# but we just end up calling the Model::File superclass
-
=head2 new_object( I<context>, I<args> )
Implements required method.
@@ -48,7 +40,8 @@
sub new_object {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->new_object(@arg);
}
=head2 fetch( I<context>, I<args> )
@@ -59,7 +52,8 @@
sub fetch {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->fetch(@arg);
}
=head2 search( I<context>, I<args> )
@@ -70,7 +64,8 @@
sub search {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->search(@arg);
}
=head2 iterator( I<context>, I<args> )
@@ -81,7 +76,8 @@
sub iterator {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->iterator(@arg);
}
=head2 count( I<context>, I<args> )
@@ -92,7 +88,8 @@
sub count {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->count(@arg);
}
=head2 make_query( I<context>, I<args> )
@@ -103,9 +100,54 @@
sub make_query {
my ( $self, $c, @arg ) = @_;
- $self->next::method(@arg);
+ my $model = $c->model( $self->model_name );
+ $model->make_query(@arg);
}
+=head2 create( I<context>, I<file_object> )
+
+Implements required CRUD method.
+
+=cut
+
+sub create {
+ my ( $self, $c, $file ) = @_;
+ $file->create;
+}
+
+=head2 read( I<context>, I<file_object> )
+
+Implements required CRUD method.
+
+=cut
+
+sub read {
+ my ( $self, $c, $file ) = @_;
+ $file->read;
+}
+
+=head2 update( I<context>, I<file_object> )
+
+Implements required CRUD method.
+
+=cut
+
+sub update {
+ my ( $self, $c, $file ) = @_;
+ $file->update;
+}
+
+=head2 delete( I<context>, I<file_object> )
+
+Implements required CRUD method.
+
+=cut
+
+sub delete {
+ my ( $self, $c, $file ) = @_;
+ $file->delete;
+}
+
1;
=head1 AUTHOR
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter.pm 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/ModelAdapter.pm 2008-05-06 18:53:33 UTC (rev 7690)
@@ -1,10 +1,14 @@
package CatalystX::CRUD::ModelAdapter;
use strict;
use warnings;
-use base qw( CatalystX::CRUD Class::Accessor::Fast );
+use base qw(
+ CatalystX::CRUD
+ Class::Accessor::Fast
+);
+use Class::C3;
use Carp;
-__PACKAGE__->mk_accessors(qw( model_name context ));
+__PACKAGE__->mk_accessors(qw( model_name model_meta ));
=head1 NAME
@@ -57,7 +61,7 @@
=head1 METHODS
-CatalystX::CRUD::Model inherits from CatalystX::CRUD.
+CatalystX::CRUD::ModelAdapter inherits from CatalystX::CRUD.
The following methods should be implemented in your subclass.
@@ -114,6 +118,44 @@
sub make_query { shift->throw_error("must implement make_query()") }
+=head1 CRUD Methods
+
+The following methods are implemented in CatalystX::CRUD::Object when
+using the CatalystX::CRUD::Model API. When using the ModelAdapter API, they
+should be implemented in the adapter class.
+
+=head2 create( I<context>, I<object> )
+
+Should implement the C in CRUD.
+
+=cut
+
+sub create { shift->throw_error("must implement create()") }
+
+=head2 read( I<context>, I<object> )
+
+Should implement the R in CRUD.
+
+=cut
+
+sub read { shift->throw_error("must implement read()") }
+
+=head2 update( I<context>, I<object> )
+
+Should implement the U in CRUD.
+
+=cut
+
+sub update { shift->throw_error("must implement update()") }
+
+=head2 delete( I<context>, I<object> )
+
+Should implement the D in CRUD.
+
+=cut
+
+sub delete { shift->throw_error("must implement delete()") }
+
1;
__END__
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Test/Controller.pm 2008-05-06 18:53:33 UTC (rev 7690)
@@ -57,8 +57,9 @@
my $obj = $c->stash->{object};
my $obj_meth = $self->init_object;
my $form_meth = $self->init_form;
- my $pk = $self->primary_key
- ; # id always comes from url but not necessarily from form
+
+ # id always comes from url but not necessarily from form
+ my $pk = $self->primary_key;
my $id = $c->req->params->{$pk} || $c->stash->{object_id};
# initialize the form with the object's values
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/t/01-file.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/01-file.t 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/01-file.t 2008-05-06 18:53:33 UTC (rev 7690)
@@ -1,4 +1,4 @@
-use Test::More tests => 15;
+use Test::More tests => 24;
use lib qw( lib t/lib );
use_ok('CatalystX::CRUD::Model::File');
use_ok('CatalystX::CRUD::Object::File');
@@ -7,8 +7,6 @@
use Data::Dump qw( dump );
use HTTP::Request::Common;
-$ENV{CXCRUD_TEST} = 1; # we want stack traces in exceptions
-
###########################################
# set up the test env and config
ok( get('/foo'), "get /foo" );
@@ -68,3 +66,57 @@
#diag( $res->content );
like( $res->content, qr/content => undef/, "file nuked" );
+
+##############################################################
+## Adapter API
+
+# create
+ok( $res = request(
+ POST( '/fileadapter/testfile/save', [ content => 'hello world' ] )
+ ),
+ "POST new file adapter"
+);
+
+is( $res->content,
+ '{ content => "hello world", file => "testfile" }',
+ "POST new file response adapter"
+);
+
+# read the file we just created
+ok( $res
+ = request(
+ HTTP::Request->new( GET => '/fileadapter/testfile/view' ) ),
+ "GET new file adapter"
+);
+
+#diag( $res->content );
+
+like( $res->content, qr/content => "hello world"/, "read file adapter" );
+
+# update the file
+ok( $res = request(
+ POST( '/fileadapter/testfile/save', [ content => 'foo bar baz' ] )
+ ),
+ "update file adapter"
+);
+
+like( $res->content, qr/content => "foo bar baz"/, "update file adapter" );
+
+# delete the file
+
+ok( $res = request( POST( '/fileadapter/testfile/rm', [] ) ),
+ "rm file adapter" );
+
+#diag( $res->content );
+
+# confirm it is gone
+ok( $res
+ = request(
+ HTTP::Request->new( GET => '/fileadapter/testfile/view' ) ),
+ "confirm we nuked the file adapter"
+);
+
+#diag( $res->content );
+
+like( $res->content, qr/content => undef/, "file nuked adapter" );
+
Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/FileAdapter.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/FileAdapter.pm 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/FileAdapter.pm 2008-05-06 18:53:33 UTC (rev 7690)
@@ -11,7 +11,7 @@
form_class => 'MyApp::Form',
form_fields => [qw( file content )],
model_adapter => 'CatalystX::CRUD::ModelAdapter::File',
- model_name => 'FileAdapter',
+ model_name => 'File',
primary_key => 'file',
init_form => 'init_with_file',
init_object => 'file_from_form',
Deleted: CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileAdapter.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileAdapter.pm 2008-05-06 18:52:40 UTC (rev 7689)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileAdapter.pm 2008-05-06 18:53:33 UTC (rev 7690)
@@ -1,9 +0,0 @@
-package MyApp::Model::FileAdapter;
-use strict;
-use base qw( CatalystX::CRUD::Model::File );
-use MyApp::File;
-
-# don't think we need/want this do we?
-__PACKAGE__->config->{object_class} = 'MyApp::File';
-
-1;
More information about the Catalyst-commits
mailing list