And one more for luck

For today's MooseX I was having a look at  MooseX::ShortCut::BuildInstance which was one of the last ones on my initial list.

So this MooseX required me to almost as much jigging about as in my last post and I had a few false starts on it implementation as the documentation is sparse as are examples, but none the less I did manage to get something up and running in my DA.

So to start I again has my DLS class in a separate file and I set that up as the shortcut maker. So all I needed to do was this


package DA_SC::LSD;
use Moose;
use MooseX::ShortCut::BuildInstance qw( build_instance should_re_use_classes);
should_re_use_classes( 1 );

and then in my retrieve sub of DA I did this

my $driver;
if ($conn eq 'DBH'){
$driver= "DA_SC::LSD::SQL";
}
elsif ($conn eq 'MONGO'){
$driver= "DA_SC::LSD::Mongo";
}
my $lsd = DA_SC::LSD::build_instance(
package => "LSD::$conn",
superclasses =>['DA_SC::LSD'],
roles =>[$driver,'DA::Roles::API'],);

I then had to covert my former LSD classes back into roles but I noticed that I would have to expand the DA_SC::LSD class like this

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

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


and then back in retrieve add this

my $lsd = DA_SC::LSD::build_instance(
package => "LSD::$conn",
superclasses =>['DA_SC::LSD'],
roles =>[$driver,'DA::Roles::API'],
view=>$self->view,
elements=>$self->elements );

and in my SQL and https://github.com/byterock/DA-blog/blob/master/lib/DA_SC/LSD/Mongo.pm role I had to change their scrip a little as they now had an view and element s attributes and then all my test run.

What I also discovered was I could move the LSD class back into the DA file and save a file and a use statement.

Now some of the details. In the build_instance call I am creating a new name-space 'LSD::XXX' and that keeps the differing LSDs out of each others and DA programming space. As well on my DA_SC::LSD I set up the attributes to be read only so a programmer writing a LSD can not play about with the the DA attributes. This was one of the snags I ran into when coding this up as I kept getting


The old class definitions will be overwritten with args:...

errors until I figured out the name-space part on the package.

So this was one of the easier ones to use, or I am just getting use to this type of programing and one of the nice things I like about it is the created class sticks about in the present perls name-space so It is only ever loaded once.

Cool.

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