Can a Moose Juggle?

The next class in the Moose-pen is Database::Accessor::Link which would represent a 'Join' in SQL and a I think they call it a 'Pipe Lookup' in Mongo or it could mean a find on a document buried under the parent document. I guess that is a topic for a later post.

The first thing I noticed was this class and the 'Condtion' class I looked at yesterday is they share the same 'predicates' attribute. So being a good Mooser the first thing to day is to pull that attribute out and add it into a role.

So after a few copy and pastes and a little typing I have


{
package Database::Accessor::Roles::PredicateArray;
BEGIN {
$Database::Accessor::Roles::PredicateArray = "0.01";
}
use Moose::Role;
use MooseX::Aliases;

has predicates => (
traits => ['Array'],
is => 'rw',
isa => 'ArrayRefofPredicates',
coerce => 1,
alias => 'conditions',
handles => {
_add_predicate => 'push',
count_predicates => 'count',
},
);
1;
}

and in my Condition class I just add in the role

with qw(Database::Accessor::Roles::Alias
++ Database::Accessor::Roles::PredicateArray
);

That simple. Now onto the Link class proper. First I add in the new role and my first Attribute 'view'

package Database::Accessor::Link;
use Moose;
with qw(Database::Accessor::Roles::Alias
++ Database::Accessor::Roles::PredicateArray);
use MooseX::Aliases;

has view => (
is => 'rw',
isa => 'Str',
required => 1,
alias => 'to'
);


So this view is what you are going to link to so I also added in the alias 'to' and after update my 11_link.t test case a little I gave it a run and got

You are overwriting a locally defined function (alias) with
an accessor at C:/Dwimperl/perl/site/lib/Moose/Meta/Attribute.pm line 1111
Moose::Meta::Attribute::_process_accessors('Moose::Meta::Attribute=HASH(0x5732ed4)', 'accessor', 'alias', undef)
….

Opps what is going on here.

In this case my use MooseX::Aliases is conflicting with the 'alias' accessor I have imported with the Alias role. Now I was importing that alias role as I want my view attribute to have an alias so I can do stuff like this in SQL

LEFT JOIN users invited_by ON project_user.invited_by_user_id = invited_by.id

So I am not dead in the water yet I just need to change back over to my 'Base' role and then add rename a few things and add in that renamed 'alias' attribute. So with this

 
has to => (
is => 'rw',
isa => 'Str',
required => 1,
alias => [qw( to_view view)]
);

has to_alias => (
is => 'rw',
isa => 'Str',
alias =>[qw( alias view_alias)]
);


I can have my cake and eat it as well. So I start with two new attribute names and in my alias part I add back in the original two I wanted 'view' and 'alias' and here is the test to prove it

my $link = Database::Accessor::Link->new({ view=>'test',
alias=>'new_test',
predicates=>[{left=> {name=>'field-1',

ok( $link->view eq 'test',"view is 'test'");
ok( $link->alias eq 'new_test',"alias is 'new_test'");

and the result

… ok 9 - view is 'test' ok 10 - alias is 'new_test'
Now the last attribute to add in is what type of link or Join I want to do.

    has type => (
        is       => 'rw',
        isa      => 'Link',
        required => 1,
    );
and again I added in a new type called 'Link' and just followed the same pattern that I used for 'Aggregate' and 'Operator' types so no need to repeat that. For now I just added the baic left, right and outer, I may add more later., Finally I updated my test like this

my $link = Database::Accessor::Link->new({ view=>'test',
++                                          link=>'LEFT',
                                          alias=>'new_test',
…

ok( $link->alias eq 'new_test',"alias is 'new_test'");
++ok( $link->type eq 'LEFT',"type is 'left'");


and got


ok 11 - type is 'left'

Moose comes though again.
Viles-0716-Buddies--600x900.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