Moose Cracks Case!

It still stuck on Case here in the Moose-Pen

Same sort of story as my last post I as looking at my tests and just cleaning up a few typos and a little perl tidy and I notice the when I have a 'case' like this;


whens => [
{
left => { name => 'Price', },
right => { value => '10' },
operator => '<',
statement => { value => 'under 10$' }
},
[
{
left => { name => 'Price' },
right => { value => '10' },
operator => '>=',
},
{
condition => 'and',
left => { name => 'Price' },
right => { value => '30' },
operator => '<=',
statement => { value => '10~30$' }
},
],
{ statement => { value => 'Over 100$' } },
]

I translate that into a flat array-ref of 'When' classes like this;

[ 'Database::Accessor::Case::When' ,
'Database::Accessor::Case::When',
'Database::Accessor::Case::When',
'Database::Accessor::Case::When'
],

which is fine but what I am doing here is making it hard on myself when I move into the DAD side of the code. I will need to but in some funky logic to tell the difference between the first 'when' with one condition and the second 'when' that has two conditions.

What that flattening does is wipe out the distinction between the two clauses and I will have to add it in either though, as I mentioned above, logic in my DAD or some extra keys to identify a new 'when' block. Well I do not like either of those so I will just fix my Type coercion so the proper model as described in the Prototype hash-ref is passed into the DAD.

What I want is;


[ 'Database::Accessor::Case::When' ,
[ 'Database::Accessor::Case::When',
'Database::Accessor::Case::When',]
'Database::Accessor::Case::When'
],

in that 'whens' array ref then I can easily discern in my DAD when a when starts and stops.

Well first I will add a few tests to the '15_case.'t case to check for the above


ok( ref( $case->whens->[0] ) eq 'Database::Accessor::Case::When',
"Cases[0] is a when" );
++ok( ref( $case->whens->[1] ) eq 'ARRAY',
"Cases->whens->[1] is an array-ref" );
++ok( ref( $case->whens->[1]->[0] ) eq 'Database::Accessor::Case::When',
"Cases->whens->[1]->[0] is a when" );

and that should cover the change I want to make. Now for the change which will have to start in the '_when_array_or_object' sub and completely change the way I handle array-refs;
 
}
elsif ( ref($object) eq "ARRAY" ) {
my $sub_objects = [];
foreach my $sub_object (@{$object}){
push(
@{$sub_objects},
@{ _when_array_or_object( $sub_object ) });
}
push(
@{$objects},
@{ $sub_objects });
}
else {

In the above I loop though add the $sub_objects in array-refs and add each to a new array-refs, after I recourse down on each. I then add that new array-refs to the array-ref I am going to return;

So here we go;


Not an ARRAY reference at D:\GitHub\database-accessor\lib/Database/Accessor/Types.pm line 221.

hmm I think I need to pass only an array-refs in that foreach loop


 foreach my $sub_object (@{$object}){
                push(
                @{$sub_objects},
--              @{ _when_array_or_object( $sub_object ) });
++              @{ _when_array_or_object( [$sub_object ]) });
            }
on the next run I get;

not ok 7 - Cases->whens->[1]  is an array-ref
…
so that solved the problem but I still get a fail. I then spent a little time looking at that 'ARRAY' section and I tried this small change

 push(
                @{$objects},
--              @{$sub_objects});
++             $sub_objects );
and now I just push the new 'array-ref' on there and now I get;

Attribute (whens) does not pass the type constraint because: Validation failed for 
'ArrayRefofWhens' with value [ Database::Accessor::Case::When{ close_parentheses: 
0, condition: undef, left: Database::Accessor::Element=HASH(0x429232c), 
open_parentheses: 0, operator: "<", right: 
...
which I means I have a partial success but I need to look at my sub-type;

subtype 'ArrayRefofWhens'           => as 'ArrayRef[When]'; 
and I will have to expand on that type to allow for an 'Array-refs' to be present as well as 'When'

--subtype 'ArrayRefofWhens'           => as 'ArrayRef[When]'; 
++subtype 'ArrayRefofWhens'           => as 'ArrayRef[When|ArrayRef]'; 
and now I get;

ok 6 - Cases->whens->[0]  is a when
ok 7 - Cases->whens->[1]  is an array-ref
ok 8 - Cases->whens->[1]->[0]   is a when
Success! I guess it is a good Wednesday after all.

_MG_3370a.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