File Coverage

blib/lib/MooseX/MethodAttributes/Role/Meta/Role/Application.pm
Criterion Covered Total %
statement 23 24 95.8
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 33 35 94.2


line stmt bran cond sub pod time code
1             package MooseX::MethodAttributes::Role::Meta::Role::Application;
2             # ABSTRACT: generic role for applying a role with method attributes to something
3              
4             our $VERSION = '0.31';
5              
6 21     21   20253 use Moose::Role;
  21         73  
  21         203  
7 21     21   105457 use Moose::Util qw/find_meta/;
  21         46  
  21         163  
8 21     21   3741 use MooseX::MethodAttributes ();
  21         42  
  21         451  
9 21     21   109 use MooseX::MethodAttributes::Role ();
  21         53  
  21         399  
10 21     21   102 use Carp qw/croak/;
  21         59  
  21         1271  
11 21     21   109 use namespace::autoclean;
  21         62  
  21         324  
12              
13             requires qw/
14             _copy_attributes
15             apply
16             /;
17              
18             #pod =method apply
19             #pod
20             #pod The apply method is wrapped to ensure that the correct metaclasses to hold and propagate
21             #pod method attribute data are present on the target for role application, delegates to
22             #pod the original method to actually apply the role, then ensures that any attributes from
23             #pod the role are copied to the target class.
24             #pod
25             #pod =cut
26              
27             around 'apply' => sub {
28             my ($orig, $self, $thing, %opts) = @_;
29             $thing = $self->_apply_metaclasses($thing);
30              
31             my $ret = $self->$orig($thing, %opts);
32              
33             $self->_copy_attributes($thing);
34              
35             return $ret;
36             };
37              
38             sub _apply_metaclasses {
39 15     15   36 my ($self, $thing) = @_;
40 15 100       129 if ($thing->isa('Moose::Meta::Class')) {
    50          
41 13         199 $thing = MooseX::MethodAttributes->init_meta( for_class => $thing->name );
42             }
43             elsif ($thing->isa('Moose::Meta::Role')) {
44 2         41 $thing = MooseX::MethodAttributes::Role->init_meta( for_class => $thing->name );
45             }
46             else {
47 0         0 croak("Composing " . __PACKAGE__ . " onto instances is unsupported");
48             }
49              
50             # Note that the metaclass instance we started out with may have been turned
51             # into lies by the metatrait role application process, so we explicitly
52             # re-fetch it here.
53              
54 15         3871 return find_meta($thing->name);
55             }
56              
57             1;
58              
59             __END__
60              
61             =pod
62              
63             =encoding UTF-8
64              
65             =head1 NAME
66              
67             MooseX::MethodAttributes::Role::Meta::Role::Application - generic role for applying a role with method attributes to something
68              
69             =head1 VERSION
70              
71             version 0.31
72              
73             =head1 METHODS
74              
75             =head2 apply
76              
77             The apply method is wrapped to ensure that the correct metaclasses to hold and propagate
78             method attribute data are present on the target for role application, delegates to
79             the original method to actually apply the role, then ensures that any attributes from
80             the role are copied to the target class.
81              
82             =head1 SUPPORT
83              
84             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-MethodAttributes>
85             (or L<bug-MooseX-MethodAttributes@rt.cpan.org|mailto:bug-MooseX-MethodAttributes@rt.cpan.org>).
86              
87             There is also a mailing list available for users of this distribution, at
88             L<http://lists.perl.org/list/moose.html>.
89              
90             There is also an irc channel available for users of this distribution, at
91             irc://irc.perl.org/#moose.
92              
93             =head1 AUTHORS
94              
95             =over 4
96              
97             =item *
98              
99             Florian Ragwitz <rafl@debian.org>
100              
101             =item *
102              
103             Tomas Doran <bobtfish@bobtfish.net>
104              
105             =back
106              
107             =head1 COPYRIGHT AND LICENCE
108              
109             This software is copyright (c) 2009 by Florian Ragwitz.
110              
111             This is free software; you can redistribute it and/or modify it under
112             the same terms as the Perl 5 programming language system itself.
113              
114             =cut