And the Winner is? (well maybe anyway)

Today I started taking a closer look at MooseX::RelatedClasses to see if that could help me out. Well after reading though the documentation and looking at the test files to see how it really works I do not think I need this MooseX at all as it really is just for coordinating name-spaces, So I will move onto the next one for today and that is MooseX::Scaffold

This one does look promising as I can use one common sub to load in my LSDs when one of the DA CRUD commands are issued with all the correct attributes I need.

This one did call for a rather large redesign of how I put things together;

First I had to take the DA_S::LSD out the DA pm file and put it in its own file and in that I added the Scaffold code like this

package DA_S::LSD;

use Moose;
use MooseX::Scaffold;

my $class = shift;
my %given = @_;
$class->has($given{kind} => is=>'ro', isa=>'Object', required=>1);


Next I adjusted my reteive sub to this;

if ($conn eq 'DBH'){
use DA_S::LSD::SQL;
$lsd = DA_S::LSD::SQL->new(DA=>$self);

Now that is a little problematic as I have know Idea what my LSD writer will pick as a name and I cannot define it for them. However, I expected this as I would not know what Role name they picked as well so it has always been part of my evil plan to have the LSDs load via a namespace but that will be in a much later post.

Finally the part I had to adjust the most were my LSD roles. I had to convert them back to a Class and then add in the following;

use Moose;
with qw(DA::Roles::API);
use MooseX::ClassAttribute;
use DA_S::LSD kind => 'DA';

Now what should happen is when I create my new $lsd in DA_S the scaffolding in DA_S::LSD should kick in and then apply the 'DA' attribute to the DA_S::LSD::SQL with the correct instance value of the current DA. Guess what! this worked;
ok 8 - Address is a DA_S
ok 9 - SQL correct

as my test shows.

Now for the Mogo one I though that maybe I can work a little majick here. So in the Mongo I did this

use Moose;
with qw(DA::Roles::API);
use MooseX::ClassAttribute;
use DA_S::LSD elements=>'elements', view=>'view';

and changed the retrieve code a little to this

$sql .= $self->view()->name();
$sql .= ".find({},{";

foreach my $element ( @{ $self->elements() } ) {

$sql .= $delimiter . $element->name() . ": 1";
$delimiter = ", ";

and in my DA_S I did this

elsif ($conn eq 'MONGO'){
use DA_S::LSD::Mongo;
$lsd = DA_S::LSD::Mongo->new(elements=>$self->elements,view=>$self->view);

and in the DA_S::LSD this

my $class = shift;
my %given = @_;
$class->has($given{kind} => is=>'ro', isa=>'Object', required=>1)
if (exists($given{kind}) );

$class->has(elements => is=>'ro', isa=>'ArrayRef', required=>1)
if (exists($given{elements} ));

$class->has(view => is=>'ro', isa=>'Object', required=>1)
if (exists($given{view} ));

and all my tests incluging the Mongo passed

ok 9 - SQL correct
ok 10 - Mongo Query correct

So now I have a very quick way without iteration to get the Element, View and any other DA level attributes into my LSD instances.

Ok this this one I am going to keep around despite a few problems like needing to have the LSD classes in my DA class and trying to ensure that my LSD writer adds in the proper scaffolding call to DA_::LSD. I am sure I can find some was to fix those up though either though a role or at BUILD time.

Well onto the next one tomorrow.

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