No, when a role is applied it's methods are copied into the composing class.

In general you should look at roles as being closed or immutable. This train of thought can only lead to the end of civilization as we know it.

- Stevan


On Apr 22, 2010, at 9:27 PM, Dan Horne wrote:

Hi,


is there a way that I can dynanically add methods to a role that will be available to all classes that consume that role> Consider the something like
the following:

Singleton class:

use MooseX::Declare;
class My::Singleton {
   use MooseX::Singleton;
   has 'schema' => (is => 'ro', isa => 'App::Schema');
};

Role:

use MooseX::Declare;
role App::Role::Schema {
      use My::Schema
      method schema {
           return My::Singleton->schema
      }
}

In this case, the role simply provides a simple wrapper around the
Singleton's schema (i.e. a DBIx::Class schema). But now I'd like to provide shortcut methods for the DBIx::Class resultsets. In the past, I have done
something like this in a base class:

my @sources = $self->schema->sources;
foreach my $sub (@sources) {
   __PACKAGE__->meta->add_method(
       $sub => sub {
           return shift->schema->resultset($sub);
       }
   );
}

In other words, when the application is first instantiated, it creates a
schema object and makes it available to all other application classes
through App::Role::Schema. But I'd also like to add helper methods for each source class resultset at the app startup time to be made available to all
classes, and I'm not quite sure how to do this.

Dan

Reply via email to