Entering MooseX, Part the Fourth
Well in my last post I manged to get rid of numerous bits of the original MooseX::Authorized Methods and get it down to what I find is just what I need the bare-basic check if a class has a role.
Now digging ever deeper into the 'clone' of Authorized Methods I have the 'MooseX::Meta::Method::Role::Authorized::HasRoles' class and I can just delete that one as I moved its functionality into the 'MooseX::Meta::Method::Role::Authorized' class so that leaves me with 'MooseX::Meta::Method::Role::Authorized::Meta::Role' and here that is
package MooseX::Meta::Method::Role::Authorized::Meta::Role;
use Moose::Role;
use Moose::Util::MetaRole;
use Moose::Exporter;
Moose::Exporter->setup_import_methods(also => 'Moose::Role');
sub init_meta {
my ($class, %opts) = @_;
Moose::Role->init_meta(%opts);
Moose::Util::MetaRole::apply_metaroles
(
for => $opts{for_class},
role_metaroles =>
{
application_to_role =>
['MooseX::Meta::Method::Role::Authorized::Application::ToComposite'],
application_to_class =>
['MooseX::Meta::Method::Role::Authorized::Application::ToClass'],
application_to_instance =>
['MooseX::Meta::Method::Role::Authorized::Application::ToInstance'],
}
);
return $opts{for_class}->meta();
};
1;
Well giving it the quick once over it all looks good and nothing for me to do here as this is the 'Role' that I use in 'MooseX::Meta::Method::Role::Authorized'. Now here is where Moose::Util::MetaRole set up the meta for any calling class of this role so there is not much I can do here.
The batch of files 'ToClass', 'ToComposite' and 'ToInstance' are under the 'MooseX::Meta::Method::Role::Authorized::Application' namespace and used by ' Moose::Util::MetaRole' to set things up.
Out of the three only 'ToInstance' my have some changes
package MooseX::Meta::Method::Role::Authorized::Application::ToInstance;
use Moose::Role;
after apply => sub {
my ($self, $role, $instance, $args) = @_;
$instance->meta->add_role($role);
my $original_body = $instance->body;
my $new_body = sub {
$instance->verifier->authorized_do($instance, $original_body, @_)
};
# HACK! XXX!
# 1 - body is ro in Method, need to force the change,,,
$instance->{body} = $new_body;
# 2 - need to reinstall the CODE ref in the glob
no warnings 'redefine';
no strict 'refs';
*{$instance->package_name.'::'.$instance->name} = $new_body;
};
1;
Well a #HACK! XXX! comment at lest the fellow is honest. It is a little funny to me why that would be considered a Hack it was at one time rather normal Perl and I am sure 90% of us out there have used a Modl that used something similar, oh well I guess time change.
Anyway there might be some changes I can make in this one as I do not think I need that ' $instance->verifier->' anymore I will give it the once over like this
package MooseX::Meta::Method::Role::Authorized::Application::ToInstance;
use Moose::Role;
after apply => sub {
my ($self, $role, $instance, $args) = @_;
$instance->meta->add_role($role);
};
1;
and I give my code from the other post a run
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 what I want
Route_id=603723
You Die Now GI!! at C:/Dwimperl/perl/site/lib/MooseX/Meta/Method/Role/Authorized.pm line 40.
Lovely! Things will work as I expected. So to me that is a good deal cleaner. Now I have to do the hard part. Tests, Documentation and Packaging, but maybe an enhancement or two first??
Leave a comment