Use Test::Exception in this test
[gitmo/Mouse.git] / lib / Mouse / Util.pm
CommitLineData
4093c859 1package Mouse::Util;
2use strict;
3use warnings;
5c12655d 4use base qw/Exporter/;
d72006ca 5use Carp;
4093c859 6
eae80759 7our @EXPORT_OK = qw(
eae80759 8 get_linear_isa
eae80759 9);
10our %EXPORT_TAGS = (
11 all => \@EXPORT_OK,
12);
13
42d7df00 14BEGIN {
272a1930 15 my $impl;
bcd39bf4 16 if ($] >= 5.009_005) {
272a1930 17 $impl = \&mro::get_linear_isa;
18 } else {
f172d4e7 19 my $loaded = do {
20 local $SIG{__DIE__} = 'DEFAULT';
bcd39bf4 21 eval "require MRO::Compat; 1";
f172d4e7 22 };
272a1930 23 if ($loaded) {
24 $impl = \&mro::get_linear_isa;
25 } else {
26# VVVVV CODE TAKEN FROM MRO::COMPAT VVVVV
27 my $code; # this recurses so it isn't pretty
28 $code = sub {
29 no strict 'refs';
30
31 my $classname = shift;
32
33 my @lin = ($classname);
34 my %stored;
35 foreach my $parent (@{"$classname\::ISA"}) {
36 my $plin = $code->($parent);
37 foreach (@$plin) {
38 next if exists $stored{$_};
39 push(@lin, $_);
40 $stored{$_} = 1;
41 }
42 }
43 return \@lin;
44 };
45# ^^^^^ CODE TAKEN FROM MRO::COMPAT ^^^^^
46 $impl = $code;
eae80759 47 }
48 }
bcd39bf4 49
272a1930 50 no strict 'refs';
51 *{ __PACKAGE__ . '::get_linear_isa'} = $impl;
eae80759 52}
53
2e92bb89 54sub apply_all_roles {
55 my $meta = Mouse::Meta::Class->initialize(shift);
2e92bb89 56
21498b08 57 my @roles;
58 my $max = scalar(@_);
59 for (my $i = 0; $i < $max ; $i++) {
60 if ($i + 1 < $max && ref($_[$i + 1])) {
61 push @roles, [ $_[$i++] => $_[$i] ];
62 } else {
63 push @roles, [ $_[$i] => {} ];
64 }
65 }
66
67 foreach my $role_spec (@roles) {
68 Mouse::load_class( $role_spec->[0] );
69 }
70
71 ( $_->[0]->can('meta') && $_->[0]->meta->isa('Mouse::Meta::Role') )
72 || croak("You can only consume roles, "
73 . $_->[0]
74 . " is not a Moose role")
75 foreach @roles;
76
77 if ( scalar @roles == 1 ) {
78 my ( $role, $params ) = @{ $roles[0] };
79 $role->meta->apply( $meta, ( defined $params ? %$params : () ) );
80 }
81 else {
82 Mouse::Meta::Role->combine_apply($meta, @roles);
83 }
84
2e92bb89 85}
86
4093c859 871;
88
f38ce2d0 89__END__
90
91=head1 NAME
92
93Mouse::Util - features, with or without their dependencies
94
95=head1 IMPLEMENTATIONS FOR
96
97=head2 L<MRO::Compat>
98
99=head3 get_linear_isa
100
f38ce2d0 101=cut
102