more docs and examples
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Keyword / Action.pm
index b60cc21..eb4f20e 100644 (file)
@@ -1,24 +1,27 @@
 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 Perl6::Junction     qw( any );
     use Data::Dump          qw( pp );
     use MooseX::Types::Util qw( has_available_type_export );
-    use Moose::Util         qw( add_method_modifier );
+    use Moose::Util         qw( add_method_modifier ensure_all_roles );
     use Class::Inspector;
     use Class::MOP;
 
 
     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';
 
 
     method parse (Object $ctx, Str :$modifier?, Int :$skipped_declarator = 0) {
@@ -77,6 +80,10 @@ class CatalystX::Declare::Keyword::Action
 
         my $name   = $attributes{Subname};
 
+        if ($attributes{Private}) {
+            $attributes{Signature} ||= '@';
+        }
+
         my $method = Method->wrap(
             signature       => qq{($attributes{Signature})},
             package_name    => $ctx->get_curstash_name,
@@ -93,7 +100,7 @@ class CatalystX::Declare::Keyword::Action
             for @populators;
 
         unless ($attributes{Private}) {
-            $attributes{PathPart} ||= "'$name'";
+            $attributes{PathPart} ||= $name;
 
             delete $attributes{CaptureArgs}
                 if exists $attributes{Args};
@@ -105,12 +112,18 @@ class CatalystX::Declare::Keyword::Action
 
         if ($attributes{Private}) {
             delete $attributes{ $_ }
-                for qw( Args CaptureArgs Chained Signature Subname Action );
+                for qw( Args CaptureArgs Chained Signature Private );
         }
 
+        # inject a hashref for resolving runtime attribute values
+        $self->_inject_attributes($ctx, \%attributes);
+
+        # our declaration is followed by a block
         if ($ctx->peek_next_char eq '{') {
             $ctx->inject_if_block($ctx->scope_injector_call . $method->injectable_code);
         }
+
+        # there is no block, so we insert one.
         else {
             $ctx->inject_code_parts_here(
                 sprintf '{ %s%s }',
@@ -119,45 +132,122 @@ class CatalystX::Declare::Keyword::Action
             );
         }
 
-        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 };
+
+                # the compiletime chained attr might contain the under global var name
+                next if $attr eq 'Chained' and $value eq UNDER_VAR;
+
+                push @attributes, 
+                    map { sprintf '%s%s', $attr, defined($_) ? sprintf('(%s)', $_) : '' }
+                        (ref($value) eq 'ARRAY') 
+                        ? @$value
+                        : $value;
+            }
+
+            return \@attributes;
+        };
 
-        return $ctx->shadow(sub (&) {
+        return $ctx->shadow(sub {
             my $class = caller;
+            my $attrs = shift;
             my $body  = shift;
 
-            $method->_set_actual_body($body);
-            $method->{attributes} = \@attributes;
+            # the runtime-resolved name
+            my $name  = $attrs->{Subname};
+
+            # in case no hashref was specified
+            $body = $attrs and $attrs = {}
+                if ref $attrs eq 'CODE';
+
+            # default path part to runtime-resolved name
+            unless ($attrs->{Private}) {
+
+                $attrs->{PathPart} = $attrs->{Subname}
+                    unless defined $attrs->{PathPart};
+            }
+
+            # in CXD we are explicit about chained values, an undefined
+            # value means we defaulted to the outer-scope under and there
+            # was none.
+            delete $attrs->{Chained}
+                unless defined $attrs->{Chained};
 
+            # some attrs need to be single quoted in their stringified forms
+            defined($attrs->{ $_ }) and $attrs->{ $_ } = sprintf "'%s'", $attrs->{ $_ }
+                for qw( Chained PathPart );
+
+            # merge runtime and compiletime attrs
+            my %full_attrs = (%attributes, %$attrs);
+            my $compiled_attrs = $compile_attrs->(\%full_attrs);
+
+            my $real_method = $method->reify(
+                actual_body => $body,
+                attributes  => $compiled_attrs,
+                name        => $name,
+            );
+
+            # NYI
             if ($modifier) {
 
-                add_method_modifier $class, $modifier, [$name, $method];
+                add_method_modifier $class, $modifier, [$name, $real_method];
             }
             else {
 
-                $class->meta->add_method($name, $method);
-                $class->meta->register_method_attributes($class->can($method->name), \@attributes);
+                my $prepare_meta = sub {
+                    my ($meta) = @_;
+
+                    $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) {
+                    my $real_meta = MooseX::Role::Parameterized->current_metaclass;
+
+                    $real_meta->meta->make_mutable
+                        if $real_meta->meta->is_immutable;
+                    ensure_all_roles $real_meta->meta, AttributeMetaRole
+                        if $real_meta->isa('Moose::Meta::Role');
+
+                    $real_meta->$prepare_meta;
+                }
+                else {
+
+                    $class->meta->$prepare_meta;
+                }
             }
         });
     }
 
     method _handle_with_option (Object $ctx, HashRef $attrs) {
 
-        my $role = $ctx->strip_name
-            or croak "Expected bareword role specification for action after with";
+        my @roles_with_args = ();
+        push @roles_with_args, @{ $ctx->strip_names_and_args };
 
         # we need to fish for aliases here since we are still unclean
-        if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
-            $role = $alias;
+        my @roles = ();
+        for my $role_with_arg(@roles_with_args) {
+            my ($role, $params) = @{$role_with_arg};
+            if($params) {
+                my ($first, @rest) = eval $params;
+                my %params = ref $first eq 'HASH' ? %$first : ($first, @rest); # both (%opts) and {%opts}
+                for my $key (keys %params) {
+                    my $parameters = ref $params{$key} eq 'ARRAY' ? @{$params{$key}} : $params{$key};
+                    push @{$attrs->{$key}}, $parameters;
+                }
+            }          
+
+            if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
+                $role = $alias;
+            }
+            push @roles, $role;
         }
 
-        push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, $role;
+        push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, @roles;
 
         return;
     }
@@ -188,12 +278,13 @@ class CatalystX::Declare::Keyword::Action
     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;
         my $populator;
 
+        # shortcut under base option is basically handled by the under handler
         if (substr($ctx->get_linestr, $ctx->offset, 2) eq '<-') {
             my $linestr = $ctx->get_linestr;
             substr($linestr, $ctx->offset, 2) = '';
@@ -211,9 +302,8 @@ class CatalystX::Declare::Keyword::Action
 
         push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, CatchValidationError;
 
-        if (defined $CatalystX::Declare::SCOPE::UNDER) {
-            $attrs->{Chained} ||= $CatalystX::Declare::SCOPE::UNDER;
-        }
+        # default chained base to the global under var, to be resolved at runtime
+        $attrs->{Chained} ||= UNDER_VAR;
 
         return unless $populator;
         return $populator;
@@ -234,6 +324,9 @@ class CatalystX::Declare::Keyword::Action
 
     method _build_flag_populator (Object $ctx, HashRef $attrs, Str $what) {
 
+        $attrs->{Private} = []
+            if $what eq 'private';
+
         return sub {
             my $method = shift;
 
@@ -248,21 +341,20 @@ class CatalystX::Declare::Keyword::Action
 
     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;
@@ -282,14 +374,14 @@ class CatalystX::Declare::Keyword::Action
 
         $ctx->skipspace;
 
-        my $path = $self->_strip_actionpath($ctx);
-        $attrs->{PathPart} = "'$path'";
+        my $path = $self->_strip_actionpath($ctx, interpolate => 1);
+        $attrs->{PathPart} = $path;
 
         return;
     }
 
     method _count_positional_arguments (Object $method) {
-        my $signature = $method->_parsed_signature;
+        my $signature = $method->parsed_signature;
 
         if ($signature->has_positional_params) {
             my $count = @{ scalar($signature->positional_params) };
@@ -304,26 +396,61 @@ class CatalystX::Declare::Keyword::Action
         return 0;
     }
 
-    method _strip_actionpath (Object $ctx) {
+    method _inject_attributes (Object $ctx, HashRef $attrs) {
+
+        # attrs that need to be runtime-resolved
+        my @inject = qw( Chained PathPart Subname );
+
+        # turn specific attributes into a hashref
+        my $code = sprintf ' +{ %s }, sub ',        # the ', sub ' turns method +{ ... } { ... } into 
+            join ', ',                              #   method +{ ... }, sub { ... }
+            map  { (@$_) }
+            map  { defined( $_->[1] ) ? $_ : [$_->[0], 'undef'] }
+            map  { [pp($_), $attrs->{ $_ }] }
+            grep { defined $attrs->{ $_ } }
+            @inject;
+
+        # inject the hashref code before the action body
+        $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] };
 
+        # find simple barewords
         if ($rest =~ /^ ( [_a-z] [_a-z0-9]* ) \b/ix) {
             substr($linestr, $ctx->offset, length($1)) = '';
             $ctx->set_linestr($linestr);
-            return $1;
+            return $interp->($1);
         }
+
+        # allow single quoted more complex barewords
         elsif ($rest =~ /^ ' ( (?:[.:;,_a-z0-9]|\/)* ) ' /ix) {
             substr($linestr, $ctx->offset, length($1) + 2) = '';
             $ctx->set_linestr($linestr);
-            return $1;
+            return $interp->($1);
+        }
+
+        # double quoted strings and variables
+        elsif ($interpolate and my $str = $ctx->get_string) {
+            return $str;
         }
+
+        # not suitable as action path
         else {
             croak "Invalid syntax for action path: $rest";
         }
     }
+    
+    # down here because it requires the parse method
+    with 'MooseX::Declare::Syntax::KeywordHandling';
+
+    around context_traits { $self->$orig, StringParsing }
 }
 
 __END__
@@ -371,6 +498,10 @@ CatalystX::Declare::Keyword::Action - Declare Catalyst Actions
             # 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
@@ -412,6 +543,12 @@ C<get_item> method:
         $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
@@ -510,8 +647,8 @@ specify an action after a C<E<lt>-> following the action name:
 =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:
 
@@ -535,6 +672,10 @@ Named parameters will be populated with the values in the query parameters:
     # /view/17/?page=3
     final action view (Int $id, Int :$page = 1) under '/';
 
+If you specify a query parameter to be an C<ArrayRef>, it will be specially
+handled. For one, it will match even if there is no such value in the
+parameters. Second, it will always be wrapped as an array reference.
+
 Your end-points can also take an unspecified amount of arguments by specifying
 an array as a variable:
 
@@ -543,10 +684,25 @@ an array as a variable:
 
 =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<Bad Request> and the
-status to C<400>. This only applies when debug mode is off. If it is turned on,
-the error message will be prefixed with C<BAD REQUEST: >.
+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
 
@@ -559,7 +715,7 @@ C<base> action:
 
     use CatalystX::Declare;
 
-    component_role MyApp::Web::ControllerRole::RichBase {
+    controller_role MyApp::Web::ControllerRole::RichBase {
 
         before base (Object $ctx) {
             $ctx->stash(something => $ctx->model('Item'));
@@ -586,6 +742,102 @@ consume the C<RichBase> role declared above:
         }
     }
 
+You can consume multiple action roles similarly to the way you do with the
+class or role keyword:
+
+    action user
+    with LoggedIn
+    with isSuperUser {}
+
+Or
+
+    action User
+    with (LoggedIn, isSuperUser) {}
+
+Lastly, you can pass parameters to the underlying L<Catalyst::Action> using
+a syntax that is similar to method traits:
+
+    action myaction with hasRole(opt1=>'val1', opt2=>'val2')
+
+Where C<%opts> is a hash that is used to populate $action->attributes in the
+same way you might have done the following in classic L<Catalyst>
+
+    sub myaction :Action :Does(hasRole) :opt1(val1) :opt2(val2)
+
+Here's a more detailed example:
+
+    action User
+    with hasLogger(log_engine=>'STDOUT')
+    with hasPermissions(
+        role=>['Administrator', 'Member'],
+    ) {}
+
+Think of these are classic catalyst subroutine attributes on steriods.  Unlike
+subroutine attributes, you can split and format your code across multiple lines
+and you can use deep and complex data structures such as HashRefs or ArrayRefs.
+Also, since the parameters are grouped syntactically within the C<with> keyword
+this should improve readability of your code, since it will be more clear which
+parameters belong to with roles.  This should give L<CatalystX::Declare> greater
+compatibility with legacy L<Catalyst> code but offer us a way forward from 
+needing subroutine attributes, which suffer from significant drawbacks.
+
+A few caveats and differences from method traits.  First of all, unlike method
+traits, parameters are not passed to the L<Catalyst::Action> constructor, but 
+instead used to populate the C<attributes> attribute, which is to preserve
+compatibility with how subroutine attributes work in classic L<Catalyst>.
+
+Additionally, since subroutines attributes supported a very limited syntax for
+supplying values, we follow the convention where parameter values are pushed 
+onto an arrayref.  In other words the following:
+
+    action User with hasLogger(engine=>'STDOUT')
+
+would create the following data structure:
+
+    $action->attributes->{engine} = ['STDOUT']
+
+The one exception is that if the value is an arrayref, those will be merged:
+
+    action User with Permissions(roles=>[qw/admin member/]) {}
+    ## Creates: $action->attributes->{roles} = ['admin','member']
+
+My feeling is that this gives better backward compatibility with classic sub
+attributes:
+
+    sub User :Action :Does(Permissions) :roles(admin) :roles(member)
+
+However, I realize this method could lead to namespace collisions within the
+C<$action->attributes> attribute.  For now this is an avoidable issue.  In the
+future we may add a C<$action->trait_attributes> or similar attribute to the
+L<Catalyst::Action> class in order to resolve this issue.
+
+=head2 Action Classes
+
+B<This option is even more experimental>
+
+You might want to create an action with a different class than the usual
+L<Catalyst::Action>. A usual suspect here is L<Catalyst::Action::RenderView>.
+You can use the C<isa> option (did I mention it's experimental?) to specify
+what class to use:
+
+    controller MyApp::Web::Controller::Root {
+
+        $CLASS->config(namespace => '');
+
+        action end isa RenderView;
+    }
+
+The loaded class will be L<Moose>ified, so we are able to apply essential
+roles.
+
+=head2 Private Actions
+
+B<This option is a bit less, but still pretty experimental>
+
+You can declare private actions with the C<is private> trait:
+
+    action end is private isa RenderView;
+
 =head1 ROLES
 
 =over