blib/lib/MooseX/ClassCompositor.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 92.3% 24 / 26 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 94.4% 153 / 162 CC 20 SCAR 18.1
CC
20
Cov
96%
class_for
13.0
_memoization_key
5.0
__hash_to_string
4.3
CRAP 6.1
↑ Summary
1 package MooseX::ClassCompositor 0.010;
2 # ABSTRACT: a factory that builds classes from roles
3  
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Moose::Util qw(apply_all_roles);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Moose::Util::MetaRole ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use MooseX::StrictConstructor::Trait::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use MooseX::Types::Perl qw(PackageName);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use String::RewritePrefix;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod my $comp = MooseX::ClassCompositor->new({
19 #pod class_basename => 'MyApp::Class',
20 #pod class_metaroles => {
21 #pod class => [ 'MooseX::StrictConstructor::Trait::Class' ],
22 #pod },
23 #pod role_prefixes => {
24 #pod '' => 'MyApp::Role::',
25 #pod '=' => '',
26 #pod },
27 #pod });
28 #pod
29 #pod my $class = $comp->class_for( qw( PieEater ContestWinner ) );
30 #pod
31 #pod my $object = $class->new({
32 #pod pie_type => 'banana',
33 #pod place => '2nd',
34 #pod });
35 #pod
36 #pod =head1 OVERVIEW
37 #pod
38 #pod A MooseX::ClassCompositor is a class factory. If you think using a class
39 #pod factory will make you feel like a filthy "enterprise" programmer, maybe you
40 #pod should turn back now.
41 #pod
42 #pod The compositor has a C<L</class_for>> method that builds a class by combining a
43 #pod list of roles with L<Moose::Object>, applying any supplied metaclass, and
44 #pod producing an arbitrary-but-human-scannable name. The metaclass is then
45 #pod made immutable, the operation is memoized, and the class name is returned.
46 #pod
47 #pod In the L</SYNOPSIS> above, you can see all the major features used:
48 #pod C<class_metaroles> to enable strict constructors, C<role_prefixes> to use
49 #pod L<String::RewritePrefix> to expand role name shorthand, and C<class_basename>
50 #pod to pick a namespace under which to put constructed classes.
51 #pod
52 #pod Not shown is the C<L</known_classes>> method, which returns a list of pairs
53 #pod describing all the classes that the factory has constructed. This method can
54 #pod be useful for debugging and other somewhat esoteric purposes like
55 #pod serialization.
56 #pod
57 #pod =cut
58  
59 #pod =attr class_basename
60 #pod
61 #pod This attribute must be given, and must be a valid Perl package name.
62 #pod Constructed classes will all be under this namespace.
63 #pod
64 #pod =cut
65  
66 4 has class_basename => (
67 is => 'ro',
68 isa => PackageName,
69 required => 1,
70 );
71  
72 #pod =attr class_metaroles
73 #pod
74 #pod This attribute, if given, must be a hashref of class metaroles that will be
75 #pod applied to newly-constructed classes with
76 #pod L<Moose::Util::MetaRole/apply_metaroles>.
77 #pod
78 #pod =cut
79  
80 has class_metaroles => (
81 reader => '_class_metaroles',
82 isa => 'HashRef',
83 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
84 4 );
85  
86 #pod =attr known_classes
87 #pod
88 #pod This attribute stores a mapping of class names to the parameters used to
89 #pod construct them. The C<known_classes> method returns its contents as a list of
90 #pod pairs.
91 #pod
92 #pod =cut
93  
94 has known_classes => (
95 reader => '_known_classes',
96 isa => 'HashRef',
97 traits => [ 'Hash' ],
98 handles => {
99 _learn_class => 'set',
100 known_classes => 'elements',
101 },
102 init_arg => undef,
103 7 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 4 );
105  
106 #pod =attr role_prefixes
107 #pod
108 #pod This attribute is used as the arguments to L<String::RewritePrefix> for
109 #pod expanding role names passed to the compositor's L<class_for> method.
110 #pod
111 #pod =cut
112  
113 has role_prefixes => (
114 reader => '_role_prefixes',
115 isa => 'HashRef',
116 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 4 );
118  
119 11 ▶sub _rewrite_roles {
Subroutine _rewrite_roles: called CC=1 SCAR=0.0
120 11 my $self = shift;
121 11 String::RewritePrefix->rewrite($self->_role_prefixes, @_);
122 }
123  
124 #pod =attr fixed_roles
125 #pod
126 #pod This attribute may be initialized with an arrayref of role names and/or
127 #pod L<Moose::Meta::Role> objects. These roles will I<always> be composed in
128 #pod the classes built by the compositor.
129 #pod
130 #pod Role names (but not Moose::Meta::Role objects) I<will> be rewritten by
131 #pod the role prefixes.
132 #pod
133 #pod =cut
134  
135 has fixed_roles => (
136 reader => '_fixed_roles',
137 isa => 'ArrayRef',
138 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 4 );
140  
141 4 has serial_counter => (
142 reader => '_serial_counter',
143 isa => 'Str',
144 default => 'AA',
145 traits => [ 'String' ],
146 handles => { next_serial => 'inc' },
147 init_arg => undef,
148 );
149  
150 has memoization_table => (
151 isa => 'HashRef',
152 7 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
153 4 traits => [ 'Hash' ],
154 handles => {
155 _class_for_key => 'get',
156 _set_class_for_key => 'set',
157 },
158 init_arg => undef,
159 );
160  
161 #pod =attr forbid_meta_role_objects
162 #pod
163 #pod If true, an exception will be raised if a Moose::Meta::Role object is passed to
164 #pod C<L</class_for>>. This is only rarely useful, such as if it's a strict
165 #pod requirement that the memoization table of the compositor be serializable and
166 #pod its contents reproduceable.
167 #pod
168 #pod Probably you don't need this.
169 #pod
170 #pod =cut
171  
172 4 has forbid_meta_role_objects => (
173 is => 'ro',
174 isa => 'Bool',
175 default => 0,
176 );
177  
178 #pod =method class_for
179 #pod
180 #pod my $class = $compositor->class_for(
181 #pod
182 #pod 'Role::Name', # <-- will be expanded with role_prefixes
183 #pod Other::Role->meta, # <-- will not be touched
184 #pod
185 #pod [
186 #pod 'Param::Role::Name', # <-- will be expanded with role_prefixes
187 #pod 'ApplicationName', # <-- will not be touched
188 #pod { ...param... },
189 #pod ],
190 #pod );
191 #pod
192 #pod This method will return a class with the roles passed to it. They can be given
193 #pod either as names (which will be expanded according to C<L</role_prefixes>>), as
194 #pod L<Moose::Meta::Role> objects, or as arrayrefs containing a role name,
195 #pod application name, and hashref of parameters. In the arrayref form, the
196 #pod application name is just a name used to uniquely identify this application of
197 #pod a parameterized role, so that they can be applied multiple times with each
198 #pod application accounted for internally.
199 #pod
200 #pod =cut
201  
202 22 ▶sub class_for {
Subroutine class_for: called CC=13 SCAR=25.7
Pod documented
203 22 my ($self, @args) = @_;
204  
205 # can't use memoize without losing subclassability, so we reimplemented
206 # -- rjbs, 2011-08-05
207 22 my $memo_key = $self->_memoization_key(\@args);
208 22 ▶ if (my $cached = $self->_class_for_key($memo_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached = $self->_class_for_key($memo_key)) 12 10 22
209 12 return $cached;
210 }
211  
212 # Arguments here are role names, or role objects followed by nonce-names.
213 10 my @orig_args = @args;
214  
215 # $role_hash is a hash mapping nonce-names to role objects
216 # $role_names is an array of names of more roles to add
217 10 my (@roles, @role_class_names, @all_names);
218  
219 10 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 14 9 23
220 14 my $name = shift @args;
221 14 ▶ if (ref $name eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $name eq 'ARRAY') { } 2 12 14
elsif (builtin::blessed($name) and $name->isa('Moose::Meta::Role')) { } 5 7 12
222 2 my ($role_name, $moniker, $params) = @$name;
223  
224 2 my $full_name = $self->_rewrite_roles($role_name);
225 2 Class::Load::load_class($full_name);
226 2 my $role_object = $full_name->meta->generate_role(
227 parameters => $params,
228 );
229  
230 2 push @roles, $role_object;
231 2 $name = $moniker;
232 ▶ } elsif (blessed $name and $name->isa('Moose::Meta::Role')) {
Condition66%2/3
builtin::blessed($name) and $name->isa("Moose::Meta::Role")
!ll&&!rl&&r
705
MC/DC50%1/2
builtin::blessed($name) and $name->isa("Moose::Meta::Role")
builtin::blessed($name) $name->isa("Moose::Meta::Role")
Truth table66%2/3
A and B
A= builtin::blessed($name)B= $name->isa("Moose::Meta::Role")
ABresult
0X0
100
111
233 5 ▶ confess "this class compositor does not allow role objects"
Branch: 1 branch
BranchTrueFalseTotal
if $self->forbid_meta_role_objects 1 4 5
234 if $self->forbid_meta_role_objects;
235  
236 4 push @roles, $name;
237 4 $name = $name->name;
238 } else {
239 7 push @role_class_names, $name;
240 }
241  
242 13 ▶ $name =~ s/::/_/g if @all_names;
Branch: 1 branch
BranchTrueFalseTotal
if @all_names 4 9 13
243 13 $name =~ s/^=//;
244  
245 13 push @all_names, $name;
246 }
247  
248 9 my $name = join q{::}, $self->class_basename, @all_names;
249  
250 9 for my $r (@{ $self->_fixed_roles }) {
251 3 ▶ if (blessed $r and $r->isa('Moose::Meta::Role')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($r) and $r->isa('Moose::Meta::Role')) { } 1 2 3
Condition66%2/3
builtin::blessed($r) and $r->isa("Moose::Meta::Role")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($r) and $r->isa("Moose::Meta::Role")
builtin::blessed($r) $r->isa("Moose::Meta::Role")
Truth table66%2/3
A and B
A= builtin::blessed($r)B= $r->isa("Moose::Meta::Role")
ABresult
0X0
100
111
252 1 push @roles, $r;
253 } else {
254 2 push @role_class_names, $r;
255 }
256 }
257  
258 9 @role_class_names = $self->_rewrite_roles(@role_class_names);
259  
260 # We only _try_ to load because in use, some of these are embedded in other
261 # packages. While we'd like to stop relying on this, this is an expedient
262 # change. After all, it'll fail during composition, if the role package is
263 # not properly set up. -- rjbs, 2018-06-21
264 9 Class::Load::try_load_class($_) for @role_class_names;
265  
266 9 ▶ if ($name->can('meta')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->can("meta")) 1 8 9
267 1 $name .= "_" . $self->next_serial;
268 }
269  
270 9 my $class = Moose::Meta::Class->create( $name => (
271 superclasses => [ 'Moose::Object' ],
272 ));
273  
274 9 $class = Moose::Util::MetaRole::apply_metaroles(
275 for => $class->name,
276 class_metaroles => $self->_class_metaroles,
277 );
278  
279 9 apply_all_roles($class, @role_class_names, map $_->name, @roles);
280  
281 9 $class->make_immutable;
282  
283 9 $self->_learn_class($name, \@orig_args);
284 9 $self->_set_class_for_key($memo_key, $name);
285  
286 9 return $class->name;
287 }
288  
289 22 ▶sub _memoization_key {
Subroutine _memoization_key: called CC=5 SCAR=16.1
290 22 my ($self, $args) = @_;
291 22 my @args = @$args;
292  
293 22 my @k;
294 22 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 32 22 54
295 32 my $arg = shift @args;
296 32 ▶ if (ref $arg eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 6 26 32
elsif (builtin::blessed($arg) and $arg->isa('Moose::Meta::Role')) { } 5 21 26
297 6 my ($role_name, $moniker, $params) = @$arg;
298 6 push @k, "$moniker : { " . __hash_to_string($params) . " }";
299 ▶ } elsif (blessed $arg and $arg->isa('Moose::Meta::Role')) {
Condition66%2/3
builtin::blessed($arg) and $arg->isa("Moose::Meta::Role")
!ll&&!rl&&r
2105
MC/DC50%1/2
builtin::blessed($arg) and $arg->isa("Moose::Meta::Role")
builtin::blessed($arg) $arg->isa("Moose::Meta::Role")
Truth table66%2/3
A and B
A= builtin::blessed($arg)B= $arg->isa("Moose::Meta::Role")
ABresult
0X0
100
111
300 5 push @k, $arg->name;
301 } else {
302 21 push @k, $arg;
303 }
304 }
305 22 my $key = join "; ", sort @k;
306 22 return $key;
307 }
308  
309 6 ▶sub __hash_to_string {
Subroutine __hash_to_string: called CC=4 SCAR=14.6
310 6 my ($h) = @_;
311 6 my @k;
312 6 for my $k (sort keys %$h) {
313 my $v = ! defined($h->{$k}) ? "<undef>" :
314 0 ▶ ref($h->{$k}) ? join("-", @{$h->{$k}}) : $h->{$k};
Branch: 2 branches
BranchTrueFalseTotal
!defined($h->{$k}) ? : 0 6 6
ref $h->{$k} ? : 0 6 6
315 6 push @k, "$k => $v";
316 }
317 6 join ", " => @k;
318 }
319  
320 #pod =head1 THANKS
321 #pod
322 #pod Thanks to Pobox.com for sponsoring the development of this library.
323 #pod
324 #pod =cut
325  
326 4 __PACKAGE__->meta->make_immutable;
327 4 1;
328  
329 __END__
↑ Summary