MooseX for No Poop

Well there still lots of life in the a little Moose-Pen today seem I still have plenty of poop to cleanup.

Well in my last post I cleaned up a little moose poop and today I am going to take a major step forward and clean up allot more. So far most if not all the poop I have been generation comes from my efforts of either

  1. Having required attributes
  2. Coercion into classes or
  3. Type errors

Well one would think there will be a large amount code to cover in a single post but thanks to Moose I really only have to add


use MooseX::Constructor::AllErrors

after the with command on my various Database::Accessor classes and that will take care of all of the above as it captures all of the constructor errors into one larger error class 'MooseX::Constructor::AllErrors::Error::Constructor ' with all sorts of nice attributes like 'missing' which would contain any missing required attributes, and 'invalid' which holds any of my coercion and type errors. In short this MooseX greatly simplifies making my Accessor much more user friendly.

So lets start with a simple with a simple example in the 30_view.t test case. So I first add in this on Accessor class


use Moose;
with qw(Database::Accessor::Types);
++ use MooseX::Constructor::AllErrors;

and in the test case I add in
  
eval {
$da = Database::Accessor->new({});
};
if ($@){
pass("View is Required")
}
else {
fail("View is Required");
}

which should fail as I have no view but opps!

not ok 4 - View is Required

Found another bug, see test driven programming really pasy off, so after I added in the 'required' for that I get

ok 4 - View is Required

Now with MooseX::Constructor::AllErrors; at the top of my file my $@ is no longer a big trail of Moose poop but a and object as one can see in this test


pass("View is Required");
++ ok(ref($@) eq 'MooseX::Constructor::AllErrors::Error::Constructor','Got error object');

ok 4 - View is Required
ok 5 - Got error object

so that is all neat and good. Now as a side note if the eval was not there and I ran this just in a script I would get


Attribute (view) is required at …

so even without the eval most of the Moose Poop is gone.

Now this MooseX should collect all of the input errors so lets try this


$in_hash = {
view => {name=>undef,
alias=>undef}
};
eval {
$da = Database::Accessor->new($in_hash);
};
ok($@->has_errors(),"Error on New");
ok(scalar($@->errors) == 2,"Two errors on new");
my @errors = $@->errors;
ok($errors[0]->attribute->name() == 'name',"View: param name fails");
ok($errors[1]->attribute->name() == 'alias',"View: param alias failes");

and give the tests a go and I get

Can't locate object method "has_errors" via package "Moose::Exception::ValidationFailedForTypeConstraint"

opp seems I forgot to add this

package Database::Accessor::View;
use Moose;
with qw(Database::Accessor::Roles::Alias);
++ use MooseX::Constructor::AllErrors;

which is only logical as I now need to test the params going into a coercion for View. Now with the above added in I get

ok 7 - Two errors on new
ok 8 - View: param name fails
ok 9 - View: param alias fails

So this little MooseX is going to see a lot of use from me.

examining-moose-poop.600x600.jpg

6 Comments

I made an account specifically to comment that the whole "Moose poop" and other strange titles are driving people away from reading your blog.

I wish the titles were more descriptive. "Exception Handling in Moose," is more appealing that "Cute Moose" or "Cleaning up Moose Poop."

Just a suggestion.

They're also spamming the r/perl subreddit.

To be fair, they're not spamming reddit. _perly_bot just posts every post from blogs.perl.org to reddit.

I've had to ban the _perly_bot to avoid seeing these ridiculous titles when I visit /r/perl.

Hopefully byterock reconsiders making more descriptive and less strange titles because the current ones are just driving people away from both his/her blog and /r/perl.

FWIW: including comment in the article's source will prevent _perly_bot from posting it in places.

-_- looks like the site strips HTML comments... I meant text "no-perly-bot" inside a comment.

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