Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

The best Moo::Role way to do an alias to 'after BUILD'

by celogeek (Initiate)
on Jan 04, 2013 at 17:05 UTC ( #1011683=perlquestion: print w/ replies, xml ) Need Help??
celogeek has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to create a role that will be a based for validators. In my 'CreativesValidator' I have this method :
#only there for the validator below which do stuff after build. sub BUILD {} has '_creative_validators' => ( is => 'ro', default => sub { [] }, ); sub register_validator { my ($class, $sub) = @_; $class->can('after')->(BUILD => sub { my ($self) = @_; push @{$self->_creative_validators}, $sub; }); }
So here a validator : (ForcedPause)
use Moo::Role; with 'Weborama::Collect::CreativesValidator'; #you have to define that one requires qw/creatives_validator_publisher_forced_pause_settings/; __PACKAGE__->register_validator(sub { my ( $self, @creatives ) = @_; ... })
is they a cleaner way to do that ? I really want to avoir the copy paste of the register_validator method everywhere. I will have 50 filters or more.

Comment on The best Moo::Role way to do an alias to 'after BUILD'
Select or Download Code
Re: The best Moo::Role way to do an alias to 'after BUILD'
by tobyink (Abbot) on Jan 04, 2013 at 20:57 UTC

    Maybe something like...

    { package Weborama::Collect::CreativesValidator; use Moo::Role; use MooX::ClassAttribute; class_has _registered_validators => ( is => 'ro', default => sub { [] }, ); has _creative_validators => ( is => 'ro', default => sub { [] }, ); sub register_validator { my $class = shift; push @{$class->_registered_validators}, @_; } after BUILD => sub { my $self = shift; my $class = ref $self; push @{$self->_creative_validators}, @{$class->_registered_validat +ors}; }; } { package Weborama::ForcedPause; use Moo::Role; with 'Weborama::Collect::CreativesValidator'; # you have to define that one requires qw/creatives_validator_publisher_forced_pause_settings/; __PACKAGE__->register_validator(sub { my ( $self, @creatives ) = @_; ... }); }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011683]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2015-07-04 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls