parameterized roles now available
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Keyword / Role.pm
CommitLineData
856ac9a7 1use MooseX::Declare;
2
3class CatalystX::Declare::Keyword::Role
4 extends MooseX::Declare::Syntax::Keyword::Role {
5
6
7 use aliased 'MooseX::MethodAttributes::Role::Meta::Role';
c2a8165b 8 use aliased 'MooseX::Role::Parameterized::Meta::Role::Parameterizable';
9 use aliased 'CatalystX::Declare::Keyword::Action', 'ActionKeyword';
856ac9a7 10
11
c2a8165b 12 around import_symbols_from (Object $ctx) {
13
14 $ctx->has_parameter_signature
15 ? $self->$orig($ctx)
16 : sprintf('Moose::Role -traits => q(MethodAttributes),')
17 }
18
856ac9a7 19 before add_namespace_customizations (Object $ctx, Str $package) {
20
c2a8165b 21 my $source = $self->import_symbols_from($ctx);
22 my @symbols = $self->imported_moose_symbols;
23
856ac9a7 24 $ctx->add_preamble_code_parts(
25 'use CLASS',
856ac9a7 26 );
27 }
28
c2a8165b 29 after add_namespace_customizations (Object $ctx, Str $package) {
30
31 $ctx->add_preamble_code_parts(
32 sprintf(
33 'use %s -traits => q(%s), qw( has )',
34 'MooseX::Role::Parameterized',
35 'MooseX::MethodAttributes::Role::Meta::Role',
36 ),
37 ) if $ctx->has_parameter_signature;
38 }
856ac9a7 39
c2a8165b 40 around default_inner (@args) {
856ac9a7 41
42 return [
c2a8165b 43 @{ $self->$orig(@args) },
856ac9a7 44 ActionKeyword->new(identifier => 'action'),
45 ActionKeyword->new(identifier => 'under'),
46 ActionKeyword->new(identifier => 'final'),
47 ];
48 }
49}
50
51__END__
52
53=head1 NAME
54
55CatalystX::Declare::Keyword::Role - Declare Catalyst Controller Roles
56
57=head1 SYNOPSIS
58
59 use CatalystX::Declare;
60
205323ac 61 controller_role MyApp::Web::ControllerRole::Foo {
856ac9a7 62
63 method provided_method { ... }
64
65 action foo, under base, is final { ... }
66
67 around bar_action (Object $ctx) { ... }
68 }
69
2387a55a 70 controller_role MyApp::Web::ControllerRole::WithParam (Str :$msg!) {
71
72 final action message under base {
73 $ctx->stash(message => $msg);
74 }
75 }
76
856ac9a7 77=head1 DESCRIPTION
78
205323ac 79This handler provides the C<controller_role> keyword. It is an extension of the
856ac9a7 80L<MooseX::Declare::Syntax::Keyword::Role> handler. Like with declared
81controllers, the C<method> keyword and the modifiers are provided. For details
82on the syntax for action declarations have a look at
83L<CatalystX::Declare::Keyword::Action>, which also documents the effects of
84method modifiers on actions.
85
2387a55a 86=head2 Parameters
87
88You can use a parameter signature containing named parameters for a role. To
89apply the controller role in the L</SYNOPSIS>, you'd use code like this:
90
91 controller MyApp::Web::Controller::Hello {
92 with 'MyApp::Web::ControllerRole::WithParam' => { msg => 'Hello!' };
93
94 action base under '/' as '';
95 }
96
856ac9a7 97=head1 SUPERCLASSES
98
99=over
100
101=item L<MooseX::Declare::Syntax::Keyword::Role>
102
103=back
104
105=head1 METHODS
106
107=head2 add_namespace_customizations
108
109 Object->add_namespace_customizations (Object $ctx, Str $package)
110
111This hook is called by L<MooseX::Declare> and will set the package up as a role
112and apply L<MooseX::MethodAttributes>.
113
114=head2 default_inner
115
116 ArrayRef[Object] Object->default_inner ()
117
118Same as L<CatalystX::Declare::Keyword::Class/default_inner>.
119
120=head1 SEE ALSO
121
122=over
123
124=item L<CatalystX::Declare>
125
126=item L<MooseX::Declare/role>
127
128=item L<CatalystX::Declare::Keyword::Action>
129
130=item L<CatalystX::Declare::Keyword::Controller>
131
132=back
133
134=head1 AUTHOR
135
136See L<CatalystX::Declare/AUTHOR> for author information.
137
138=head1 LICENSE
139
140This program is free software; you can redistribute it and/or modify it under
141the same terms as perl itself.
142
143=cut