Fix defualt -> default typos in perldoc
[catagits/Reaction.git] / lib / Reaction / UI / Controller.pm
index aa1b8be..45a85d8 100644 (file)
@@ -1,12 +1,19 @@
 package Reaction::UI::Controller;
 
-use base qw(
-  Catalyst::Controller
-  Catalyst::Component::ACCEPT_CONTEXT
-  Reaction::Object
-);
+use base qw(Catalyst::Controller Reaction::Object);
 
 use Reaction::Class;
+use Scalar::Util 'weaken';
+use namespace::clean -except => [ qw(meta) ];
+
+has context => (is => 'ro', isa => 'Object', weak_ref => 1);
+with 'Catalyst::Component::InstancePerContext';
+
+sub build_per_context_instance {
+  my ($self, $c, @args) = @_;
+  my $newself =  $self->new($self->_application, {%$self, context => $c, @args});
+  return $newself;
+}
 
 sub push_viewport {
   my $self = shift;
@@ -53,17 +60,17 @@ sub redirect_to {
 
   #the confess calls could be changed later to $c->log ?
   my $action;
-  if(!ref $to){
-      $action = $self->action_for($to);
-      confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
-  }
-  elsif( blessed $to && $to->isa('Catalyst::Action') ){
-      $action = $to;
-  } elsif(ref $action eq 'ARRAY' && @$action == 2){ #is that overkill / too strict?
-      $action = $c->controller($to->[0])->action_for($to->[1]);
-      confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
+  my $reftype = ref($to);
+  if( $reftype eq '' ){
+    $action = $self->action_for($to);
+    confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
+  } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
+    $action = $c->controller($to->[0])->action_for($to->[1]);
+    confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
+  } elsif( blessed $to && $to->isa('Catalyst::Action') ){
+    $action = $to;
   } else{
-      confess("Failed to locate action from ${to}");
+    confess("Failed to locate action from ${to}");
   }
 
   $cap ||= $c->req->captures;
@@ -73,6 +80,13 @@ sub redirect_to {
   $c->res->redirect($uri);
 }
 
+sub make_context_closure {
+  my($self, $closure) = @_;
+  my $ctx = $self->context;
+  weaken($ctx);
+  return sub { $closure->($ctx, @_) };
+}
+
 1;
 
 __END__;