File Coverage

blib/lib/MooseX/ComposedBehavior/Guts.pm
Criterion Covered Total %
statement 42 42 100.0
branch 21 30 70.0
condition n/a
subroutine 15 15 100.0
pod n/a
total 78 87 89.6


line stmt bran cond sub pod time code
1             package MooseX::ComposedBehavior::Guts 0.005;
2 5     5   1944 use MooseX::Role::Parameterized 0.21;
  5         386044  
  5         27  
3             # ABSTRACT: the gooey, meaty bits that help MooseX::ComposedBehavior work
4              
5             #pod =head1 OVERVIEW
6             #pod
7             #pod MooseX::ComposedBehavior::Guts contains a bunch of code that is used by
8             #pod L<MooseX::ComposedBehavior> to get its job done. It is basically a hack, and
9             #pod relying on any part of its interface would be a I<terrible> idea.
10             #pod
11             #pod Reading the source, on the other hand, might be useful in understanding what
12             #pod the heck is going on, especially if you encounter weird problem.
13             #pod
14             #pod =cut
15              
16 5     5   166541 use Moose::Util::TypeConstraints;
  5         12  
  5         47  
17              
18             parameter stub_method_name => (
19             isa => 'Str',
20             required => 1,
21             );
22              
23             parameter method_name => (
24             isa => 'Str',
25             required => 1,
26             );
27              
28             subtype 'MooseX::ComposedBehavior::Stub::_MethodList',
29             as 'ArrayRef[Str|CodeRef]';
30              
31             coerce 'MooseX::ComposedBehavior::Stub::_MethodList',
32             from 'CodeRef', via { [$_] },
33             from 'Str', via { [$_] };
34              
35             parameter also_compose => (
36             isa => 'MooseX::ComposedBehavior::Stub::_MethodList',
37             coerce => 1,
38             );
39              
40             parameter compositor => (
41             isa => 'CodeRef',
42             required => 1,
43             );
44              
45             parameter context => (
46             isa => enum([ qw(list scalar) ]),
47             predicate => 'forces_context',
48             );
49              
50             parameter method_order => (
51             isa => enum([ qw(standard reverse) ]),
52             default => 'standard',
53             );
54              
55             role {
56             my ($p) = @_;
57              
58             my $wantarray = $p->forces_context ? ($p->context eq 'list' ? 1 : 0) : undef;
59              
60             my $stub_name = $p->stub_method_name;
61       4     method $stub_name => sub { };
        6      
        6      
        6      
        4      
        2      
        2      
62              
63             my $method_name = $p->method_name;
64             my $compositor = $p->compositor;
65             my $also_compose = $p->also_compose;
66             my $reverse = $p->method_order eq 'reverse';
67              
68             method $method_name => sub {
69 2     2   615 my $self = shift;
  6     6   23188  
  4     6   15011  
        6      
        4      
        4      
70              
71 2         4 my $results = [];
  6         10  
  4         20  
72              
73 2 50       8 my $wantarray = defined $wantarray ? $wantarray : wantarray;
  6 100       16  
  4 100       12  
74              
75 2         7 my @methods = Class::MOP::class_of($self)
  6         23  
  4         15  
76             ->find_all_methods_by_name($stub_name);
77              
78 2 50       457 @methods = reverse @methods if $reverse;
  6 50       961  
  4 50       614  
79              
80 2         6 foreach my $method (@methods) {
  6         13  
  4         9  
81 4         6 my @array;
  6         9  
  4         7  
82             $wantarray ? (@array = $method->{code}->execute($self, \@_, $results))
83 4 100       21 : (scalar $method->{code}->execute($self, \@_, $results));
  6 100       30  
  4 50       40  
84             }
85              
86 2 50       9 if (defined $also_compose) {
  6 50       13  
  4 50       21  
87 2         5 for my $also_method (@$also_compose) {
  6         11  
  4         12  
88 2 100       13 push @$results, ($wantarray
  6 100       23  
  5 50       101  
89             ? [ $self->$also_method(@_) ] : scalar $self->$also_method(@_));
90             }
91             }
92              
93 2         17 return $compositor->($self, \@$results);
  6         38  
  4         48  
94             }
95             };
96              
97             1;
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             MooseX::ComposedBehavior::Guts - the gooey, meaty bits that help MooseX::ComposedBehavior work
108              
109             =head1 VERSION
110              
111             version 0.005
112              
113             =head1 OVERVIEW
114              
115             MooseX::ComposedBehavior::Guts contains a bunch of code that is used by
116             L<MooseX::ComposedBehavior> to get its job done. It is basically a hack, and
117             relying on any part of its interface would be a I<terrible> idea.
118              
119             Reading the source, on the other hand, might be useful in understanding what
120             the heck is going on, especially if you encounter weird problem.
121              
122             =head1 PERL VERSION
123              
124             This library should run on perls released even a long time ago. It should work
125             on any version of perl released in the last five years.
126              
127             Although it may work on older versions of perl, no guarantee is made that the
128             minimum required version will not be increased. The version may be increased
129             for any reason, and there is no promise that patches will be accepted to lower
130             the minimum required perl.
131              
132             =head1 AUTHOR
133              
134             Ricardo Signes <cpan@semiotic.systems>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2022 by Ricardo Signes.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             =cut