merge cleanup-moose into trunk
Budrica Petre Cosmin [Thu, 23 Jul 2009 14:30:50 +0000 (14:30 +0000)]
Makefile.PL
lib/Catalyst/Authentication/Credential/FBConnect.pm

index f17381b..f149c00 100644 (file)
@@ -3,6 +3,9 @@ use inc::Module::Install;
 name 'Catalyst-Authentication-Credential-FBConnect';
 all_from 'lib/Catalyst/Authentication/Credential/FBConnect.pm';
 
+requires 'MooseX::Types';
+requires 'MooseX::Types::Common';
+requires 'namespace::autoclean';
 requires 'WWW::Facebook::API';
 requires 'Moose';
 build_requires 'Catalyst::Runtime';
index 869d466..c87785e 100644 (file)
@@ -1,47 +1,35 @@
 package Catalyst::Authentication::Credential::FBConnect;
-use strict;
-use warnings;
-
-use Moose; # Moose gave you strict and warnings already
-
-# This _config thing just isn't needed.. It's a throwback in the code you've
-# cargo culted to the days when the auth credential used to be a plugin
-# on the app class :/
-has _config => ( is => 'rw' ); # Do these actually need to be rw??
-has debug => ( is => 'rw' );
-has key => ( is => 'rw' );
-has secret => ( is => 'rw' );
-has app_name => ( is => 'rw' );
-has fbconnect => ( is => 'rw' );
-
+use Moose;
+use MooseX::Types::Moose qw/ Bool /;
+use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
 use WWW::Facebook::API;
 use Catalyst::Exception ();
+use namespace::autoclean;
 
-sub new { # Writing your own ->new method makes Moose sad, implement BUILDARGS
-          # and BUILD instead
-       my ($class, $config, $c, $realm) = @_;
+has debug => ( is => 'ro', isa => Bool, );
+has api_key => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has secret => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has app_name => ( is => 'ro', isa => NonEmptySimpleStr, required => 1 );
+has fbconnect => ( is => 'ro', lazy_build => 1, init_arg => undef, isa => 'WWW::Facebook::API' );
 
-       my $self = { _config => {
-               %{ $config },
-               %{ $realm->{config} } # Ewww, gross hack to steal the realms config too.
-       } };
+sub BUILDARGS {
+       my ($class, $config, $c, $realm) = @_;
 
-       bless $self, $class;
+    return $config;
+}
 
-       $self->debug( $self->_config->{debug} );
+sub BUILD {
+    my ($self) = @_;
+    $self->fbconnect; # Ensure lazy value is built.
+}
 
-       $self->key( $self->_config->{key} );
-       $self->secret( $self->_config->{secret} );
-       $self->app_name( $self->_config->{app_name} );
+sub _build_fbconnect {
+    my $self = shift;
 
-       $self->fbconnect( WWW::Facebook::API->new(
+       WWW::Facebook::API->new(
                desktop => 0,
-               app_name => $self->app_name,
-               api_key => $self->key,
-               secret => $self->secret
-       ) );
-
-       return $self;
+               map { $_ => $self->$_() } qw/ app_name api_key secret /
+       );
 }
 
 sub authenticate {
@@ -70,8 +58,7 @@ sub authenticate {
                return;
        }
        else {
-
-               $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action ) ) );
+               $c->res->redirect( $self->fbconnect->get_login_url( next => $c->uri_for( $c->action, $c->req->captures, @{ $c->req->args } ) ) );
        }
 
 }
@@ -102,19 +89,19 @@ In MyApp.pm
 
 In myapp.conf
 
- <Plugin::Authentication>
-      default_realm    facebook
-      <realms>
-           <facebook>
+    <Plugin::Authentication>
+        default_realm  facebook
+        <realms>
+            <facebook>
                 <credential>
-                     class     FBConnect
-               </credential>
-               key my_app_key
-                secret my_app_secret
-                app_name my_app_name
-           </facebook>
-      </realms>
-</Plugin::Authentication>
+                    class       FBConnect
+                    api_key     my_app_key
+                    secret      my_app_secret
+                    app_name    my_app_name
+                </credential>
+            </facebook>
+        </realms>
+    </Plugin::Authentication>
 
 
 In controller code,