You Can't Get there From Here

My last few posts you may of noticed I have been using Moose with a more professional end in mind rather than my usual playing about with AD&D so I figured I might as well keep going on with it a bit as these are real world (well at least my world) situations rather than contrived gaming stuff. As well my latest round of work is a great source of posts.

Well on the platter today is 'MooseX::AuthorizedMethods' char broiled au jus with in season mixed vegetables with Gorau Glas served on the side.

So this module really doesn't do a whole lot it simply add a little more sugar to your Moose stew, by exporting the "authorized" declarator so now you can stop the invocation of a method if the invoker does not have a specific role.

Ok that is great in theory but how about the practical example? Well I was lucky as for this current project I decided to put the authorization part into roles and just before I wrote a whack of code to apply it I stumbled on this MooseX and saved myself a good deal of time.

So the example. Lest say I have a 'Product::Order' that has a 'shipping_authorized' method which when invoked will cause the 'Product' in question to be packaged and then shipped to the buyer. Well in most businesses that do this kind of work will enforce one of these rules this this situation

  1. Have an account with a client
  2. Accept normal business to business purchase arrangements
  3. Accept only Cash or equivalent.

These three can be translated into three roles

  1. Product::BRULE::Standing_Offer
  2. Product::BRULE::PO
  3. Product::BRULE::COD

So in my 'Product::Order' class I can do this

package Product::Order;
use MooseX::AuthorizedMethods;
authorized shipping_authorized =>  [qw(Product::BRULE::PO Product::BRULE::Standing_Offer Product::BRULE::Paid_In_Full ], sub {
So now only 'Orders' that have consumed the POs or Standing Orders role or the 'Order' has had the Paid_In_Full full role applied to will be able to use 'shipping_authorized' method. So an Order with just the 'Product::BRULE::COD' role applied cannot invoke the method until the 'Paid_In_Full' role is applied.

One thing that did throw some other programmers off was the need to only use 'use MooseX::AuthorizedMethods;' as this includes 'use Moose;' there where a few questions why but that was quickly cleared up.

So far with my somewhat limited use of this MooseX (about 12 classes so far) I have found it easy to work with, as in my case I happened to first, have the Business rules nicely laid out for me first and I decided to use roles to enforce these rules.

In the end my actual 'BRULE' roles are rather empty having just one or two default state attributes and I think only one with a method. So this simple authorization (just checks for a role) works fine for me.

I do see in the POD that it can handle the complex cases by adding your own validation subs though I haven't used them yet, but we shall see what tomorrow brings when I get into more complex "Biz Rule" matrices.


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