--- trunk/lib/Frey/Run.pm 2008/11/18 02:16:38 400 +++ trunk/lib/Frey/Run.pm 2008/11/27 22:11:13 570 @@ -3,10 +3,12 @@ #extends 'Frey::ClassLoader'; extends 'Frey::Action'; with 'Frey::Web'; -with 'Frey::Escape'; +with 'Frey::Session'; use Data::Dump qw/dump/; -use Frey::Dumper; +use Frey::View::Dumper; +use JSON; +use YAML; =head1 NAME @@ -25,8 +27,12 @@ use Moose::Util::TypeConstraints; -sub runnable { qw/data data.js markup sponge/ } -enum 'Runnable' => runnable; +subtype 'Runnable' + => as 'Str', + => where sub { m{^as_} }; + +sub formats_available { qw/html js json yaml yml/ } +enum 'Formats' => formats_available; has 'class' => ( is => 'rw', @@ -43,66 +49,104 @@ has 'run' => ( is => 'rw', isa => 'Runnable', - default => 'markup', + default => 'as_markup', +); + +has 'format' => ( + is => 'rw', + isa => 'Formats', + default => 'html', ); sub html { my ( $self ) = @_; - my $html; + my ($html,$body,$data); eval { my $class = $self->class; $self->load_class( $class ); - if ( $html = $self->params_form ) { - warn "got params form for $class"; + if ( $body = $self->params_form ) { + warn "got required params form for $class ", $self->run, " format: ", $self->format; } else { + + $self->usage->{ $class }++; + my $o; - # we don't want default status elements - $self->params->{status} = []; - $o = $class->new( %{ $self->params } ); + my ( $meta, $is_role, $instance ) = $self->class_meta( $class ); + if ( $is_role ) { + $o = $instance; + } else { + $o = $self->new_frey_class( $class, $self->params ); + } + $o->depends if $o->can('depends'); - if ( $self->run eq 'markup' ) { - warn "## using ",ref($o), "->markup"; - $html = $o->markup; - # preserve status - push @{ $self->status }, { $class => $o->status } if $o->can('status'); - - warn ">>> markup $class ",length( $html ), " bytes\n"; - } elsif ( $self->run eq 'sponge' ) { - my $data = $o->sponge; + my @status; + + push @status, { $self->editor( $class ) => $self->params }; + + if ( $self->run eq 'as_markup' && ! $o->can('page') ) { + warn "## using ",ref($o), "->as_markup"; + $body = $o->as_markup unless $html; + warn ">>> markup $class ",length( $body ), " ", $html ? 'html' : 'body', " bytes"; + } elsif ( $self->run eq 'as_sponge' ) { + $data = $o->as_sponge; confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH'; - my $rows = $#{ $data->{rows} } + 1; - $rows ||= 'no'; - $html .= "$rows rows from $class->new" . dump( $self->params ) . "->sponge"; - $html .= ''; - $html .= ''; - $html .= '' foreach @{ $data->{rows} }; - $html .= '
' . join('', @{$data->{NAME}} ) . '
' . join('', @$_ ) . '
'; - } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) { - my $data = $o->data; - if ( my $format = $1 ) { - $html .= to_json( $data ) if $format =~ m{js(on)?}; - $html .= Dump( $data ) if $format =~ m{yaml?}; - } - if ( ! $html ) { - $html .= Frey::Dumper->new( data => $data )->markup; - push @{ $self->status }, { 'Dump' => $data }; + if ( $self->format eq 'html' ) { + my $rows = $#{ $data->{rows} } + 1; + $rows ||= 'no'; + $body .= "$rows rows from $class->new" . dump( $self->params ) . "->as_sponge"; + $body .= ''; + $body .= ''; + $body .= '' foreach @{ $data->{rows} }; + $body .= '
' . join('', @{$data->{NAME}} ) . '
' . join('', @$_ ) . '
'; } + } elsif ( $self->run eq 'as_data' ) { + $data = $o->as_data; } else { - $html = $self->error( "IGNORE: $class ", $o->dump ); + $body = $self->error( "IGNORE: $class ", $o->dump ); } + + if ( defined $data ) { + $html .= to_json( $data ) if $self->format =~ m{js(on)?}; + $html .= Dump( $data ) if $self->format =~ m{ya?ml}; + push @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; + } + + warn "## status from $self ",dump(@status); + + if ( $o->can('add_status') ) { + $o->add_status($_) foreach @status; + } + + if ( $self->run eq 'as_markup' && $o->can('page') ) { + $html = $o->page; + warn "got ", length($html), " for page from $o", + $self->debug ? " status " . dump( $o->status ) : ''; + } else { + $self->add_status($_) foreach @status; + } + + $self->title( $class ); }; - if ( ref($html) eq 'HASH' ) { - $html = $self->page( %$html ); - } else { - $html = $self->page( title => $self->class, body => $html ); + if ( ref($body) eq 'HASH' ) { + $html = $self->page( %$body ); + warn "WARNING: old calling convention with HASH which is depriciated but produced ", length($html), " bytes"; + } elsif ( $body && ! $html ) { + $html = $self->page( title => $self->class . ' run', body => $body ); + warn "wrap body of ",length($body), " in page with ", length($html), " bytes"; }; }; - $html = $self->error( $@ ) if $@; + $self->status_parts; + + $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@; return $html; }