Re: Role attributes

2009-05-27 Thread Stevan Little


On May 27, 2009, at 4:40 PM, Dimitri Ostapenko wrote:

Chris Prather wrote:
On Wed, May 27, 2009 at 4:24 PM, Dmitri Ostapenko > wrote:
Is there a way to tell which attributes come from role? I can't  
think of a
good way of doing this besides from changing attributes names in  
some way.

Using prefix for example..
tx,


What are you trying to achieve?

-Chris


I'm writing interface module for DB table (Pg)  that has close  
relationship with another table, but does not inherit from it as  
each row in main table can have multiple rows in base table. Class  
I'm writing needs to interact with both tables.


In case of pure postgres base tables, classes map to tables  
beautifully. Base table has corresponding role and child table has  
corresponding class. This clean mapping breaks if underlying tables  
don't have inheritance.


So using a role to model lower-level table  I  need to  be able to  
tell which attributes come from  class and which come from role in  
methods for saving and retrieving data.


Maybe base class would be better suited for something like this?


Perhaps. However using a custom meta-attribute would work too, like so  
(untested):


package MyCustomAttributeMarker;
use Moose::Role;

package SomeRole;
use Moose::Role;
has foo => ( traits => [ 'MyCustomAttributeMarker' ], ... );

package SomeOtherRole;
use Moose::Role;
has bar => ( traits => [ 'MyCustomAttributeMarker' ], ... );

package SomeClass;
use Moose;

with 'SomeRole', 'SomeOtherRole';

has baz => ( ... );

...

my @attrs_from_roles = grep { $_->does('MyCustomAttributeMarker') }  
SomeClass->meta->get_all_attributes;


Moose does not provide this by default because the whole purpose of  
roles is that they are composed into the class so you don't need to  
actually worry about where they came from.


- Stevan




Re: Role attributes

2009-05-27 Thread Hans Dieter Pearcey
On Wed, May 27, 2009 at 05:20:03PM -0400, Dimitri Ostapenko wrote:
> Ah, that would work! That is if I knew how to use traits :)  Guess will  
> have to figure it out

If you keep replies on-list, other people can benefit from this conversation in
the future.

There's a cookbook recipe for the kind of thing I'm suggesting.

http://search.cpan.org/~drolsky/Moose-0.79/lib/Moose/Cookbook/Meta/Recipe3.pod

hdp.


Re: Role attributes

2009-05-27 Thread Hans Dieter Pearcey
On Wed, May 27, 2009 at 05:16:28PM -0400, Dimitri Ostapenko wrote:
> # Need to know here which attr comes from where to be able to save into  
> correct table

Yes, I understand the problem.  That's why I suggested a role for your
attribute class so that you can say e.g.

has foo => (is => 'ro', ..etc.., from_table => "tablename");

hdp.


Re: Role attributes

2009-05-27 Thread Dimitri Ostapenko

Hans Dieter Pearcey wrote:

On Wed, May 27, 2009 at 04:40:37PM -0400, Dimitri Ostapenko wrote:
  
So using a role to model lower-level table  I  need to  be able to tell  
which attributes come from  class and which come from role in methods  
for saving and retrieving data.



Maybe you want a trait for your attributes that lets you store a table per
attribute definition, or something?

hdp.

!DSPAM:2337,4a1da61670674007936253!


  


Let's say we have table 'family' and table 'family_member'. Family has 
attributes:

 - size
 - family name
 - kind

family_member has:
 - sex
 - first_name
 - age


For a family of 3 there will be 3 rows in 'family_member' for 1 row in 
'family'.


package FamilyMember;

use Moose::Role;
use MY::Types;

has  'id' => (is => 'rw', isa => 'PosInt' );  
  # serial

has  'sex' => (is => 'ro', isa => 'SexType',required=>1);
has  'first_name' => (is => 'ro', isa => 'Str',required=>1 );
has 'age' => (is => 'ro', isa => 'PosInt', default=> 0 );
has 'family_id' => (is => 'ro', isa => 'PosInt', required=>1 ); # 
foreign key



package Family;
use Moose;
use MY::Types;

with qw(MY::Base MY::FamilyMember );

has  'id' => (is => 'rw', isa => 'PosInt' );
has  'size' => (is => 'ro', isa => 'PosInt', required=>1);
has  'family_name' => (is => 'ro', isa => 'Str', required=>1 );# 
Unique within a table

has 'kind' => (is => 'ro', isa => 'FamilyKind', default=> 'Traditional' );


sub _table { 'family'};
sub _base_table { 'family_member'};

# Return 3 rows from base table, table
sub get {
  my (%arg) = @_;
  my $id  = $arg{id};
  my $family_name;
  my  ($rows,$where);

 croak "'id' or 'family_name' is required" unless ($id || $family_name);

  if ( $id ) {
 $where = "id='$id'";
  } else {
 $where = "family_name='$family_name'";
  }

  my $qry = 'SELECT * FROM '. _table().' a,'. _base_table().' b WHERE 
a.id=b.family_id AND '.$where;

  my $rows = MY::DB::select (sql => $qry, returns => 'hash');

  return $rows 
}


sub save {

# Need to know here which attr comes from where to be able to save into 
correct table


}



Re: Role attributes

2009-05-27 Thread Hans Dieter Pearcey
On Wed, May 27, 2009 at 04:40:37PM -0400, Dimitri Ostapenko wrote:
> So using a role to model lower-level table  I  need to  be able to tell  
> which attributes come from  class and which come from role in methods  
> for saving and retrieving data.

Maybe you want a trait for your attributes that lets you store a table per
attribute definition, or something?

hdp.


Re: Role attributes

2009-05-27 Thread Dimitri Ostapenko

Chris Prather wrote:

On Wed, May 27, 2009 at 4:24 PM, Dmitri Ostapenko  wrote:
  

Is there a way to tell which attributes come from role? I can't think of a
good way of doing this besides from changing attributes names in some way.
Using prefix for example..


tx,




What are you trying to achieve?

-Chris

!DSPAM:2337,4a1da1e770674824671560!


  


I'm writing interface module for DB table (Pg)  that has close 
relationship with another table, but does not inherit from it as each 
row in main table can have multiple rows in base table. Class I'm 
writing needs to interact with both tables.


In case of pure postgres base tables, classes map to tables beautifully. 
Base table has corresponding role and child table has corresponding 
class. This clean mapping breaks if underlying tables don't have 
inheritance.


So using a role to model lower-level table  I  need to  be able to tell 
which attributes come from  class and which come from role in methods 
for saving and retrieving data.


Maybe base class would be better suited for something like this?









Re: Role attributes

2009-05-27 Thread Chris Prather
On Wed, May 27, 2009 at 4:24 PM, Dmitri Ostapenko  wrote:
> Is there a way to tell which attributes come from role? I can't think of a
> good way of doing this besides from changing attributes names in some way.
> Using prefix for example..
>
>
> tx,


What are you trying to achieve?

-Chris


Re: Role attributes

2009-05-27 Thread Hans Dieter Pearcey
On Wed, May 27, 2009 at 04:24:07PM -0400, Dmitri Ostapenko wrote:
> Is there a way to tell which attributes come from role? I can't think of  
> a good way of doing this besides from changing attributes names in some  
> way. Using prefix for example..

Why do you need to know?  I can't think of anything offhand that isn't a bad
idea.

hdp.