added action classes and roles
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declarative / Keyword / Action.pm
1 use MooseX::Declare;
2
3 class CatalystX::Declarative::Keyword::Action
4     with MooseX::Declare::Syntax::KeywordHandling {
5
6
7     use Carp                qw( croak );
8     use Perl6::Junction     qw( any );
9     use Data::Dump          qw( pp );
10     use MooseX::Types::Util qw( has_available_type_export );
11     use Class::Inspector;
12     use Class::MOP;
13
14
15     use constant STOP_PARSING   => '__MXDECLARE_STOP_PARSING__';
16     use constant UNDER_VAR      => '$CatalystX::Declarative::SCOPE::UNDER';
17
18     use aliased 'MooseX::Method::Signatures::Meta::Method';
19     use aliased 'MooseX::MethodAttributes::Role::Meta::Method', 'AttributeRole';
20
21
22     method parse (Object $ctx) {
23
24         # somewhere to put the attributes
25         my %attributes;
26         my @populators;
27         my $skipped_declarator;
28
29         # parse declarations
30         until (do { $ctx->skipspace; $ctx->peek_next_char } eq any qw( ; { } )) {
31
32             $ctx->skipspace;
33             
34             # optional commas
35             if ($ctx->peek_next_char eq ',') {
36
37                 my $linestr = $ctx->get_linestr;
38                 substr($linestr, $ctx->offset, 1) = '';
39                 $ctx->set_linestr($linestr);
40
41                 next;
42             }
43
44             # next thing should be an option name
45             my $option = (
46                 $skipped_declarator 
47                 ? $ctx->strip_name 
48                 : do { 
49                     $ctx->skip_declarator; 
50                     $skipped_declarator++;
51                     $ctx->declarator;
52                 })
53               or croak "Expected option token, not " . substr($ctx->get_linestr, $ctx->offset);
54
55             # we need to be able to handle the rest
56             my $handler = $self->can("_handle_${option}_option")
57                 or croak "Unknown action option: $option";
58
59             # call the handler
60             my $populator = $self->$handler($ctx, \%attributes);
61
62             if ($populator and $populator eq STOP_PARSING) {
63
64                 return $ctx->shadow(sub (&) {
65                     my ($body) = @_;
66                     return $body->();
67                 });
68             }
69
70             push @populators, $populator
71                 if defined $populator;
72         }
73
74         croak "Need an action specification"
75             unless exists $attributes{Signature};
76
77         my $name   = $attributes{Subname};
78         my $method = Method->wrap(
79             signature       => qq{($attributes{Signature})},
80             package_name    => $ctx->get_curstash_name,
81             name            => $name,
82         );
83
84         AttributeRole->meta->apply($method);
85
86         $_->($method)
87             for @populators;
88
89         $attributes{PathPart} ||= "'$name'";
90
91         delete $attributes{CaptureArgs}
92             if exists $attributes{Args};
93
94         $attributes{CaptureArgs} = 0
95             unless exists $attributes{Args}
96                 or exists $attributes{CaptureArgs};
97
98         if ($ctx->peek_next_char eq '{') {
99             $ctx->inject_if_block($ctx->scope_injector_call . $method->injectable_code);
100         }
101         else {
102             $ctx->inject_code_parts_here(
103                 sprintf '{ %s%s }',
104                     $ctx->scope_injector_call,
105                     $method->injectable_code,
106             );
107         }
108
109         my @attributes = map { 
110             join('',
111                 $_,
112                 sprintf('(%s)',
113                     ref($attributes{ $_ }) eq 'ARRAY'
114                     ? join(' ', @{ $attributes{ $_ } })
115                     : $attributes{ $_ }
116                 ),
117             );
118         } keys %attributes;
119
120         return $ctx->shadow(sub (&) {
121             my $class = caller;
122
123             $method->_set_actual_body(shift);
124             $method->{attributes} = \@attributes;
125     
126             $class->meta->add_method($name, $method);
127             $class->meta->register_method_attributes($class->can($method->name), \@attributes);
128         });
129     }
130
131     method _handle_with_option (Object $ctx, HashRef $attrs) {
132
133         my $role = $ctx->strip_name
134             or croak "Expected bareword role specification for action after with";
135
136         # we need to fish for aliases here since we are still unclean
137         if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
138             $role = $alias;
139         }
140
141         push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, $role;
142
143         return;
144     }
145
146     method _handle_isa_option (Object $ctx, HashRef $attrs) {
147
148         my $class = $ctx->strip_name
149             or croak "Expected bareword action class specification for action after isa";
150
151         if (defined(my $alias = $self->_check_for_available_import($ctx, $class))) {
152             $class = $alias;
153         }
154
155         $attrs->{CatalystX_Declarative_ActionClass} = $class;
156
157         return;
158     }
159
160     method _check_for_available_import (Object $ctx, Str $name) {
161
162         if (my $code = $ctx->get_curstash_name->can($name)) {
163             return $code->();
164         }
165
166         return undef;
167     }
168
169     method _handle_action_option (Object $ctx, HashRef $attrs) {
170
171         # action name
172         my $name = $ctx->strip_name
173             or croak "Anonymous actions not yet supported";
174
175         # signature
176         my $proto = $ctx->strip_proto || '';
177         $proto = join(', ', 'Object $self: Object $ctx', $proto || ());
178
179         $attrs->{Subname}   = $name;
180         $attrs->{Signature} = $proto;
181
182         if (defined $CatalystX::Declarative::SCOPE::UNDER) {
183             $attrs->{Chained} ||= $CatalystX::Declarative::SCOPE::UNDER;
184         }
185
186         return;
187     }
188
189     method _handle_is_option (Object $ctx, HashRef $attrs) {
190
191         my $what = $ctx->strip_name
192             or croak "Expected symbol token after is symbol, not " . substr($ctx->get_linestr, $ctx->offset);
193
194         return sub {
195             my $method = shift;
196
197             if ($what eq any qw( end endpoint final )) {
198                 my $count = $self->_count_positional_arguments($method);
199                 $attrs->{Args} = defined($count) ? $count : '';
200             }
201             elsif ($what eq 'private') {
202                 $attrs->{Private} = 1;
203             }
204         };
205     }
206
207     method _handle_under_option (Object $ctx, HashRef $attrs) {
208
209         my $target = $self->_strip_actionpath($ctx);
210         $ctx->skipspace;
211
212         if ($ctx->peek_next_char eq '{' and $self->identifier eq 'under') {
213             $ctx->inject_if_block(
214                 sprintf '%s; local %s; BEGIN { %s = qq(%s) };',
215                     $ctx->scope_injector_call,
216                     UNDER_VAR,
217                     UNDER_VAR,
218                     $target,
219             );
220             return STOP_PARSING;
221         }
222
223         $attrs->{Chained} = "'$target'";
224
225         return sub {
226             my $method = shift;
227
228             my $count = $self->_count_positional_arguments($method);
229             $attrs->{CaptureArgs} = $count
230                 if defined $count;
231         };
232     }
233
234     method _handle_chains_option (Object $ctx, HashRef $attrs) {
235
236         $ctx->skipspace;
237         $ctx->strip_name eq 'to'
238             or croak "Expected to token after chains symbol, not " . substr($ctx->get_linestr, $ctx->offset);
239
240         return $self->_handle_under_option($ctx, $attrs);
241     }
242
243     method _handle_as_option (Object $ctx, HashRef $attrs) {
244
245         $ctx->skipspace;
246
247         my $path = $self->_strip_actionpath($ctx);
248         $attrs->{PathPart} = "'$path'";
249
250         return;
251     }
252
253     method _count_positional_arguments (Object $method) {
254         my $signature = $method->_parsed_signature;
255
256         if ($signature->has_positional_params) {
257             my $count = @{ scalar($signature->positional_params) };
258
259             if ($count and ($signature->positional_params)[-1]->sigil eq '@') {
260                 return undef;
261             }
262
263             return $count - 1;
264         }
265
266         return 0;
267     }
268
269     method _strip_actionpath (Object $ctx) {
270
271         $ctx->skipspace;
272         my $linestr = $ctx->get_linestr;
273         my $rest    = substr($linestr, $ctx->offset);
274
275         if ($rest =~ /^ ( [_a-z] [_a-z0-9]* ) \b/ix) {
276             substr($linestr, $ctx->offset, length($1)) = '';
277             $ctx->set_linestr($linestr);
278             return $1;
279         }
280         elsif ($rest =~ /^ ' ( (?:[.:;,_a-z0-9]|\/)* ) ' /ix) {
281             substr($linestr, $ctx->offset, length($1) + 2) = '';
282             $ctx->set_linestr($linestr);
283             return $1;
284         }
285         else {
286             croak "Invalid syntax for action path: $rest";
287         }
288     }
289 }
290
291
292