File Coverage

blib/lib/MooseX/Declare/Syntax/MooseSetup.pm
Criterion Covered Total %
statement 29 29 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod 3 3 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package MooseX::Declare::Syntax::MooseSetup;
2             # ABSTRACT: Common Moose namespaces declarations
3              
4             our $VERSION = '0.43';
5              
6 24     24   21744 use Moose::Role;
  24         71549  
  24         128  
7              
8 24     24   90592 use Moose::Util qw( find_meta );
  24         33  
  24         125  
9 24     24   3435 use Sub::Install qw( install_sub );
  24         35  
  24         160  
10              
11 24     24   1697 use aliased 'MooseX::Declare::Syntax::Keyword::MethodModifier';
  24         254  
  24         185  
12 24     24   2686 use aliased 'MooseX::Declare::Syntax::Keyword::Method';
  24         36  
  24         136  
13 24     24   2390 use aliased 'MooseX::Declare::Syntax::Keyword::With', 'WithKeyword';
  24         42  
  24         128  
14 24     24   1843 use aliased 'MooseX::Declare::Syntax::Keyword::Clean', 'CleanKeyword';
  24         31  
  24         122  
15              
16 24     24   1828 use namespace::autoclean;
  24         40  
  24         97  
17              
18             #pod =head1 DESCRIPTION
19             #pod
20             #pod This role is basically an extension to
21             #pod L<NamespaceHandling|MooseX::Declare::Syntax::NamespaceHandling>. It adds all
22             #pod the common parts for L<Moose> namespace definitions. Examples of this role
23             #pod can be found in the L<class|MooseX::Declare::Syntax::Keyword::Class> and
24             #pod L<role|MooseX::Declare::Syntax::Keyword::Role> keywords.
25             #pod
26             #pod =head1 CONSUMES
27             #pod
28             #pod =for :list
29             #pod * L<MooseX::Declare::Syntax::NamespaceHandling>
30             #pod * L<MooseX::Declare::Syntax::EmptyBlockIfMissing>
31             #pod
32             #pod =cut
33              
34             with qw(
35             MooseX::Declare::Syntax::NamespaceHandling
36             MooseX::Declare::Syntax::EmptyBlockIfMissing
37             );
38              
39             #pod =method auto_make_immutable
40             #pod
41             #pod Bool Object->auto_make_immutable ()
42             #pod
43             #pod Since L<Moose::Role>s can't be made immutable (this is not a bug or a
44             #pod missing feature, it would make no sense), this always returns false.
45             #pod
46             #pod =cut
47              
48 14     14 1 65 sub auto_make_immutable { 0 }
49              
50             #pod =method imported_moose_symbols
51             #pod
52             #pod List Object->imported_moose_symbols ()
53             #pod
54             #pod This will return C<confess> and C<blessed> by default to provide as
55             #pod additional imports to the namespace.
56             #pod
57             #pod =cut
58              
59 61     61 1 2636 sub imported_moose_symbols { qw( confess blessed ) }
60              
61             #pod =method import_symbols_from
62             #pod
63             #pod Str Object->import_symbols_from ()
64             #pod
65             #pod The namespace from which the additional imports will be imported. This
66             #pod will return C<Moose> by default.
67             #pod
68             #pod =cut
69              
70 47     47 1 258 sub import_symbols_from { 'Moose' }
71              
72             #pod =head1 MODIFIED METHODS
73             #pod
74             #pod =head2 default_inner
75             #pod
76             #pod ArrayRef default_inner ()
77             #pod
78             #pod This will provide the following default inner-handlers to the namespace:
79             #pod
80             #pod =for :list
81             #pod * method
82             #pod A simple L<Method|MooseX::Declare::Syntax::Keyword::Method> handler.
83             #pod * around
84             #pod This is a L<MethodModifier|MooseX::Declare::Syntax::Keyword::MethodModifier>
85             #pod handler that will start the signature of the generated method with
86             #pod C<$orig: $self> to provide the original method in C<$orig>.
87             #pod * after
88             #pod * before
89             #pod * override
90             #pod * augment
91             #pod These four handlers are L<MethodModifier|MooseX::Declare::Syntax::Keyword::MethodModifier>
92             #pod instances.
93             #pod * clean
94             #pod This is an instance of the L<Clean|MooseX::Declare::Syntax::Keyword::Clean> keyword
95             #pod handler.
96             #pod
97             #pod The original method will never be called and all arguments are ignored at the
98             #pod moment.
99             #pod
100             #pod =cut
101              
102             around default_inner => sub {
103             return [
104             WithKeyword->new(identifier => 'with'),
105             Method->new(identifier => 'method'),
106             MethodModifier->new(
107             identifier => 'around',
108             modifier_type => 'around',
109             prototype_injections => {
110             declarator => 'around',
111             injections => [ 'CodeRef $orig' ],
112             },
113             ),
114             map { MethodModifier->new(identifier => $_, modifier_type => $_) }
115             qw( after before override augment ),
116             ];
117             };
118              
119             #pod =head2 setup_inner_for
120             #pod
121             #pod Object->setup_inner_for (ClassName $class)
122             #pod
123             #pod This will install a C<with> function that will push its arguments onto a global
124             #pod storage array holding the roles of the current namespace.
125             #pod
126             #pod =cut
127              
128             after setup_inner_for => sub {
129             my ($self, $setup_class, %args) = @_;
130             my $keyword = CleanKeyword->new(identifier => 'clean');
131             $keyword->setup_for($setup_class, %args);
132             };
133              
134             #pod =head2 add_namespace_customizations
135             #pod
136             #pod Object->add_namespace_customizations (Object $context, Str $package, HashRef $options)
137             #pod
138             #pod After all other customizations, this will first add code to import the
139             #pod L</imported_moose_symbols> from the package returned in L</import_symbols_from> to
140             #pod the L<preamble|MooseX::Declare::Context/preamble_code_parts>.
141             #pod
142             #pod Then it will add a code part that will immutabilize the class to the
143             #pod L<cleanup|MooseX::Declare::Context/cleanup_code_parts> code if the
144             #pod L</auto_make_immutable> method returned a true value and C<< $options->{is}{mutable} >>
145             #pod does not exist.
146             #pod
147             #pod =cut
148              
149             after add_namespace_customizations => sub {
150             my ($self, $ctx, $package) = @_;
151              
152             # add Moose initializations to preamble
153             $ctx->add_preamble_code_parts(
154             sprintf 'use %s qw( %s )', $self->import_symbols_from($ctx), join ' ', $self->imported_moose_symbols($ctx),
155             );
156              
157             # make class immutable unless specified otherwise
158             $ctx->add_cleanup_code_parts(
159             "${package}->meta->make_immutable",
160             ) if $self->auto_make_immutable
161             and not exists $ctx->options->{is}{mutable};
162             };
163              
164             #pod =head2 handle_post_parsing
165             #pod
166             #pod CodeRef Object->handle_post_parsing (Object $context, Str $package, Str|Object $name)
167             #pod
168             #pod Generates a callback that sets up the roles in the global role storage for the current
169             #pod namespace. The C<$name> parameter will be the specified name (in contrast to C<$package>
170             #pod which will always be the fully qualified name) or the anonymous metaclass instance if
171             #pod none was specified.
172             #pod
173             #pod =cut
174              
175             after handle_post_parsing => sub {
176             my ($self, $ctx, $package, $class) = @_;
177 3     3   8568 $ctx->shadow(sub (&) { shift->(); return $class; });
  3         14000  
178             };
179              
180              
181             #pod =head1 SEE ALSO
182             #pod
183             #pod =for :list
184             #pod * L<MooseX::Declare>
185             #pod * L<Moose>
186             #pod
187             #pod =cut
188              
189             1;
190              
191             __END__
192              
193             =pod
194              
195             =encoding UTF-8
196              
197             =head1 NAME
198              
199             MooseX::Declare::Syntax::MooseSetup - Common Moose namespaces declarations
200              
201             =head1 VERSION
202              
203             version 0.43
204              
205             =head1 DESCRIPTION
206              
207             This role is basically an extension to
208             L<NamespaceHandling|MooseX::Declare::Syntax::NamespaceHandling>. It adds all
209             the common parts for L<Moose> namespace definitions. Examples of this role
210             can be found in the L<class|MooseX::Declare::Syntax::Keyword::Class> and
211             L<role|MooseX::Declare::Syntax::Keyword::Role> keywords.
212              
213             =head1 METHODS
214              
215             =head2 auto_make_immutable
216              
217             Bool Object->auto_make_immutable ()
218              
219             Since L<Moose::Role>s can't be made immutable (this is not a bug or a
220             missing feature, it would make no sense), this always returns false.
221              
222             =head2 imported_moose_symbols
223              
224             List Object->imported_moose_symbols ()
225              
226             This will return C<confess> and C<blessed> by default to provide as
227             additional imports to the namespace.
228              
229             =head2 import_symbols_from
230              
231             Str Object->import_symbols_from ()
232              
233             The namespace from which the additional imports will be imported. This
234             will return C<Moose> by default.
235              
236             =head1 CONSUMES
237              
238             =over 4
239              
240             =item *
241              
242             L<MooseX::Declare::Syntax::NamespaceHandling>
243              
244             =item *
245              
246             L<MooseX::Declare::Syntax::EmptyBlockIfMissing>
247              
248             =back
249              
250             =head1 MODIFIED METHODS
251              
252             =head2 default_inner
253              
254             ArrayRef default_inner ()
255              
256             This will provide the following default inner-handlers to the namespace:
257              
258             =over 4
259              
260             =item *
261              
262             method
263              
264             A simple L<Method|MooseX::Declare::Syntax::Keyword::Method> handler.
265              
266             =item *
267              
268             around
269              
270             This is a L<MethodModifier|MooseX::Declare::Syntax::Keyword::MethodModifier>
271             handler that will start the signature of the generated method with
272             C<$orig: $self> to provide the original method in C<$orig>.
273              
274             =item *
275              
276             after
277              
278             =item *
279              
280             before
281              
282             =item *
283              
284             override
285              
286             =item *
287              
288             augment
289              
290             These four handlers are L<MethodModifier|MooseX::Declare::Syntax::Keyword::MethodModifier>
291             instances.
292              
293             =item *
294              
295             clean
296              
297             This is an instance of the L<Clean|MooseX::Declare::Syntax::Keyword::Clean> keyword
298             handler.
299              
300             =back
301              
302             The original method will never be called and all arguments are ignored at the
303             moment.
304              
305             =head2 setup_inner_for
306              
307             Object->setup_inner_for (ClassName $class)
308              
309             This will install a C<with> function that will push its arguments onto a global
310             storage array holding the roles of the current namespace.
311              
312             =head2 add_namespace_customizations
313              
314             Object->add_namespace_customizations (Object $context, Str $package, HashRef $options)
315              
316             After all other customizations, this will first add code to import the
317             L</imported_moose_symbols> from the package returned in L</import_symbols_from> to
318             the L<preamble|MooseX::Declare::Context/preamble_code_parts>.
319              
320             Then it will add a code part that will immutabilize the class to the
321             L<cleanup|MooseX::Declare::Context/cleanup_code_parts> code if the
322             L</auto_make_immutable> method returned a true value and C<< $options->{is}{mutable} >>
323             does not exist.
324              
325             =head2 handle_post_parsing
326              
327             CodeRef Object->handle_post_parsing (Object $context, Str $package, Str|Object $name)
328              
329             Generates a callback that sets up the roles in the global role storage for the current
330             namespace. The C<$name> parameter will be the specified name (in contrast to C<$package>
331             which will always be the fully qualified name) or the anonymous metaclass instance if
332             none was specified.
333              
334             =head1 SEE ALSO
335              
336             =over 4
337              
338             =item *
339              
340             L<MooseX::Declare>
341              
342             =item *
343              
344             L<Moose>
345              
346             =back
347              
348             =head1 AUTHOR
349              
350             Florian Ragwitz <rafl@debian.org>
351              
352             =head1 COPYRIGHT AND LICENSE
353              
354             This software is copyright (c) 2008 by Florian Ragwitz.
355              
356             This is free software; you can redistribute it and/or modify it under
357             the same terms as the Perl 5 programming language system itself.
358              
359             =cut