--- trunk/lib/Frey/Run.pm 2008/11/24 21:32:32 499 +++ trunk/lib/Frey/Run.pm 2009/01/10 23:28:43 989 @@ -1,16 +1,16 @@ package Frey::Run; use Moose; -#extends 'Frey::ClassLoader'; +#extends 'Frey::Class::Loader'; extends 'Frey::Action'; -with 'Frey::Web'; -with 'Frey::Escape'; with 'Frey::Session'; use Data::Dump qw/dump/; -use Frey::View::Dumper; use JSON; use YAML; +use lib 'lib'; +use Frey::View::Dumper; + =head1 NAME Frey::Run - display required form field for Class and run it @@ -27,10 +27,11 @@ =cut use Moose::Util::TypeConstraints; +use Frey::Class::Loader; # class_runnable_re subtype 'Runnable' => as 'Str', - => where sub { m{^as_} }; + => where sub { Frey::Class::Loader::class_runnable_re }; sub formats_available { qw/html js json yaml yml/ } enum 'Formats' => formats_available; @@ -59,41 +60,59 @@ default => 'html', ); +has 'request_url' => ( + documentation => 'Take url from params if not specified', + is => 'rw', + isa => 'Uri', coerce => 1, + lazy => 1, + default => sub { + my $self = shift; + $self->params->{request_url}; + }, +); + sub html { my ( $self ) = @_; my ($html,$body,$data); + + my $current_status; + $current_status->{$_}++ foreach $self->status; + eval { my $class = $self->class; $self->load_class( $class ); - if ( $body = $self->params_form ) { + if ( my $form = $self->params_form ) { + $html = $self->html_page( body => $form ); warn "got required params form for $class ", $self->run, " format: ", $self->format; } else { $self->usage->{ $class }++; +=begin remove + my $o; my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); if ( $is_role ) { $o = $instance; + if ( $o->can('add_status') ) { + $self->TODO("missing add_status in $o"); + Frey::Web->meta->apply( $o ); + warn "# apply Frey::Web to $class instance $o"; + } } else { $o = $self->new_frey_class( $class, $self->params ); } +=cut + my $o = $self->new_frey_class( $class, $self->params ); $o->depends if $o->can('depends'); - push @{ $self->status }, { $self->editor( $class ) => $self->params }; - - if ( $self->run eq 'as_markup' ) { - warn "## using ",ref($o), "->as_markup"; - if ( $o->can('page') ) { - $html = $o->page; - } - $body = $o->as_markup unless $html; - warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes"; - } elsif ( $self->run eq 'as_sponge' ) { - $data = $o->as_sponge; + if ( $self->run =~ m{as_markup} ) { + $html = $o->html_page( run => $self->run ); + } elsif ( $self->run =~ m{(.*as_sponge)} ) { + $data = $o->$1; confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; if ( $self->format eq 'html' ) { my $rows = $#{ $data->{rows} } + 1; @@ -103,9 +122,21 @@ $body .= '' . join('', @{$data->{NAME}} ) . ''; $body .= '' . join('', @$_ ) . '' foreach @{ $data->{rows} }; $body .= ''; + + $self->add_css(qq| + tr:nth-child(even) { + background-color: #eee; + } + |); + + delete( $data->{rows} ); # too much dumplication + $body .= Frey::View::Dumper->new( data => $data )->as_markup if $data; } - } elsif ( $self->run eq 'as_data' ) { - $data = $o->as_data; + } elsif ( $self->run =~ m{(as_data|sql)} ) { + my $run = $self->run; + $data = $o->$run; + confess "no data for $class->$run" unless defined $data; + $self->add_status( { $self->run => $data } ); } else { $body = $self->error( "IGNORE: $class ", $o->dump ); } @@ -113,29 +144,42 @@ if ( defined $data ) { $html .= to_json( $data ) if $self->format =~ m{js(on)?}; $html .= Dump( $data ) if $self->format =~ m{ya?ml}; - push @{ $self->status }, { 'data' => $data }; } if ( ! $html ) { $body = Frey::View::Dumper->new( data => $body )->as_markup if ref $body; - $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data; + $body .= Frey::View::Dumper->new( data => $data )->as_markup if defined $data && ! defined $body; } - # override our status with one from object - eval { - $self->status( $o->status ); - }; - warn "can't override status: $@" if $@; - }; + $o->title( $class ); + + $html = $o->html_page( body => $body ) if $body && !$html; + $self->content_type( $o->content_type ); + confess "no html output for $class ", $o->dump unless defined $html; + + if ( $o->can('status') ) { + foreach ( $o->status ) { + next if $current_status->{$_}++; + $self->add_status( $_ ); + warn "# run add_status: ", $self->dump( $_ ); + } + } - if ( ref($body) eq 'HASH' ) { - $html = $self->page( %$body ); - } elsif ( $body && ! $html ) { - $html = $self->page( title => $self->class . ' run', body => $body ); }; + }; - $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@; + $self->status_parts; + + if ( $@ ) { + my $error = $@; + my $o = Frey->new; + $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right? + Frey::Web->meta->apply( $o ); + $html = $o->html_page( body => $self->error( $error, undef ) ); + } + + warn $self->class, " produced ", length($html), " bytes of ", $self->content_type; return $html; }