Baby Moose in a Pen

Still stuck in the Accessor.pm in the Moose-pen today.

Since I decided in yesterday's post to make one more round of changes I am a little closeer to getting back to Driver::DBI but not much.

For today's changes I decided to go full out and re-factor a good chunk of the common code between Accessor.pm and any DAD that I might write. Not to bore you with reamls of code pasting you will never read I will give you the over-view.

First action was to strip out the attributes from Database::Accessor::Roles::Driver and Database::Accessor that where the same and put them into a new role 'Database::Accessor::Roles::Common'. My Driver Role is now just a few lines


package
Database::Accessor::Roles::Driver;

use Moose::Role;
with qw(Database::Accessor::Types
Database::Accessor::Roles::Common);
use namespace::autoclean;
requires 'DB_Class';
requires 'execute';


The first problem I had to fix after I moved all the common elements between Accessor.pm and a Driver was this;

Can't locate object method "view" via package "Database::Accessor" at 20_dad_load.t line 18.

The fix was simple enough really, just move the new 'Database::Accessor::Roles::Common' to the start of the Accessor.pm file. The only nasty thing with this is some Nosy-Parker who has a peek inside the code-base may become a little confused as the first package is not 'Database::Accessor' , but I can live with that.

What I had to next was clean up the 'Common' role so that all the attributes had a default value of an empty hashref in order to stop this error;


Attribute (links) does not pass the type constraint because: Validation failed for 'ArrayRefofLinks' with value undef at C:\Dwimperl\perl\site\lib\Moose\Object.pm line 24
Moose::Object::new('Database::Accessor::Driver::Test', 'HASH(0x61627c4)') called at D:\GitHub\database-accessor\lib\Database\Accessor.pm line 645
Database::Accessor::__ANON__('Database::Accessor=HASH(0x6146794)', 'CREATE', 'Data::Test=HASH(0x615ad44)', 'HASH(0x613c184)', undef) called at

easy really just a slight change like this

has links => (
is => 'ro',
isa => 'ArrayRefofLinks',
traits => ['Array'],
handles => { link_count => 'count', },
++ default => sub { [] },
);

to each, Next was this

not ok 1 - Role DAD can dynamic_sorts

and to clean this one up I had to add this;

has dynamic_sorts => (
isa => 'ArrayRefofElements',
__ traits => ['Array'],
++ traits => ['Array','MooseX::MetaDescription::Meta::Trait'],
++ description => { not_in_DAD => 1 },
is => 'rw',
default => sub { [] },
init_arg => undef,
handles => {
add_sort => 'push',
dynamic_sort_count => 'count',
},
);

on all those dynamic_ attributes found in Accessor.pm as they are no longer in the DAD.

Finally I had to clean up the call out to the DAD class by eliminating all those 'dynamic_' attributes and combining the 'static' and 'dynamic' attributes coming from Accessor.pm and going into the DAD as in this example


– conditions => $self->conditions,
– dynamic_conditions => $self->dynamic_conditions,
++ conditions => [@{$self->conditions},@{$self->dynamic_conditions}],

and a little clean up on the counts for some tests and I have the first round of clean up done.

Now only to get rid of those pesky dynamic_ attributes in my other tests and then finally write up a test for all those element flags and I might be ready to get back to Driver::DBI but that is another post.

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