Baby Moose struts his stuff

Well is still container day here in the Moose-pen.

So now that I have the 'execute_array' all fixed up and working I think I will carry on the same past and get the container to work with an array of 'classes'. In that end I created this little class;


package Test::User;
use Moose;
has username => ( is => 'rw',
isa => 'Str',);
has address => ( is => 'rw',
isa => 'Str',);
1;

Now the next thing I needed to do was make a test out of what I fixed yesterday;

$container = [{username=>'Bill',address =>'ABC'},
{username=>'Jane',address =>'DEF'},
{username=>'John',address =>'HIJ'},
{username=>'Joe',address =>'KLM'},
];
ok($user->create( $utils->connect(),$container),"Execute Array add 4");
unless($user->result()->is_error) {
ok(scalar(@{$user->result()->set}) == 4,"Four recodes added");
}else{
fail("Execute Array failed");
}

Now I add in the code to create all my classes;

use Test::User;
my $class_container = [];
foreach my $item (@{$container}){
push(@{$class_container},Test::User->new($item ));
}

Now the problem I will encounter is that that DBD::DBM is very limited and it will not allow me to enter duplicate values into the 'username' field as it used this field as a default identity key.

To get around this I used this code


$user->dynamic_conditions([]);
$user->delete($utils->connect());

First I empty out my dynamic conditions and then do a simple delete on the DA. Now I just need this test;

ok($user->create( $utils->connect(),$class_container),"Execute Array add 4 from a class");
unless($user->result()->is_error) {
ok(scalar(@{$user->result()->set}) == 4,"Four records added");
}else{
fail("Execute Array failed for container of classes");
}

and believe it or not I got a 100% on the first run. I was very surprised at that. Then I remembered one of the neat little things about perl is that a class is really just a blessed hash-ref, so my code will just worked as is.

Now that gets me to thinking I could make my test cases a little less bloated if I just started with;


use Test::User;
$container = [Test::User->new({username=>'Bill',address =>'ABC'}),
              {username=>'Jane',address =>'DEF'},
              Test::User->new({username=>'John',address =>'HIJ'}),
              {username=>'Joe',address =>'KLM'},
              ];
Now I am testing execute_array, array-ref for a container, an hash-ref as an row in a container and finally a class as a row in a container. I made that little change and sure enough that worked so I guess I do not need the next tests as above and only the first one. Every day is a learning day here in the Moose-pen. I guess you can just now ignore the middle portion of this post as I just got rid of that code.

Not that it was not a total waste of time today as I noticed that the


$user->dynamic_conditions([]);
codes as a little clunky for me. It is very easy to forget the '[]' in the call to empty the attribute. Fortunately Moose Native::Traits lets me clean that up a little as I have the 'clear' trait to play wit.

The 'clear' empties out an array-ref which I just what I want to do. Back into Accessor.pm I go and add in a quick;


has dynamic_conditions => (
isa => 'ArrayRefofConditions',
traits => ['Array','MooseX::MetaDescription::Meta::Trait'],
description => { not_in_DAD => 1 },
is => 'rw',
default => sub { [] },
init_arg => undef,
handles => {
++ reset_conditions => 'clear',
add_condition => 'push',
dynamic_condition_count => 'count',
},
);

to that attribute and a similar one for the other 'dynamic' attributes. Now in the future I can just do

$user->reset_conditions();

a little cleaner API.

A Good day today, a few useful things accomplished;

ss80-9.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