[Catalyst-commits] r7899 - in CatalystX-CRUD/CatalystX-CRUD/trunk: . lib/CatalystX/CRUD t

karpet at dev.catalyst.perl.org karpet at dev.catalyst.perl.org
Fri Jun 6 21:31:16 BST 2008


Author: karpet
Date: 2008-06-06 21:31:15 +0100 (Fri, 06 Jun 2008)
New Revision: 7899

Added:
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Results.pm
   CatalystX-CRUD/CatalystX-CRUD/trunk/t/02-controller.t
Modified:
   CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
   CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
Log:
* added CatalystX::CRUD::Results class
* added naked_results() config option to base Controller
* refactored base Controller to make all config options into accessors. added t/02-controller


Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/Changes
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2008-06-06 09:01:37 UTC (rev 7898)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/Changes	2008-06-06 20:31:15 UTC (rev 7899)
@@ -122,4 +122,9 @@
 0.27    xxx
         * fix Controller->rm() to check for model_adapter() and call its delete() method as advertised.
         * check for length($oid) instead of defined($oid) in REST->default
+        * added CatalystX::CRUD::Results class
+        * added naked_results() config option to base Controller
+        * refactored base Controller to make all config options into accessors. added t/02-controller
 
+
+

Modified: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm	2008-06-06 09:01:37 UTC (rev 7898)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Controller.pm	2008-06-06 20:31:15 UTC (rev 7899)
@@ -7,10 +7,33 @@
 );
 use Carp;
 use Catalyst::Utils;
+use CatalystX::CRUD::Results;
 use Class::C3;
 
-__PACKAGE__->mk_accessors(qw( model_adapter ));
+__PACKAGE__->mk_accessors(
+    qw(
+        model_adapter
+        form_class
+        init_form
+        init_object
+        model_name
+        model_meta
+        default_template
+        primary_key
+        allow_GET_writes
+        naked_results
+        page_size
+        )
+);
 
+__PACKAGE__->config(
+    primary_key           => 'id',
+    view_on_single_result => 0,
+    page_size             => 50,
+    allow_GET_writes      => 0,
+    naked_results         => 0,
+);
+
 our $VERSION = '0.27';
 
 =head1 NAME
@@ -36,6 +59,7 @@
             view_on_single_result   => 0,
             page_size               => 50,
             allow_GET_writes        => 0,
+            naked_results           => 0,
     );
                     
     1;
@@ -570,6 +594,14 @@
 Prepare and execute a search. Called internally by list()
 and search().
 
+Results are saved in stash() under the C<results> key.
+
+If B<naked_results> is true, then results are set just as they are
+returned from search() or list() (directly from the Model).
+
+If B<naked_results> is false (default), then results is a
+CatalystX::CRUD::Results object.
+
 =cut
 
 sub do_search {
@@ -618,12 +650,14 @@
             $pager = $self->do_model( $c, 'make_pager', $count, $results );
         }
 
-        $c->stash->{results} = {
-            count   => $count,
-            pager   => $pager,
-            results => $results,
-            query   => $query,
-        };
+        $c->stash->{results}
+            = $self->naked_results ? $results : CatalystX::CRUD::Results->new(
+            {   count   => $count,
+                pager   => $pager,
+                results => $results,
+                query   => $query,
+            }
+            );
     }
 }
 
@@ -649,18 +683,12 @@
 
 =item allow_GET_writes
 
+=item naked_results
+
 =back
 
 =cut
 
-sub form_class       { shift->config->{form_class} }
-sub init_form        { shift->config->{init_form} }
-sub init_object      { shift->config->{init_object} }
-sub model_name       { shift->config->{model_name} }
-sub default_template { shift->config->{default_template} }
-sub primary_key      { shift->config->{primary_key} }
-sub allow_GET_writes { shift->config->{allow_GET_writes} }
-
 # see http://use.perl.org/~LTjake/journal/31738
 # PathPrefix will likely end up in an official Catalyst RSN.
 # This lets us have a sane default fetch() method without having

Added: CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Results.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Results.pm	                        (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/lib/CatalystX/CRUD/Results.pm	2008-06-06 20:31:15 UTC (rev 7899)
@@ -0,0 +1,126 @@
+package CatalystX::CRUD::Results;
+use strict;
+use warnings;
+use base qw( Class::Accessor::Fast );
+use Carp;
+use Class::C3;
+
+__PACKAGE__->mk_ro_accessors(qw( count pager query results ));
+
+our $VERSION = '0.27';
+
+=head1 NAME
+
+CatalystX::CRUD::Results - search results class
+
+=head1 SYNOPSIS
+
+ # in .tt file
+ Your search returned [% results.count %] hits.
+ Your query was [% results.query %].
+ You are on page [% results.pager.current_page %].
+ [% FOREACH r IN results.results %]
+  [% loop.count %]: [% r.name %]
+ [% END %]
+
+=head1 DESCRIPTION
+
+CatalystX::CRUD::Results is a class for search results from a
+CatalystX::CRUD::Controller.  See the do_search() method
+in CatalystX::CRUD::Controller.
+
+=head1 METHODS
+
+The following read-only accessors are available:
+
+=head2 count
+
+Returns number of results.
+
+=head2 pager
+
+Returns Data::Pageset object for paging through results.
+
+=head2 query
+
+Returns the search query.
+
+=head2 results
+
+Returns array ref of found objects.
+
+=cut
+
+=head2 next
+
+Returns next result. If results() is an arrayref, shift() is used.
+Otherwise, the results() value is assumed to act like a 
+CatalystX::CRUD::Iterator and its next() method will be called.
+
+=cut
+
+sub next {
+    my $self = shift;
+    if ( ref( $self->results ) eq 'ARRAY' ) {
+        return shift @{ $self->{results} };
+    }
+    else {
+        return $self->results->next;
+    }
+}
+
+1;
+
+__END__
+
+
+=head1 AUTHOR
+
+Peter Karman, C<< <perl at peknet.com> >>
+
+=head1 BUGS
+
+Please report any bugs or feature requests to
+C<bug-catalystx-crud at rt.cpan.org>, or through the web interface at
+L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD>.
+I will be notified, and then you'll automatically be notified of progress on
+your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+    perldoc CatalystX::CRUD
+
+You can also look for information at:
+
+=over 4
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/CatalystX-CRUD>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/CatalystX-CRUD>
+
+=item * RT: CPAN's request tracker
+
+L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/CatalystX-CRUD>
+
+=back
+
+=head1 ACKNOWLEDGEMENTS
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2008 Peter Karman, all rights reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut

Added: CatalystX-CRUD/CatalystX-CRUD/trunk/t/02-controller.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/02-controller.t	                        (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/02-controller.t	2008-06-06 20:31:15 UTC (rev 7899)
@@ -0,0 +1,19 @@
+use Test::More tests => 7 ;
+
+use_ok('CatalystX::CRUD::Controller');
+ok(my $controller = CatalystX::CRUD::Controller->new, "new controller");
+is($controller->page_size, 50, 'default page_size');
+ok($controller->page_size(10), "set page_size");
+is($controller->page_size, 10, "get page_size");
+
+{
+    package MyC;
+    @MyC::ISA = ( 'CatalystX::CRUD::Controller' );
+    MyC->config( page_size => 30 );
+}
+
+ok( my $myc = MyC->new,  "new MyC");
+is( $myc->page_size, 30, "set page_size in package config");
+
+
+




More information about the Catalyst-commits mailing list