[Bps-public-commit] r17227 - in Prophet/branches/actions/lib/Prophet: Server Server/ViewHelpers Web

jesse at bestpractical.com jesse at bestpractical.com
Mon Dec 15 02:47:27 EST 2008


Author: jesse
Date: Mon Dec 15 02:47:27 2008
New Revision: 17227

Added:
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/ParamFromFunction.pm
Modified:
   Prophet/branches/actions/lib/Prophet/Server.pm
   Prophet/branches/actions/lib/Prophet/Server/Controller.pm
   Prophet/branches/actions/lib/Prophet/Server/View.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm
   Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
   Prophet/branches/actions/lib/Prophet/Util.pm
   Prophet/branches/actions/lib/Prophet/Web/FunctionResult.pm

Log:
* web functions can now be ordered
* web functions can pull prop values from previously run functions

Modified: Prophet/branches/actions/lib/Prophet/Server.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server.pm	Mon Dec 15 02:47:27 2008
@@ -108,9 +108,8 @@
         app_handle => $self->app_handle,
         result => $self->result
     );
-    $controller->handle_actions();
+    $controller->handle_functions();
 
-    warn YAML::Dump($self->result); use YAML;
 
     my $dispatcher_class = ref( $self->app_handle ) . "::Server::Dispatcher";
     if ( !$self->app_handle->try_to_require($dispatcher_class) ) {
@@ -302,8 +301,8 @@
 
     print "HTTP/1.0 200 OK\r\n";
     print "Content-Type: " . $args{'content_type'} . "\r\n";
-    print "Content-Length: " . length( $args{'content'} ) . "\r\n\r\n";
-    print $args{'content'};
+    print "Content-Length: " . length( $args{'content'} ||'' ) . "\r\n\r\n";
+    print $args{'content'} || '';
     return '200';
 }
 

Modified: Prophet/branches/actions/lib/Prophet/Server/Controller.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/Controller.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/Controller.pm	Mon Dec 15 02:47:27 2008
@@ -5,7 +5,7 @@
 
 has cgi => (is => 'rw', isa => 'CGI');
 has failure_message => ( is => 'rw', isa => 'Str');
-has actions => (is => 'rw', isa => 'HashRef');
+has functions => (is => 'rw', isa => 'HashRef');
 has app_handle => (is => 'rw', isa => 'Prophet::App');
 has result => ( is => 'ro', isa => 'Prophet::Web::Result');
 
@@ -23,78 +23,91 @@
 
 =cut
 
-sub extract_actions_from_cgi {
+sub extract_functions_from_cgi {
     my $self = shift;
 
-    my $actions = {};
+    my $functions = {};
    foreach my $param ($self->cgi->all_parameters){
         next unless $param =~ /^prophet-function-(.*)$/;
         my $name = $1;
-        warn "Duplicate action definition for @{[$name]}." if (exists $actions->{$name});
+        warn "Duplicate function definition for @{[$name]}." if (exists $functions->{$name});
 
 
-        my $action_data = $self->cgi->param($param);
-        my $attr = $self->string_to_hash($action_data);
+        my $function_data = $self->cgi->param($param);
+        my $attr = $self->string_to_hash($function_data);
         $attr->{name} = $name;
 
-        $actions->{$name} = $attr;
-        $actions->{$name}->{params} = $self->params_for_action_from_cgi($name);
+        $functions->{$name} = $attr;
+        $functions->{$name}->{params} = $self->params_for_function_from_cgi($name);
    } 
-   $self->actions($actions);
+   $self->functions($functions);
 }
 
-sub params_for_action_from_cgi {
+sub params_for_function_from_cgi {
     my $self   = shift;
-    my $action = shift;
+    my $function = shift;
 
     my $values;
     for my $field ( $self->cgi->all_parameters ) {
-        next unless ( $field =~ /^prophet-field-function-$action-prop-(.*)$/ );
+        if ( $field =~ /^prophet-field-function-$function-prop-(.*)$/ ) {
         my $name     = $1;
-        my $meta     = {};
-        $meta->{prop} = $name;
-        $meta->{value} = $self->cgi->param($field);
-        $meta->{original_value} = $self->cgi->param( "original-value-" . $field );
-        $values->{$name} = $meta;
+        $values->{$name} = {
+            prop           => $name,
+            value          => $self->cgi->param($field),
+            original_value => $self->cgi->param( "original-value-" . $field )
+        };
+    }
+    elsif ($field =~ /^prophet-fill-function-$function-prop-(.*)$/) {
+        my $name = $1;
+        my $meta = {};
+        my $value = $self->cgi->param($field);
+        next unless ($value =~ /^function-(.*)\|result-(.*)$/);
+        $values->{$name} = {
+            prop           => $name,
+            from_function => $1,
+            from_result => $2
+        };
+    } 
+    else {
+            next;
+    }
 
     }
 
     return $values;
 }
 
-sub handle_actions {
+sub handle_functions {
     my $self = shift;
 
    my @workflow = qw(
-       extract_actions_from_cgi 
-       canonicalize_actions
-       validate_actions
-       execute_actions    
+       extract_functions_from_cgi 
+       canonicalize_functions
+       validate_functions
+       execute_functions    
     );
     eval {
         $self->$_() for @workflow;
     }; 
     
     if (my $err = $@) {
+        warn "This run failed - $err";
         $self->result->success(0);
         $self->result->message($err);   
     }
+
 }
 
-sub canonicalize_actions {
+sub canonicalize_functions {
     my $self    = shift;
-    my $actions = $self->actions;
-    foreach my $action ( keys %$actions ) {
-        foreach my $param (
-            keys %{ $actions->{$action}->{params} }
-
-            )
-        {
-            if ( $actions->{$action}->{params}->{$param}->{original_value} eq
-                $actions->{$action}->{params}->{$param}->{value} )
-            {
-
-                delete $actions->{$action}->{params}->{$param};
+    my $functions = $self->functions;
+    foreach my $function ( keys %$functions ) {
+        foreach my $param ( keys %{ $functions->{$function}->{params} } ) {
+            if ( 
+                defined $functions->{$function}->{params}->{$param}->{original_value} &&
+                ($functions->{$function}->{params}->{$param}->{original_value} eq
+                $functions->{$function}->{params}->{$param}->{value} )) {
+                delete $functions->{$function}->{params}->{$param};
                 next;
             }
 
@@ -104,71 +117,94 @@
 
 }
 
-sub validate_actions {
+sub validate_functions {
 
 }
 
-sub execute_actions {
-    my $self = shift;
 
-    foreach my $action (keys %{$self->actions}) {
+sub fill_params_from_previous_functions {
+    my $self = shift;   
+    my $function = shift;
+
+    my $params = $self->functions->{$function}->{params};
+
+    foreach my $param ( keys %$params) {
+            if ( my $from_function = $params->{$param}->{from_function}) {
+                my $from_result = $params->{$param}->{from_result};  
+                my $func_result = $self->result->get($from_function);
+                # XXX TODO - $from_result should be locked down tighter
+                if ($func_result->can($from_result)) { 
+                    $params->{$param}->{value} = $func_result->$from_result();
+                }
+
+            }
+
+    }
+
+}
+
+sub execute_functions {
+    my $self = shift;
+    my $fs = $self->functions;
 
-        if ($self->actions->{$action}->{action} eq 'update') {
-            $self->_exec_action_update($self->actions->{$action});
-        } elsif ($self->actions->{$action}->{action} eq 'create') {
-            $self->_exec_action_create($self->actions->{$action});
+    foreach my $function ( sort { $fs->{$a}->{order} <=> $fs->{$b}->{order}}  keys %{$fs}) {
+        $self->fill_params_from_previous_functions($function); 
+        if ($fs->{$function}->{action} eq 'update') {
+            $self->_exec_function_update($fs->{$function});
+        } elsif ($fs->{$function}->{action} eq 'create') {
+            $self->_exec_function_create($fs->{$function});
         } else {
-            die "I don't know how to handle a ".$self->actions->{$action}->{action};
+            die "I don't know how to handle a ".$fs->{$function}->{action};
         }
 
     }
 
 }
 
-sub _exec_action_create {
+sub _exec_function_create {
     my $self = shift;
-    my $action = shift;
+    my $function = shift;
 
-    die $action->{class} ." is not a valid class " unless (UNIVERSAL::isa($action->{class}, 'Prophet::Record'));
-    my $object = $action->{class}->new(  app_handle => $self->app_handle);
+    die $function->{class} ." is not a valid class " unless (UNIVERSAL::isa($function->{class}, 'Prophet::Record'));
+    my $object = $function->{class}->new(  app_handle => $self->app_handle);
     my ( $val, $msg ) = $object->create(
         props => {
             map {
-                $action->{params}->{$_}->{prop} => $action->{params}->{$_}->{value}
-                } keys %{ $action->{params} }
+                $function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
+                } keys %{ $function->{params} }
             }
 
     );
 
-    my $res = Prophet::Web::FunctionResult->new( function_name => $action->{name}, 
-                                                 class => $action->{class},
+    my $res = Prophet::Web::FunctionResult->new( function_name => $function->{name}, 
+                                                 class => $function->{class},
                                                  success => $object->uuid? 1 :0,
                                                  record_uuid => $object->uuid,
                                                  msg => ($msg || 'Record created'));
                                                 
-    $self->result->set($action->{name} => $res);
+    $self->result->set($function->{name} => $res);
 }
 
-sub _exec_action_update {
+sub _exec_function_update {
     my $self = shift;
-    my $action = shift;
+    my $function = shift;
 
-    my $object = Prophet::Util->instantiate_record( uuid => $action->{uuid}, class=>$action->{class}, app_handle=> $self->app_handle);
+    my $object = Prophet::Util->instantiate_record( uuid => $function->{uuid}, class=>$function->{class}, app_handle=> $self->app_handle);
     my ( $val, $msg ) = $object->set_props(
         props => {
             map {
-                $action->{params}->{$_}->{prop} => $action->{params}->{$_}->{value}
-                } keys %{ $action->{params} }
+                $function->{params}->{$_}->{prop} => $function->{params}->{$_}->{value}
+                } keys %{ $function->{params} }
             }
 
     );
-    my $res = Prophet::Web::FunctionResult->new( function_name => $action->{name}, 
-                                                 class => $action->{class},
+    my $res = Prophet::Web::FunctionResult->new( function_name => $function->{name}, 
+                                                 class => $function->{class},
                                                  success => $val? 1 :0,
                                                  record_uuid => $object->uuid,
                                                  msg => ($msg || 'Record updated'));
                                                 
-    $self->result->set($action->{name} => $res);
+    $self->result->set($function->{name} => $res);
 
 }
 

Modified: Prophet/branches/actions/lib/Prophet/Server/View.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/View.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/View.pm	Mon Dec 15 02:47:27 2008
@@ -36,7 +36,7 @@
     $SERVER = shift if (@_);
     return $SERVER;
 
-};
+}
 
 
 
@@ -51,12 +51,25 @@
             uuid       => $self->cgi->param('uuid'),
             app_handle => $self->app_handle
         );
+        
+        my @possible;
+        if ($obj->loaded) {
+            push @possible,$obj->prop($self->cgi->param('prop'));
+        }  else {
+            my $params = { $self->cgi->param('prop') => undef };
+            $obj->default_props($params);
+            push @possible, $params->{ $self->cgi->param('prop') };
+            # XXX fill in defaults;
+        }
+        
+        push @possible, $obj->recommended_values_for_prop($self->cgi->param('prop'));
+       
+       my %seen; 
+        for (grep {defined && !$seen{$_}++ } @possible) {
+                outs($_ ."\n");#." | ".$_."\n");
 
-        outs_raw(
-        $obj->prop($self->cgi->param('prop')). " | ".
-        $obj->prop($self->cgi->param('prop'))
-        );
-
+        }
+        
 };
 
 

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers.pm	Mon Dec 15 02:47:27 2008
@@ -7,9 +7,10 @@
 use Params::Validate qw/validate/;
 use Template::Declare::Tags;
 use Prophet::Web::Field;
-our @EXPORT = ( qw(form page content widget function));
+our @EXPORT = ( qw(form page content widget function param_from_function));
 use Prophet::Server::ViewHelpers::Widget;
 use Prophet::Server::ViewHelpers::Function;
+use Prophet::Server::ViewHelpers::ParamFromFunction;
 
 
 sub page (&;$) {
@@ -46,6 +47,14 @@
     return $f;
 }
 
+sub param_from_function {
+    my $w = Prophet::Server::ViewHelpers::ParamFromFunction->new(@_);
+    $w->render;
+    return $w;
+
+
+}
+
 sub widget {
     my $w = Prophet::Server::ViewHelpers::Widget->new(@_);
     $w->render;

Added: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/ParamFromFunction.pm
==============================================================================
--- (empty file)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/ParamFromFunction.pm	Mon Dec 15 02:47:27 2008
@@ -0,0 +1,76 @@
+package Prophet::Server::ViewHelpers::ParamFromFunction;
+
+use Template::Declare::Tags;
+
+BEGIN { delete ${__PACKAGE__."::"}{meta}; 
+ delete ${__PACKAGE__."::"}{with};
+}
+
+use Moose;
+
+use Moose::Util::TypeConstraints;
+
+
+=head1 NAME
+
+=head1 METHODS
+
+=head1 DESCRIPTION
+
+=cut
+
+
+has function => (
+    isa => 'Prophet::Server::ViewHelpers::Function',
+    is  => 'ro'
+);
+has name          => ( isa => 'Str',                 is => 'rw' );
+has prop          => ( isa => 'Str',                 is => 'ro' );
+has from_function => ( isa => 'Prophet::Server::ViewHelpers::Function',                 is => 'rw' );
+has from_result   => ( isa => 'Str',                 is => 'rw' );
+has field         => ( isa => 'Prophet::Web::Field', is => 'rw' );
+
+
+sub render {
+    my $self = shift;
+
+    my $unique_name = $self->_generate_name();
+   
+    my $record = $self->function->record;
+
+    my $value = "function-".$self->from_function->name."|result-".$self->from_result;
+
+    $self->field( Prophet::Web::Field->new(
+        name   => $unique_name,
+        type => 'hidden',
+        record => $record,
+        value  => $value
+        
+    ));
+
+    outs_raw( $self->field->render_input );
+}
+
+
+
+
+sub _generate_name {
+    my $self = shift;
+    return "prophet-fill-function-"
+        . $self->function->name
+        . "-prop-"
+        . $self->prop;
+}
+
+=head1 METHODS
+
+=cut
+
+
+
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+
+1;
+

Modified: Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Server/ViewHelpers/Widget.pm	Mon Dec 15 02:47:27 2008
@@ -10,7 +10,6 @@
 
 use Moose::Util::TypeConstraints;
 
-
 =head1 NAME
 
 =head1 METHODS
@@ -19,17 +18,17 @@
 
 =cut
 
-
 has function => (
     isa => 'Prophet::Server::ViewHelpers::Function',
     is  => 'ro'
 );
+
 has name => ( isa => 'Str', is => 'rw' );
+
 has prop => ( isa => 'Str', is => 'ro' );
 
 has field => ( isa => 'Prophet::Web::Field', is => 'rw');
 
-
 sub render {
     my $self = shift;
 
@@ -69,28 +68,16 @@
 
     outs_raw( $orig->render_input );
     outs_raw( $self->field->render );
-        outs_raw('<script>
-        $("#'.$self->field->id.'").autocomplete("/=/prophet/autocomplete",{ 
-       
-        selectFirst: false, 
-        autoFill: true,
-        minChars: 0,
-        delay: 0,
+        outs_raw('<script>$("#'.$self->field->id.'").autocomplete("/=/prophet/autocomplete",{ 
+        selectFirst: true, autoFill: false, minChars: 0, delay: 0,
         extraParams: {
                     "function": "'.$self->field->name.'",
                     "class": "'.ref($record).'",
                     "uuid": "'.($record->uuid||'').'",
                     "type": "'.$record->type.'",
-                    "prop": "'.$self->prop.'",
-                }
-                 }   
-                );
-        </script> ');
+                    "prop": "'.$self->prop.'" } }   ); </script> ');
 }
 
-
-
-
 sub _generate_name {
     my $self = shift;
     return
@@ -104,9 +91,6 @@
 
 =cut
 
-
-
-
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Modified: Prophet/branches/actions/lib/Prophet/Util.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Util.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Util.pm	Mon Dec 15 02:47:27 2008
@@ -31,7 +31,6 @@
         });
     die $args{class} ." is not a valid class " unless (UNIVERSAL::isa($args{class}, 'Prophet::Record'));
     my $object = $args{class}->new( uuid => $args{uuid}, app_handle => $args{app_handle});
-    die "Did not find the object " unless $object->uuid;
     return $object;
 }
 

Modified: Prophet/branches/actions/lib/Prophet/Web/FunctionResult.pm
==============================================================================
--- Prophet/branches/actions/lib/Prophet/Web/FunctionResult.pm	(original)
+++ Prophet/branches/actions/lib/Prophet/Web/FunctionResult.pm	Mon Dec 15 02:47:27 2008
@@ -19,6 +19,20 @@
 has success => (isa => 'Bool', is => 'rw');
 has message => (isa => 'Str', is => 'rw');
 
+has result => (
+             metaclass => 'Collection::Hash',
+             is        => 'rw',
+             isa       => 'HashRef[Str]',
+             default   => sub { {} },
+             provides  => {
+                 exists    => 'exists',
+                 keys      => 'items',
+                 get       => 'get',
+                 set       => 'set',
+             },
+
+);
+
 
 __PACKAGE__->meta->make_immutable;
 no Moose;



More information about the Bps-public-commit mailing list