Entering MooseX, Part the Third

Now for my next installment I think I will carry on the same pattern as in my last post namely just taking the next file in line and bending it to my will. So lets see what is next.
Now the namespace is 'MooseX::Meta::Method' and the file is 'Authorized.pm'.

Now my code base I have nothing in this name-space because I wanted to avoid a name-space collision as we all know you cannot have two files with the same name (well except in windows if the case is different). So under 'Method' I added another namespace called 'Role' and add my version of 'Authorized.pm'. there,

So in the the original we have
 
package MooseX::Meta::Method::Authorized;
use MooseX::Meta::Method::Authorized::Meta::Role;
use Moose::Util::TypeConstraints;
use aliased 'MooseX::Meta::Method::Authorized::HasRoles';

has requires =>
  ( is => 'ro',
    isa => 'ArrayRef',
    default => sub { [] } );

my $default_verifier = HasRoles->new();

has verifier =>
  ( is => 'ro',
    isa => duck_type(['authorized_do']),
    default => sub { $default_verifier } );

around wrap => sub {
    my ($wrap, $method, $code, %options) = @_;

    my $meth_obj;
    $meth_obj = $method->$wrap
      (
       sub {
           $meth_obj->verifier->authorized_do($meth_obj, $code, @_)
       },
       %options
      );
    return $meth_obj;
};

1;

So what changes to make. Well the name space of course and I can get rid of the '%options' in the around wrap code.

That leaves the 'verifier' attribute and the rather unsightly

 
my $default_verifier = HasRoles->new();'

local variable code. Not that this is bad per say I just do not like to mix in that sort of old school local variable outside a sub in a Moose class. Come to think of it, I may not need it at all, as I no longer want to use a custom 'verifier' so I might be able to get away with just moving the 'authorized_do' code right into this Mod. No if I can get away with that I can also get rid of 'use Moose::Util::TypeConstraints;' and the 'use aliased 'MooseX::Meta::Method::Role::Authorized::HasRoles;' at the same time.

So now I my Mod looks like this

 
package MooseX::Meta::Method::Role::Authorized;
use  MooseX::Meta::Method::Role::Authorized::Meta::Role;

has requires =>
  ( is => 'ro',
    isa => 'ArrayRef',
    default => sub { [] } );

around wrap => sub {
    my ($wrap, $method, $code, %options) = @_;
    my $meth_obj;
    $meth_obj = $method->$wrap
      (
       sub {
           $meth_obj->authorized_do($meth_obj, $code, @_)
       },
       %options
      );
    return $meth_obj;
};

sub authorized_do {
    my $self = shift;
    my $method = shift;
    my $roles = $method->requires;
    my $code = shift;
     my ($instance) = @_;
       # warn("here someplce roles=".Dumper($roles ));
    die "You die now GI!!" if !Moose::Util::does_role($instance,$roles->[0]);
     $code->(@_);
    #just to see if this works
}
1;

and I gave it a try with this

 


my $order1 = Product::Order->new({type=>'PO'});
my $route_id = $order1->shipping_authorized();
print "Route_id=".$route_id;
my $order2 = Product::Order->new({type=>'COD'});
$route_id = $order2->shipping_authorized();
print  $route_id;



and then I get something like this

 


Route_id=603723 
You Die Now GI!! at C:/Dwimperl/perl/site/lib/MooseX/Meta/Method/Role/Authorized.pm line 40.



So that works. It is beginning to look less and less like a simple clone and more its own entity.

Well onto bigger things tomorrow!

1385720206828.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations