reverted meaningless code style changes
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Keyword / Action.pm
index 8c0d879..b90ede1 100644 (file)
@@ -9,6 +9,7 @@ class CatalystX::Declare::Keyword::Action {
     use Data::Dump          qw( pp );
     use MooseX::Types::Util qw( has_available_type_export );
     use Moose::Util         qw( add_method_modifier ensure_all_roles );
+    use Data::Pond qw(pond_write_datum);
     use Class::Inspector;
     use Class::MOP;
 
@@ -80,6 +81,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,
@@ -139,10 +144,12 @@ class CatalystX::Declare::Keyword::Action {
                 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;
+                    map {
+                        my $value = ref $_ ? pond_write_datum($_) : $_;
+                        sprintf '%s%s', $attr, defined($value) ? sprintf('(%s)', $value) : ''
+                    } (
+                        ref($value) eq 'ARRAY'
+                    ) ? @$value : $value;
             }
 
             return \@attributes;
@@ -180,7 +187,6 @@ class CatalystX::Declare::Keyword::Action {
             # 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,
@@ -189,7 +195,6 @@ class CatalystX::Declare::Keyword::Action {
 
             # NYI
             if ($modifier) {
-
                 add_method_modifier $class, $modifier, [$name, $real_method];
             }
             else {
@@ -211,24 +216,40 @@ class CatalystX::Declare::Keyword::Action {
 
                     $real_meta->$prepare_meta;
                 }
-
-                $class->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 $value = ref $params{$key} eq 'ARRAY' ? $params{$key} : [$params{$key}];
+                    push @{ $attrs->{$key} ||=[] }, @$value;
+                    ##push @{ $attrs->{$key} ||=[] }, $params;
+
+                }
+            }          
 
-        push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, $role;
+            if (defined(my $alias = $self->_check_for_available_import($ctx, $role))) {
+                $role = $alias;
+            }
+            push @roles, $role;
+        }
 
+        push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, @roles;
         return;
     }
 
@@ -280,7 +301,7 @@ class CatalystX::Declare::Keyword::Action {
         $attrs->{Signature} = $proto;
         $attrs->{Action}    = [];
 
-        push @{ $attrs->{CatalystX_Declarative_ActionRoles} ||= [] }, CatchValidationError;
+        push @{ $attrs->{CatalystX_Declarative_DefaultActionRoles} ||= [] }, CatchValidationError;
 
         # default chained base to the global under var, to be resolved at runtime
         $attrs->{Chained} ||= UNDER_VAR;
@@ -304,6 +325,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;
 
@@ -649,6 +673,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:
 
@@ -715,6 +743,75 @@ 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>
@@ -734,6 +831,14 @@ what class to use:
 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