>>>>> "Adam" == Adam Guthrie <[email protected]> writes:

    Adam> Hi,
    Adam> I've the following class

    Adam> package AG::Dirs;

    Adam> use Moose;
    Adam> use namespace::autoclean;
    Adam> use Path::Class::Dir;
    Adam> use MooseX::Types::Path::Class;

    Adam> has 'root_dir' => ( isa => 'Path::Class::Dir', is => 'ro', coerce => 
1,
    Adam> required => 1 );

    Adam> has [ qw/
    Adam>         upload_dir
    Adam>         dynamic_data_cache_dir
    Adam>         static_image_dir
    Adam>         user_image_dir
    Adam>     / ]
    Adam> => ( isa => 'Path::Class::Dir', is => 'ro', coerce => 1, lazy_build 
=> 1 );

    Adam> sub _build_update_dir { $_[0]->root_dir->subdir('upload') };
    Adam> sub _build_dynamic_data_cache_dir {
    Adam> $_[0]->root_dir->subdir('dynamic_data_cache') };
    Adam> sub _build_static_image_dir { $_[0]->root_dir->subdir('static_image') 
};
    Adam> sub _build_user_image_dir { $_[0]->root_dir->subdir('user_image') };

    Adam> __PACKAGE__-> meta->make_immutable;

    Adam> Is there a better way than the multiple _build functions?  A function 
that
    Adam> uses the attribute name perhaps?

my @meths = qw(upload dynamic_data_cache static_image user_image);
has [map { "${_}_dir" } @meths] => (
  isa => 'Path::Class::Dir',
  is => 'ro',
  coerce => 1,
  lazy_build => 1
);
has 'root_dir' => (
    isa => 'Path::Class::Dir',
    is => 'ro',
    coerce => 1,
    required => 1,
    handles => {
      map { ("_build_{$_}_dir" => [subdir => $_]) } @meths
    }
);

The question is, why do you need attributes instead of the plain curried
delegation? If you remove the attributes you can simplify into:

has 'root_dir' => (
    isa => 'Path::Class::Dir',
    is => 'ro',
    coerce => 1,
    required => 1,
    handles => {
      map { ("{$_}_dir" => [subdir => $_]) }
        qw(upload dynamic_data_cache static_image user_image)
    }
);

-- 
     Eden Cardim            Need help with your perl Catalyst or DBIx::Class 
project?
   Software Engineer                   http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.        Want a managed development or deployment 
platform?
http://blog.edencardim.com             http://www.shadowcat.co.uk/servers/

Reply via email to