Late Night Moose

Well in today's Moose-pen I am still going to play about with good old 33_condtions.t, not the tests per-say but the API they are calling. In my last post I fixed the my API call by adding in the 'predicates' param to the 'conditions' param like this


...
view =>'People' } ],
conditions=>[{predicates=>[{left=>{name =>'last_name',
view =>'People'},
...

to me the above is a little wordy an error prone. The original API call I attempted


view =>'People' } ],
conditions=>[{left =>{name =>'last_name',
view =>'People'},...

in this post was nicer. Now how to achieve that??

Well I could use 'around BUILDARGS' call like I did for various 'read' and 'write' flags from this post. With the 'around' I could then manipulate what is coming and return what I want. Now the problem with that is I would get no class level re-use from it. Each class that used the same pattern or in this case type of 'ArrayRefofConditions', would require it own 'around' clause.

Now Moose comes to my rescue yet again (I really am wearing that phrase out, aren't I), as I can play with the coercion to get what I want.

My present statement is


coerce 'ArrayRefofConditions', from 'ArrayRef', via {
[ map { Database::Accessor::Condition->new($_) } @$_ ];
};

If you notice in the above I have access to the '@$_' which gives me all the input params. I can then take this and iterate over it if I need like this

coerce 'ArrayRefofConditions', from 'ArrayRef', via {
my $objects = [];
foreach my $object (@$_) {
push( @{$objects}, Database::Accessor::Condition->new({predicates=>[$object]}) );
}
return $objects
};

So in the above I create an array-ref to hold the objects I am returning then do my iteration over my '@$_' and at each object I do the new on the 'Condition' class with the 'predicates' param and push that into the $objects which I just return at the end.

Now I had to adjust my test a bunch, to accommodate the new API and once I finished that I re-run my test and I get

ok 1 - use Database::Accessor;
ok 2 - Got the Test DAD
ok 3 - DA predicates correct
ok 4 - DAD predicates no 0 correct
ok 5 - DA predicates correct
ok 6 - DAD predicates no 1 correct

and I am back in the saddle again.
munchies-e1349895645408.png

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