Little Moose Goof

Well in my last post I made a little boo-boo. Well a few really. I wanted to get rid of my 'View' class as it was the same as my 'Element' class once I stripped out some parts. Unfortunately I discovered that I have to add in a number of other attributes to my 'Element' class which should never be part of the 'View' class. So I am going to backtrack on taking View out and put it back in.

Now my second boo-boo from my last post, I wanted to have first letter upper case names for my Accessor attributes like this


has View => (
is => 'rw',
isa => 'Object',
);

has Elements => (
isa => 'ArrayRef',
is => 'rw',
);


So I diligently made all the appropriate changes in my files and then had to change my DAD::SQL to take into account the missing 'sub retrieve', that pulled out in my last post. so I added in a new sub in DAD::SQL

sub element_sql {
my $self = shift;
my ($element) = @_;
if ( $element->alias() ) {
return $element->name() . " AS " . $element->alias();
}
else {
return $element->name();
}
}

and changed the _retrieve code a little to this

...
foreach my $element ( @{ $self->Elements() } ) {
$sql .= $delimiter . $self->element_sql($element);
$delimiter = ", ";
}
return $sql . " FROM " . $self->element_sql($self->View());
...

and being a good perl programmer before I got to far along I ran my tests and got

Can't call method "new" on an undefined value at 02_base_Accessor.t line 46.

Which is this line;


my $view = Database::Accessor::View->new(
{
name => 'person',
alias => 'me'
}
);

Now after a good deal of backpedaling and shifting things about and using a good number of expletives, I no longer tear at my hear as I have little left, I figured it out.

Seems 'Database::Accessor::View->new' is calling the attribute 'View' on Accessor.pm and not the 'new' on the 'Database::Accessor::View' class so I get that the 'new on an undefined' error.

I tried a few things to make this work such as moving Database::Accessor::View out of Accessor.pm and into is own file in the Database/Accessor dir but no luck. Oh well there goes that plan to do that, but maybe not.

Moose came to my rescue again as I have that 'Database::Accessor::Roles::DAD' role that I apply to my DADs and in that I left the attributes in first letter upper case like this


has View => (
is => 'ro',
isa => 'Object',
);

has Elements => (
isa => 'ArrayRef',
is => 'ro',
);


and a little change in Accessor.pm retrieve sub like this

--my $dad = $driver->new({view=>$self->view,
-- elements=>$self->elements});
++my $dad = $driver->new({View=>$self->view,
++ Elements=>$self->elements});

and leave the SQL and Mongo DADs in the first upper format and I get all my test passing.

Now this is not 100% of what I wanted but to the end user it give me exactly what I want, Moose comes though again.
badmoose.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