Moose at the edge

Its get back on track day here Moose pen.

I left off with the add_sort and did some refracting that when a little awry but I did get back on track. As it stands I have this original


sub _element_coerce {
my ($hash) = @_;
my $object;
if ( exists( $hash->{expression} ) ) {
$hash->{expression} = uc( $hash->{expression} );
$object = Database::Accessor::Expression->new( %{$hash} );
}
elsif ( exists( $hash->{function} ) ) {
$hash->{function} = uc( $hash->{function} );
$object = Database::Accessor::Function->new( %{$hash} );
}
elsif ( exists( $hash->{value} ) || exists( $hash->{param} ) ) {
$object = Database::Accessor::Param->new( %{$hash} );
}
elsif ( exists( $hash->{ifs} ) ) {
die "Attribute (ifs) does not pass the type constraint because:
Validation failed for 'ArrayRefofThens' with less than 2 ifs"
if ( exists( $hash->{ifs} )
and ref( $hash->{ifs} ) eq 'ARRAY'
and scalar( @{ $hash->{ifs} } < 2 ) );
$object = Database::Accessor::If->new( %{$hash} );
}
else {
if ( exists( $hash->{left} ) or exists( $hash->{right} ) ) {
my %copy = %{ Clone::clone($hash) };
delete( $copy{left} );
delete( $copy{right} );
$LAST = \%copy;
}
$object = Database::Accessor::Element->new( %{$hash} );
}
return $object;
}

So now I have to change a few thing above to accept the new way I am using '_create_instance' so I rebuilt it like this (including accounting for an 'undef' from a few days ago'). My rewrite is below;

sub _element_coerce {
my ($hash) = @_;
my $class ="Database::Accessor::Element";
my %copy = ($hash) ? %{ Clone::clone($hash) } : ();
unless ($hash) {
my ( $package, $filename, $line, $subroutine ) = caller(4);
my $add = substr($subroutine,4,length($subroutine));
$ALL_ERRORS->add_error(MooseX::Constructor::AllErrors::Error::Misc->new(
{
message => "Database::Accessor "
. $subroutine
. " Error:\n"
. "You cannot add undef to dynamic_"
. $add
. "! "
}
));
die _one_error($ALL_ERRORS, $hash, $subroutine, $package, $filename, $line,$subroutine, $NEW,$hash);
}
elsif ( exists( $hash->{expression} ) ) {
$hash->{expression} = uc( $hash->{expression} );
$class = "Database::Accessor::Expression";
}
elsif ( exists( $hash->{function} ) ) {
$hash->{function} = uc( $hash->{function} );
$class = "Database::Accessor::Function";
}
elsif ( exists( $hash->{value} ) || exists( $hash->{param} ) ) {
$class = "Database::Accessor::Param";
}
elsif ( exists( $hash->{ifs} ) ) {
die "Attribute (ifs) does not pass the type constraint because:
Validation failed for 'ArrayRefofThens' with less than 2 ifs"
if ( exists( $hash->{ifs} )
and ref( $hash->{ifs} ) eq 'ARRAY'
and scalar( @{ $hash->{ifs} } < 2 ) );
$class = "Database::Accessor::If";
}
else {
if ( exists( $hash->{left} ) or exists( $hash->{right} ) ) {
delete( $copy{left} );
delete( $copy{right} );
}
}
my $object = _create_instance($class,$hash,4,\%copy);
return $object;
}

and when I give this a go;


$da->add_sort(undef);

I get;

Database::Accessor Database::Accessor::add_sort Error:
You cannot add 'undef' with Database::Accessor::add_sort at 42_new_validation.t line 91

so very close again. In the above the only real change I had to introduce was on the 'undef' situation. In that part of the block I just add a new 'Misc' error to the '$ALL_ERRORS' and then just die on the '_one_error'

Now I am going to skip a bit and try this;


$da->add_link(undef);

which I want to give me a error like above and to get this all I had to do was change the '_link_array_or_object' sub a little;

++ my $instance = _create_instance('Database::Accessor::Link',$object,4,$object);
++ push( @{$objects}, $instance);
-- push( @{$objects}, Database::Accessor::Link->new($object) );

and now I get;

Database::Accessor Database::Accessor::add_link Error:
You cannot add 'undef' with Database::Accessor::add_link at 42_new_validation.t line 92

Now for this one

$da->add_gather(undef);

As 'gather' is a singleton I have to do the die back in the Accessor.pm like this

sub add_gather {
my $self = shift;
my ($gather) = @_;
++ die "Database::Accessor add_gather Error:\n You cannot add undef with add_gather!"
++ unless(defined($gather));
$self->dynamic_gather($gather);
}

and I get;

Database::Accessor add_gather Error:
You cannot add undef with add_gather! at D:\GitHub\database-accessor\lib/Database/Accessor.pm line 512
Database::Accessor::add_gather('Database::Accessor=HASH(0x38b6134)', undef) called at 42_new_validation.t line 9

close to what I want.

At least I know my pattern is working. Now to put this into full practice, but that is a post for tomorrow.

817L9kbd9yL._SX425_.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