R – Moose or Meta


I've been trying to do this a number of ways, but none of them seem graceful enough. (I'm also wondering if CPAN or Moose already has this. The dozens of searches I've done over time have shown nothing that quite matches.)

I want to create a type of class that

  • is a Base + Facade + Factory for other classes which load themselves as destination types.
  • The "factory" is just Base->new( %params ) and this creates types based on policies registered by the individual subclass.
  • Each subclass knows basic things about the domain of the Base class, but I'm trying to keep it minimal. See the example below: UnresolvedPath just knows that we should check for existence first.

The obvious example for this is file system directories and files:

package Path;
use Moose;


sub BUILD { 
    my ( $self, $params ) = @_;
    my $path = $params->{path};

    my $class_name;
    foreach my $test_sub ( @tests ) { 
        $class_name = $test_sub->( $path );
        last if $class_name;
    croak "No valid class for $path!" unless defined $class_name;
    $class_name->BUILD( $self, $params );

package Folder; 
use Moose;

extends 'Path';

use Path register => selector => sub { -d $_[0] };

sub BUILD { ... }

package UnresolvedPath; 

extends 'Path';

use Path register position => 1, selector => sub { !-e $_[0] };
  • Question: Does Moose provide a graceful solution to this? Or would I have to go into Class::MOP for the bulk?

Best Solution

Have a peek at http://code2.0beta.co.uk/moose/svn/MooseX-AbstractFactory/ and feel free to steal. (Mine.)

Related Question