Osti d'├ępais de marde d'orignal

Today in the Moose-Pen I am going to solve a little problem with Moose.

So cleaning up a lot of Moose poop over the last few days and part of that effort is to ensure some of my proposed features with this post. Now I have re-factored that a little today into this sub


sub _need_condition {
my $self = shift;
my ($action,$required) = @_;
my $is_required = $required || 0;

die "Attempt to $action without condition"
if (
$is_required
and
( $self->condition_count() + $self->dynamic_condition_count() <= 0 )
);
}


and now I still have these attributes to code in
  • no_create

  • no_retrieve

  • no_update

  • no_delete

  • retrieve_only

So I started with this sub

sub _cannot_flag {
my $self = shift;
my ($action,$cannot,$flag) = @_;
die "Attempt to $action with $flag Flag on!"
if ($cannot);
}

and call it like this

$self->_cannot_flag(Database::Accessor::Constants::DELETE,$self->no_delete(),"no_delete");

and my test is simple enough

$in_hash->{no_delete} = 1;
$da = Database::Accessor->new($in_hash);
eval {
$da->delete( $data, $return_str );
};
ok ($@,'No Delete with no_delete flag');

The problems comes with the 'retrieve_only' flag. I still get a fail with this test

delete($in_hash->{no_delete});
$in_hash->{retrieve_only} = 1;
$da = Database::Accessor->new($in_hash);
eval {
$da->delete( $data, $return_str );
};
ok ($@,'No Delete with retrieve_only flag');

but the real problem is my $@ is going to give me this error

Attempt to DELETE with no_delete Flag on!

Which is true as setting the ' retrieve_only flag' will flip a few other flags, so I might get an error but someone would be rather pissed it they did this

my $in_hash = {
no_delete => 0,
retrieve_only=>1,
...

and spent forty-five mins trying to see why the turn of no_delete is not working.

Now how can Moose help with this? Well Moose can't but a MooseX can.

If I use 'MooseX::MetaDescription' I can extend the meta data of Moose to use a custom value. So to start I need this at the top of my Accessor class


use Database::Accessor::Constants;
++ use MooseX::MetaDescription;

add with my flag attributes like this

has [
qw(no_create
no_retrieve
no_update
no_delete
retrieve_only
)
] => ( is => 'ro',
isa => 'Bool',
default => 0,
);

I will have to break them into individual 'has' commands (a small price to pay) like this

has no_delete=> (
is => 'ro',
isa => 'Bool',
default => 0,
traits => [ 'MooseX::MetaDescription::Meta::Trait' ],
description =>{message=>"Attempt to use delete with no_delete flag on!"}
);

I now have an extra 'meta' attribute on my no_delete class which contains my error message, Now in sub BUILD I add in this little bit of code

if ($self->retrieve_only){
foreach my $flag (qw(no_create no_update no_delete)) {
my $field = $self->meta->get_attribute($flag);
$field->description->{message} = "No Create, Update or Delete with retrieve_only flag on";
}
}

So if my Accessor class has the 'retrieve_only' flag set I iterate over the three other flags get their meta attributes with the 'meta->get_attribute' call then change them for all the same message with the next line.

Now my test will still pass but the message I am getting with $@ is now


No Create, Update or Delete with retrieve_only flag on at...

Moose comes though again.


IMG_8270.JPG

1 Comment

Seriously? A picture of moose poop? It's disgusting. This doesn't help attract new Moose developers.

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