use MooseX::Declare;
use MooseX::Role::Parameterized ();
-class CatalystX::Declare::Keyword::Action
- with MooseX::Declare::Syntax::KeywordHandling {
+class CatalystX::Declare::Keyword::Action {
use Carp qw( croak );
use constant STOP_PARSING => '__MXDECLARE_STOP_PARSING__';
use constant UNDER_VAR => '$CatalystX::Declare::SCOPE::UNDER';
+ use constant UNDER_STACK => '@CatalystX::Declare::SCOPE::UNDER_STACK';
use aliased 'CatalystX::Declare::Action::CatchValidationError';
+ use aliased 'CatalystX::Declare::Context::StringParsing';
use aliased 'MooseX::Method::Signatures::Meta::Method';
use aliased 'MooseX::MethodAttributes::Role::Meta::Method', 'AttributeRole';
use aliased 'MooseX::MethodAttributes::Role::Meta::Role', 'AttributeMetaRole';
for @populators;
unless ($attributes{Private}) {
- $attributes{PathPart} ||= "'$name'";
+ $attributes{PathPart} ||= $name;
delete $attributes{CaptureArgs}
if exists $attributes{Args};
if ($attributes{Private}) {
delete $attributes{ $_ }
- for qw( Args CaptureArgs Chained Signature Subname Action );
+ for qw( Args CaptureArgs Chained Signature Private );
}
+ $self->_inject_attributes($ctx, \%attributes);
+
if ($ctx->peek_next_char eq '{') {
$ctx->inject_if_block($ctx->scope_injector_call . $method->injectable_code);
}
);
}
- my @attributes;
- for my $attr (keys %attributes) {
- push @attributes,
- map { sprintf '%s%s', $attr, defined($_) ? sprintf('(%s)', $_) : '' }
- (ref($attributes{ $attr }) eq 'ARRAY')
- ? @{ $attributes{ $attr } }
- : $attributes{ $attr };
- }
+ my $compile_attrs = sub {
+ my $attributes = shift;;
+ my @attributes;
+
+ for my $attr (keys %$attributes) {
+ my $value = $attributes->{ $attr };
- return $ctx->shadow(sub (&) {
+ next if $attr eq 'Chained' and $value eq UNDER_VAR;
+
+# $value = sprintf "'%s'", $value
+# if grep { $attr eq $_ } qw( Chained PathPart );
+
+ push @attributes,
+ map { sprintf '%s%s', $attr, defined($_) ? sprintf('(%s)', $_) : '' }
+ (ref($value) eq 'ARRAY')
+ ? @$value
+ : $value;
+ }
+
+ return \@attributes;
+ };
+
+ return $ctx->shadow(sub {
my $class = caller;
+ my $attrs = shift;
my $body = shift;
+ my $name = $attrs->{Subname};
- $method->_set_actual_body($body);
- $method->{attributes} = \@attributes;
+ $body = $attrs and $attrs = {}
+ if ref $attrs eq 'CODE';
+
+ unless ($attrs->{Private}) {
+
+ $attrs->{PathPart} = $attrs->{Subname}
+ unless defined $attrs->{PathPart};
+ }
+
+ delete $attrs->{Chained}
+ unless defined $attrs->{Chained};
+
+ defined($attrs->{ $_ }) and $attrs->{ $_ } = sprintf "'%s'", $attrs->{ $_ }
+ for qw( Chained PathPart );
+
+# pp \%attributes;
+# pp $attrs;
+ my %full_attrs = (%attributes, %$attrs);
+# pp \%full_attrs;
+ my $compiled_attrs = $compile_attrs->(\%full_attrs);
+# pp $compiled_attrs;
+
+ my $real_method = $method->reify(
+ actual_body => $body,
+ attributes => $compiled_attrs,
+ name => $name,
+ );
if ($modifier) {
- add_method_modifier $class, $modifier, [$name, $method];
+ add_method_modifier $class, $modifier, [$name, $real_method];
}
else {
my $prepare_meta = sub {
my ($meta) = @_;
- $meta->add_method($name, $method);
- $meta->register_method_attributes($meta->name->can($method->name), \@attributes);
+ $meta->add_method($name, $real_method);
+ $meta->register_method_attributes($meta->name->can($real_method->name), $compiled_attrs);
};
if ($ctx->stack->[-1] and $ctx->stack->[-1]->is_parameterized) {
method _handle_action_option (Object $ctx, HashRef $attrs) {
# action name
- my $name = $ctx->strip_name
+ my $name = $self->_strip_actionpath($ctx, interpolate => 1)
or croak "Anonymous actions not yet supported";
$ctx->skipspace;
$attrs->{Action} = [];
push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, CatchValidationError;
-
- if (defined $CatalystX::Declare::SCOPE::UNDER) {
- $attrs->{Chained} ||= $CatalystX::Declare::SCOPE::UNDER;
- }
+ $attrs->{Chained} ||= UNDER_VAR;
return unless $populator;
return $populator;
method _handle_under_option (Object $ctx, HashRef $attrs) {
- my $target = $self->_strip_actionpath($ctx);
+ my $target = $self->_strip_actionpath($ctx, interpolate => 1);
$ctx->skipspace;
if ($ctx->peek_next_char eq '{' and $self->identifier eq 'under') {
$ctx->inject_if_block(
- sprintf '%s; local %s; BEGIN { %s = qq(%s) };',
- $ctx->scope_injector_call,
- UNDER_VAR,
+ $ctx->scope_injector_call .
+ sprintf ';local %s = %s;',
UNDER_VAR,
$target,
);
return STOP_PARSING;
}
- $attrs->{Chained} = "'$target'";
+ $attrs->{Chained} = $target;
return sub {
my $method = shift;
$ctx->skipspace;
- my $path = $self->_strip_actionpath($ctx);
- $attrs->{PathPart} = "'$path'";
+ my $path = $self->_strip_actionpath($ctx, interpolate => 1);
+ $attrs->{PathPart} = $path;
return;
}
return 0;
}
- method _strip_actionpath (Object $ctx) {
+ method _inject_attributes (Object $ctx, HashRef $attrs) {
+
+ my @inject = qw( Chained PathPart Subname );
+
+ my $code = sprintf ' +{ %s }, sub ',
+ join ', ',
+ map { (@$_) }
+ map { defined( $_->[1] ) ? $_ : [$_->[0], 'undef'] }
+ map { [pp($_), $attrs->{ $_ }] }
+ grep { defined $attrs->{ $_ } }
+ @inject;
+
+ $ctx->inject_code_parts_here($code);
+ $ctx->inc_offset(length $code);
+ }
+
+ method _strip_actionpath (Object $ctx, :$interpolate?) {
$ctx->skipspace;
my $linestr = $ctx->get_linestr;
my $rest = substr($linestr, $ctx->offset);
+ my $interp = sub { $interpolate ? "'$_[0]'" : $_[0] };
if ($rest =~ /^ ( [_a-z] [_a-z0-9]* ) \b/ix) {
substr($linestr, $ctx->offset, length($1)) = '';
$ctx->set_linestr($linestr);
- return $1;
+ return $interp->($1);
}
elsif ($rest =~ /^ ' ( (?:[.:;,_a-z0-9]|\/)* ) ' /ix) {
substr($linestr, $ctx->offset, length($1) + 2) = '';
$ctx->set_linestr($linestr);
- return $1;
+ return $interp->($1);
+ }
+ elsif ($interpolate and my $str = $ctx->get_string) {
+ return $str;
}
else {
croak "Invalid syntax for action path: $rest";
}
}
+
+ with 'MooseX::Declare::Syntax::KeywordHandling';
+
+ around context_traits { $self->$orig, StringParsing }
}
__END__
# the final keyword can be used to be more
# visually explicit about end-points
final action some_action { ... }
+
+ # type dispatching works
+ final action with_str (Str $x) as via_type;
+ final action with_int (Int $x) as via_type;
}
# of course you can also chain to external actions
$ctx->stash(item => $self->get_item);
}
+=head2 Why $ctx instead of $c
+
+Some might ask why the context object is called C<$ctx> instead of the usual
+C<$c>. The reason is simple: It's an opinionated best practice, since C<$ctx>
+stands out more.
+
=head2 Setting a Path Part
As usual with Catalyst actions, the path part (the public name of this part of
=head2 Arguments
You can use signatures like you are use to from L<MooseX::Method::Signatures>
-to declare action parameters. The number of arguments will be used during
-dispatching. Dispatching by type constraint is planned but not yet implemented.
+to declare action parameters. The number of positinoal arguments will be used
+during dispatching as well as their types.
The signature follows the action name:
=head2 Validation
-Currently, when the arguments do not fit the signature because of a L<Moose>
-validation error, the response body will be set to C<Not found> and the
-status to C<404>. This only applies when debug mode is off. If it is turned on,
-the error message will be prefixed with C<BAD REQUEST: >. The action will
-automatically detach after a failed signature validation.
+The signatures are now validated during dispatching-time, and an action with
+a non-matching signature (number of positional arguments and their types) will
+not be dispatched to. This means that
+
+ action base under '/' as '';
+
+ under base {
+
+ final as double, action double_integer (Int $x) {
+ $ctx->response->body( $x * 2 );
+ }
+
+ final as double, action double_string (Str $x) {
+ $ctx->response->body( $x x 2 );
+ }
+ }
+
+will return C<foofoo> when called as C</double/foo> and C<46> when called as
+C</double/23>.
=head2 Actions and Method Modifiers