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