blib/lib/CatalystX/Component/Traits.pm

filter: Statementstmt 98.6% 73 / 74 Branchbran 75.0% 15 / 20 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 90.6% 107 / 118 CC 14 SCAR 16.5
CC
14
Cov
92%
_merge_traits
10.1
_trait_search_order
4.0
COMPONENT
2.0
CRAP 5.2
↑ Summary
1 package CatalystX::Component::Traits;
2 {
3 9 $CatalystX::Component::Traits::VERSION = '0.19';
4 }
5  
6 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use List::MoreUtils qw/firstidx any uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Scalar::Util 'reftype';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Class::Load qw/ load_first_existing_class /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 with 'MooseX::Traits::Pluggable' => { -excludes => ['_find_trait'] };
13  
14 =head1 NAME
15  
16 CatalystX::Component::Traits - Automatic Trait Loading and Resolution for Catalyst Components
17  
18 =cut
19  
20 9 our $AUTHORITY = 'id:RKITOVER';
21  
22 =head1 SYNOPSIS
23  
24 package Catalyst::Model::SomeModel;
25 with 'CatalystX::Component::Traits';
26  
27 package MyApp::Model::MyModel;
28 use parent 'Catalyst::Model::SomeModel';
29  
30 package MyApp;
31  
32 __PACKAGE__->config('Model::MyModel' => {
33 traits => ['SearchedForTrait', '+Fully::Qualified::Trait']
34 });
35  
36 =head1 DESCRIPTION
37  
38 Adds a L<Catalyst::Component/COMPONENT> method to your L<Catalyst> component
39 base class that reads the optional C<traits> parameter from app and component
40 config and instantiates the component subclass with those traits using
41 L<MooseX::Traits/new_with_traits> from L<MooseX::Traits::Pluggable>.
42  
43 =head1 TRAIT SEARCH
44  
45 Trait names qualified with a C<+> are taken to be full package names.
46  
47 Unqualified names are searched for, using the algorithm described below.
48  
49 =head2 EXAMPLE
50  
51 Suppose your inheritance hierarchy is:
52  
53 MyApp::Model::MyModel
54 Catalyst::Model::CatModel
55 Catalyst::Model
56 Catalyst::Component
57 Moose::Object
58  
59 The configuration is:
60  
61 traits => ['Foo']
62  
63 The package search order for C<Foo> will be:
64  
65 MyApp::TraitFor::Model::CatModel::Foo
66 Catalyst::TraitFor::Model::CatModel::Foo
67  
68 =head2 A MORE PATHOLOGICAL EXAMPLE
69  
70 For:
71  
72 My::App::Controller::AController
73 CatalystX::Something::ControllerBase::SomeController
74 Catalyst::Controller
75 Catalyst::Model
76 Catalyst::Component
77 Moose::Object
78  
79 With:
80  
81 traits => ['Foo']
82  
83 Search order for C<Foo> will be:
84  
85 My::App::TraitFor::Controller::SomeController::Foo
86 CatalystX::Something::TraitFor::Controller::SomeController::Foo
87  
88 The C<Base> after (M|V|C) is automatically removed.
89  
90 =head1 TRAIT MERGING
91  
92 Traits from component class config and app config are automatically merged if
93 you set the C<_trait_merge> attribute default, e.g.:
94  
95 has '+_trait_merge' => (default => 1);
96  
97 You can remove component class config traits by prefixing their names with a
98 C<-> in the app config traits.
99  
100 For example:
101  
102 package Catalyst::Model::Foo;
103 has '+_trait_merge' => (default => 1);
104 __PACKAGE__->config->{traits} = [qw/Foo Bar/];
105  
106 package MyApp;
107 __PACKAGE__->config->{'Model::Foo'}{traits} = [qw/-Foo Baz/];
108  
109 Will load the traits:
110  
111 Bar Baz
112  
113 =cut
114  
115 # override MX::Traits attribute
116 9 ▶has '_trait_namespace' => (
Branch: 1 branch
BranchTrueFalseTotal
'Moose'->VERSION >= 0.84 ? : 9 0 9
117 init_arg => undef,
118 isa => 'Str',
119 (Moose->VERSION >= 0.84 ) ? (is => 'bare') : (),
120 default => '+Trait',
121 );
122  
123 9 ▶has '_trait_merge' => (
Branch: 1 branch
BranchTrueFalseTotal
'Moose'->VERSION >= 0.84 ? : 9 0 9
124 init_arg => undef,
125 isa => 'Str',
126 (Moose->VERSION >= 0.84 ) ? (is => 'bare') : (),
127 default => 0,
128 );
129  
130 5 ▶sub COMPONENT {
Subroutine COMPONENT: called CC=2 SCAR=7.1
Pod undocumented
131 5 my ($class, $app, $args) = @_;
132  
133 5 my %class_config = %{ $class->config };
134 5 my %app_config = %$args;
135  
136 my $traits = $class->_merge_traits(
137 delete $class_config{traits},
138 delete $app_config{traits},
139 5 );
140  
141 5 $args = $class->merge_config_hashes(\%class_config, \%app_config);
142  
143 5 ▶ if ($traits) {
Branch: 1 branch
BranchTrueFalseTotal
if ($traits) 5 0 5
144 5 return $class->new_with_traits($app, {
145 traits => $traits,
146 %$args
147 });
148 }
149  
150 0 return $class->new($app, $args);
151 }
152  
153 5 ▶sub _merge_traits {
Subroutine _merge_traits: called CC=10 SCAR=23.1
154 5 my $class = shift;
155 5 ▶ my $left_traits = shift || [];
Condition100%2/2
shift() || []
l!l
41
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
156 5 ▶ my $right_traits = shift || [];
Condition100%2/2
shift() || []
l!l
41
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
157  
158 my $should_merge =
159 5 eval { $class->meta->find_attribute_by_name('_trait_merge')->default };
160 5 ▶ $should_merge = $should_merge->()
Branch: 1 branch
BranchTrueFalseTotal
if ref $should_merge and builtin::reftype($should_merge) eq "CODE" 0 5 5
Condition33%1/3
ref $should_merge and builtin::reftype($should_merge) eq "CODE"
!ll&&!rl&&r
500
MC/DC0%0/2
ref $should_merge and builtin::reftype($should_merge) eq "CODE"
ref $should_merge builtin::reftype($should_merge) eq "CODE"
Truth table33%1/3
A and B
A= ref $should_mergeB= builtin::reftype($should_merge) eq "CODE"
ABresult
0X0
100
111
161 if ref($should_merge) && reftype($should_merge) eq 'CODE';
162  
163 5 ▶ my @right_traits = ref($right_traits) ? @$right_traits : $right_traits;
Branch: 1 branch
BranchTrueFalseTotal
ref $right_traits ? : 3 2 5
164 5 ▶ my @left_traits = ref($left_traits) ? @$left_traits : $left_traits;
Branch: 1 branch
BranchTrueFalseTotal
ref $left_traits ? : 4 1 5
165 5 ▶ unless ($should_merge) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($should_merge) 2 3 5
166 2 ▶ return @right_traits ? \@right_traits : \@left_traits;
Branch: 1 branch
BranchTrueFalseTotal
@right_traits ? : 2 0 2
167 }
168  
169 3 ▶ my @to_remove = map { /^-(.*)/ ? $1 : () } @left_traits, @right_traits;
Branch: 1 branch
BranchTrueFalseTotal
/^-(.*)/ ? : 1 9 10
170 3 @left_traits = grep !/^-/, @left_traits;
171 3 @right_traits = grep !/^-/, @right_traits;
172  
173 my @traits = grep {
174 3 my $trait = $_;
175 9 ▶ not any { $trait eq $_ } @to_remove;
Subroutine __ANON__: called CC=1 SCAR=0.0
176 } (@left_traits, @right_traits);
177  
178 3 return [ uniq @traits ];
179 }
180  
181 11 ▶sub _find_trait {
Subroutine _find_trait: called CC=1 SCAR=0.0
182 11 my ($class, $base, $name) = @_;
183  
184 11 load_first_existing_class($class->_trait_search_order($base, $name));
185 }
186  
187 15 ▶sub _trait_search_order {
Subroutine _trait_search_order: called CC=4 SCAR=13.9
188 15 my ($class, $base, $name) = @_;
189  
190 15 my @search_ns = $class->meta->class_precedence_list;
191  
192 15 my $MVCC = qr/(?:Model|View|Controller|Component)/;
193  
194 my $possible_parent_idx =
195 15 ▶ (firstidx { /^CatalystX?::/ } @search_ns[1 .. $#search_ns]) + 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
196  
197 15 my ($parent, $parent_idx, $parent_name, $parent_name_partial);
198  
199 15 for my $try_parent ($possible_parent_idx, 0) {
200 18 $parent_idx = $try_parent;
201 18 $parent = $search_ns[$parent_idx];
202  
203 18 ($parent_name, $parent_name_partial) =
204 $parent =~ /($MVCC(?:Base)? (?: ::)? (.*))/x;
205  
206 18 ▶ last if $parent_name_partial; # otherwise root level component
Branch: 1 branch
BranchTrueFalseTotal
if $parent_name_partial 15 3 18
207 }
208  
209 15 (my $resolved_parent_name = $parent_name) =~ s/($MVCC)Base\b/$1/;
210  
211 15 my ($parent_part) = $parent =~ /($MVCC) (?:Base)? (?: ::)?/x;
212  
213 15 my @res;
214  
215 15 for my $ns (@search_ns[0 .. $parent_idx]) {
216 27 my $find_part = $parent_part;
217  
218 27 my ($part) = $ns =~ /^(.+?)::$parent_part/;
219 27 push @res, "${part}::${base}For::${resolved_parent_name}::$name";
220 }
221  
222 15 @res;
223 }
224  
225 # we'll come back to this later...
226 # for my $ns (@search_ns[($parent_idx+1) .. $#search_ns]) {
227 # my ($part, $rest) = split /::/, $ns, 2;
228 #
229 # # no non-core crap in the Moose:: namespace
230 # $part = 'MooseX' if $part eq 'Moose';
231 #
232 # push @res, "${part}::${base}For::${rest}::$name";
233 # }
234 #
235 # @res;
236 #}
237  
238 =head1 AUTHOR
239  
240 Rafael Kitover, C<< <rkitover@cpan.org> >>
241  
242 =head1 CONTRIBUTORS
243  
244 Tomas Doran, C<< <bobtfish@bobtfish.net> >>
245  
246 =head1 BUGS
247  
248 Please report any bugs or feature requests to C<bug-catalystx-component-traits
249 at rt.cpan.org>, or through the web interface at
250 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-Component-Traits>. I
251 will be notified, and then you'll automatically be notified of progress on your
252 bug as I make changes.
253  
254 =head1 ACKNOWLEDGEMENTS
255  
256 Matt S. Trout and Tomas Doran helped me with the current design.
257  
258 =head1 COPYRIGHT & LICENSE
259  
260 Copyright (c) 2014, Rafael Kitover
261  
262 This program is free software; you can redistribute it and/or modify it
263 under the same terms as Perl itself.
264  
265 =cut
266  
267 9 __PACKAGE__; # End of CatalystX::Component::Traits
↑ Summary