Skip to content

Instantly share code, notes, and snippets.

@fgabolde
Created April 5, 2013 14:40
Show Gist options
  • Save fgabolde/5319787 to your computer and use it in GitHub Desktop.
Save fgabolde/5319787 to your computer and use it in GitHub Desktop.
Baking roles into Moose classes.
use strict;
use warnings;
use 5.012;
use Carp;
use Moose;
use Moose::Util qw/apply_all_roles with_traits/;
use Test::More;
{
package Horse;
use Moose;
sub is_horned {
return 1 if shift->DOES('Horned');
return;
}
}
{
package Horned;
use Moose::Role;
}
my $unicorn_class = with_traits('Horse', 'Horned');
my $unicorn = Horse->new;
isa_ok($unicorn, 'Horse');
ok(!$unicorn->is_horned, q{... and it is not horned yet});
apply_all_roles($unicorn, 'Horned');
isa_ok($unicorn, 'Horse');
ok($unicorn->is_horned, q{... and it has a horn now});
my $real_unicorn = $unicorn_class->new;
isa_ok($real_unicorn, 'Horse');
isa_ok($real_unicorn, $unicorn_class);
ok($real_unicorn->is_horned, q{... and it has a horn from birth});
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment