Low Stress Moose

So today in the Moose-pen I am going to move onto something a little differtn and that is my Accessor 'links' attribute which I am going to cover with the 35_links.t test suite.

I did a quick review of the bits and pieces I had stubbed in so far and I notices I need a few thing. First had a look at my Accessor 'links' attribute and I will need a new type for that. So to get this new 'ArrayRefofLinks' type I just did what we have seen in a number of older posts, namely add in a 'use', a 'class_type' and a 'subtype' into my Types.pm, I will hold off on a coercion for now till I get a little deeper into my post. So that takes case of 'links'.

I also noticed that 'Links' attribute of my DAD now had the wrong type as well so that was change to the 'ArrayRefofLinks' type

Looking at my 'Database::Accessor::Link' class I have never liked this two attributes


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)]
);


Now I need these because I know in SQL you can have a complicated join where you may want to join one of the tables you are joining on to a third table perhaps like this

 select person.name,agent.name
from person
left join on listing where person.listing_id = listing.id
left join on agent where listing.agent_id = agent.id

Now I think I can get rid of that very ugly 'to_alias' attribute by just changing the type of 'to' over to just an View class then I get both a 'name' and an optional 'alias' attribute. So now I have


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

You will see that I added in the 'coerce' on the attribure. If I left that out then I would get an error like this;

Attribute (to) does not pass the type constraint 
because: Validation failed for 'View' with value HASH(0x559084c) (not isa
Database::Accessor::View) at C

If I just passed a hash for this attribute. Always remember to do this it will save you great deal needless debugging.

Now to test this I will need a new hash like this


my $in_hash = {
view => {name => 'People'},
elements => [{ name => 'first_name',
view=>'People' },
{ name => 'last_name',
view => 'People' },
{ name => 'user_id',
view =>'People' },
{ name=>'name',
view=>'country'} ],

links=>[{to =>{name=>'country',
alias=>'a_country'},
type =>'Left',
conditions =>[{left =>{name =>'country_id',
view =>'People'},
right =>{name=>'id',
view=>'a_country'},
operator =>'=',
}]},
],
};

Here I unlike my 'conditions' attribute in my API I am not dropping the predicates/conditions param as I think it is a good Idea to leave it in. The coercion for this subtype is just like most of the others

coerce 'ArrayRefofLinks', from 'ArrayRef', via {
[ map { Database::Accessor::Link->new($_) } @$_ ];
};

but I think I will expand on that as there are many many time when there is just one join so lets try this

coerce 'ArrayRefofLinks', from 'ArrayRef', via {
[ map { Database::Accessor::Link->new($_) } @$_ ];
},
from 'HashRef', via {
return [Database::Accessor::Link->new($_) ];
};

and now I can do this

links=>{to =>{name=>'country',
alias=>'a_country'},
type =>'Left',
conditions =>{left =>{name =>'country_id',
view =>'People'},
right =>{name=>'id',
view=>'a_country'},
operator =>'=',
}},

I am not going to bore you with all the testing details lets just say things came out Moosey OK.

2097002_1.png

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