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 <dmi...@farematrix.com > 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


Reply via email to