Ricardo SIGNES > MooseX-ClassCompositor-0.004 > MooseX::ClassCompositor



Annotate this POD



Open  0
View/Report Bugs
Module Version: 0.004   Source   Latest Release: MooseX-ClassCompositor-0.008


MooseX::ClassCompositor - a factory that builds classes from roles


version 0.004


  my $comp = MooseX::ClassCompositor->new({
    class_basename  => 'MyApp::Class',
    class_metaroles => {
      class => [ 'MooseX::StrictConstructor::Trait::Class' ],
    role_prefixes   => {
      ''  => 'MyApp::Role::',
      '=' => '',

  my $class = $comp->class_for( qw( PieEater ContestWinner ) );

  my $object = $class->new({
    pie_type => 'banana',
    place    => '2nd',


A MooseX::ClassCompositor is a class factory. If you think using a class factory will make you feel like a filthy "enterprise" programmer, maybe you should turn back now.

The compositor has a "class_for" method that builds a class by combining a list of roles with Moose::Object, applying any supplied metaclass, and producing an arbitrary-but-human-scannable name. The metaclass is then made immutable, the operation is memoized, and the class name is returned.

In the "SYNOPSIS" above, you can see all the major features used: class_metaroles to enable strict constructors, role_prefixes to use String::RewritePrefix to expand role name shorthand, and class_basename to pick a namespace under which to put constructed classes.

Not shown is the "known_classes" method, which returns a list of pairs describing all the classes that the factory has constructed. This method can be useful for debugging and other somewhat esoteric purposes like serialization.



This attribute must be given, and must be a valid Perl package name. Constructed classes will all be under this namespace.


This attribute, if given, must be a hashref of class metaroles that will be applied to newly-constructed classes with Moose::Util::MetaRole::apply_metaroles.


This attribute stores a mapping of class names to the parameters used to construct them. The known_classes method returns its contents as a list of pairs.


This attribute is used as the arguments to String::RewritePrefix for expanding role names passed to the compositor's class_for method.


This attribute may be initialized with an arrayref of role names. These roles will always be composed in the classes built by the compositor.

These names will be rewritten by the role prefixes.



  my $class = $compositor->class_for(

    'Role::Name', # <-- will be expanded with role_prefixes

      'Param::Role::Name', #  <-- will be expanded with role_prefixes
      'ApplicationName',   #  <-- will not be touched
      { ...param... },

This method will return a class with the roles passed to it. They can be given either as names (which will be expanded according to "role_prefixes") or as arrayrefs containing a role name, application name, and hashref of parameters. In the arrayref form, the application name is just a name used to uniquely identify this application of a parameterized role, so that they can be applied multiple times with each application accounted for internally.

Note that at present, passing Moose::Meta::Role objects is not supported. This should change in the future.


Thanks to Pobox.com for sponsoring the development of this library.



This software is copyright (c) 2011 by Ricardo Signes.


syntax highlighting: