[Catalyst-commits] r8789 - / trunk/examples/CatalystAdvent/root/2008

zarquon at dev.catalyst.perl.org zarquon at dev.catalyst.perl.org
Mon Dec 8 11:11:58 GMT 2008


Author: zarquon
Date: 2008-12-08 11:11:58 +0000 (Mon, 08 Dec 2008)
New Revision: 8789

Added:
   trunk/examples/CatalystAdvent/root/2008/8.pod
Modified:
   /
Log:
 r14501 at fenchurch:  kd | 2008-12-08 22:11:14 +1100
 day 8
 r14502 at fenchurch:  kd | 2008-12-08 22:11:35 +1100
 day 8 live



Property changes on: 
___________________________________________________________________
Name: svk:merge
   - 1b129c88-ebf4-0310-add9-f09427935aba:/local/catalyst:4278
1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst:14499
3b9770f9-e80c-0410-a7de-cd203d167417:/local/catalyst:3514
dd8ad9ea-0304-0410-a433-df5f223e7bc0:/local/Catalyst:6909
   + 1b129c88-ebf4-0310-add9-f09427935aba:/local/catalyst:4278
1c72fc7c-9ce4-42af-bf25-3bfe470ff1e8:/local/Catalyst:14502
3b9770f9-e80c-0410-a7de-cd203d167417:/local/catalyst:3514
dd8ad9ea-0304-0410-a433-df5f223e7bc0:/local/Catalyst:6909

Added: trunk/examples/CatalystAdvent/root/2008/8.pod
===================================================================
--- trunk/examples/CatalystAdvent/root/2008/8.pod	                        (rev 0)
+++ trunk/examples/CatalystAdvent/root/2008/8.pod	2008-12-08 11:11:58 UTC (rev 8789)
@@ -0,0 +1,75 @@
+=head1 Day 8.  Some notes on ACCEPT_CONTEXT, with and without antlers.
+
+Today we'll show you how to avoid spaghetti controllers with
+ACCEPT_CONTEXT.  It's tempting to stuff lots of business logic in your
+Controller, while usually this belongs in your Model.  Although at
+first it seems like more effort to go to the trouble of setting up the
+model correctly, but as your application grows, taking care of design
+like this rapidly pays dividends.
+<
+If you need access to a database schema or two in a model, or if you
+need to be able to access C< path_to > or something else that belongs
+in $c, you'll want to use this technique.  First we'll show how to do
+this without L<Moose>, then With L<Moose>.
+
+=head2 Without Moose.
+
+Here's an example that gives us access to $c->path_to:
+
+  __PACKAGE__->mk_accessors(qw/path_to/);
+
+ sub ACCEPT_CONTEXT {
+     my ($self, $c ) = @_;
+     $self = bless({ %$self,
+                     path_to => $c->path_to(''),
+                 }, ref($self));
+     return $self;
+ }
+
+
+Or if we wanted two different database schemas accessible from two different Catalyst::Models:
+
+ __PACKAGE__->mk_accessors(qw(schema1 schema2));
+
+ sub ACCEPT_CONTEXT {
+     my ( $self, $c, @extra_arguments ) = @_;
+     $self = bless({ %$self,
+                     schema1  => $c->model('My::Schema1')->schema,
+                     schema2 => $c->model('My::Schema2')->schema,
+         }, ref($self));
+     return $self;
+ }
+
+Now when you have a subroutine in your model, you can access the
+relevant bit of $c with $self->path_to, $self->schema1 or whatever
+you named your accessor.  This avoids polluting your model with the
+bits of the context object that you don't need, makes your code more
+maintainable, and avoids some nasty gotchas that are to do with
+variable scope.
+
+=head2 Doing the same with Moose
+
+Catalyst 5.8 will use L<Moose>, so it makes sense to use the L<Moose>
+meta-object system to handle these issues for you.
+
+The code is pretty similar:
+
+ use Moose;
+
+ sub ACCEPT_CONTEXT {
+     my ($self, $c ) = @_;
+     $self->meta->clone_object($self, path_to => $c->path_to(''));
+     return $self;
+ }
+
+The C< meta > method above is a benefit of L<Moose> (actually
+L<Class::MOP>.  Although this example isn't much clearer with Moose
+than without, for the most part Moose greatly increases the read- and
+write-ability of your code.  see L<Moose::Manual::Cookbook> for many
+examples.
+
+=AUTHORS
+
+Kieren Diment <zarquon at cpan.org>
+Robert 'phaylon' Sedlacek <rs at 474.at>
+




More information about the Catalyst-commits mailing list