Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Created May 24, 2013 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/5643884 to your computer and use it in GitHub Desktop.
Save j1n3l0/5643884 to your computer and use it in GitHub Desktop.
use 5.014;
use Data::Dump 'pp';
use Test::Most;
package ErrorStack {
use MooseX::Role::Parameterized;
parameter _attributes_ => (
required => 1,
traits => ['Array'],
handles => { attributes => 'elements' },
init_arg => 'attributes'
);
role {
my $parameter = shift;
has _errors_ => (
traits => ['Array'],
default => sub { [] },
handles => { errors => 'elements', add_error => 'push' }
);
for my $attribute ( $parameter->attributes ) {
around $attribute => sub {
my ( $orig, $self, @args ) = @_;
return $self->$orig(@args) || $self->add_error($attribute);
};
}
};
};
package Person {
use Moose;
has [qw( name age gender job partner )] => ( is => 'ro' );
with 'ErrorStack' => { 'attributes' => [qw( job partner )] };
};
note 'All requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = @attributes;
my %options = ();
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
note 'No requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = ();
my %options = ( job => 'plumber', partner => Person->new );
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
note 'Some requested attributes return undef';
{
my @attributes = qw( job partner );
my @errors = qw( partner );
my %options = ( job => 'plumber' );
my $person = new_ok Person => [%options];
lives_ok { $person->$_ for @attributes }
'survives calls: ' . pp \@attributes;
is_deeply [ $person->errors ], [@errors], 'errors: ' . pp [@errors];
}
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment