--- trunk/lib/Frey/Run.pm 2008/11/17 14:37:48 369 +++ trunk/lib/Frey/Run.pm 2008/11/18 17:12:09 421 @@ -7,6 +7,7 @@ use Data::Dump qw/dump/; use Frey::Dumper; +use JSON; =head1 NAME @@ -25,9 +26,11 @@ use Moose::Util::TypeConstraints; -enum 'Runnable' => qw/data markup sponge/; +sub runnable { qw/data data.js markup sponge/ } +enum 'Runnable' => runnable; -sub runnable { qw/data markup sponge/ } +sub formats_available { qw/html js json yaml yml/ } +enum 'Formats' => formats_available; has 'class' => ( is => 'rw', @@ -47,49 +50,81 @@ default => '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 { - my $o; - $o = $class->new( %{ $self->params } ); + + my $o = $class->new( %{ $self->params } ); $o->depends if $o->can('depends'); + push @{ $self->status }, { qq|$class| => $self->params }; + if ( $self->run eq 'markup' ) { warn "## using ",ref($o), "->markup"; - $html = $o->markup; - warn ">>> markup $class ",length( $html ), " bytes\n"; + if ( $o->can('page') ) { + $html = $o->page; + $body = $o->markup unless $html; + } else { + $body = $o->markup; + } + + warn ">>> markup $class ",length( $html || $body ), " ", $html ? 'html' : 'body', " bytes"; } elsif ( $self->run eq 'sponge' ) { - my $data = $o->sponge; + $data = $o->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('', @$_ ) . '
'; + if ( $self->format eq 'html' ) { + my $rows = $#{ $data->{rows} } + 1; + $rows ||= 'no'; + $body .= "$rows rows from $class->new" . dump( $self->params ) . "->sponge"; + $body .= ''; + $body .= ''; + $body .= '' foreach @{ $data->{rows} }; + $body .= '
' . join('', @{$data->{NAME}} ) . '
' . join('', @$_ ) . '
'; + } } elsif ( $self->run eq 'data' ) { - my $data = $o->data; - $html .= Frey::Dumper->new( data => $data )->markup; - $html .= '
dump' . $self->html_dump( $data ) . ''; + $data = $o->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 @{ $self->status }, { 'data' => $data }; + } + if ( ! $html ) { + $body .= Frey::Dumper->new( data => $data )->markup; + } + + # override our status with one from object + $self->status( $o->status ) if $o->can('status') && $o->status; }; + + if ( ref($body) eq 'HASH' ) { + $html = $self->page( %$body ); + } elsif ( $body && ! $html ) { + $html = $self->page( title => $self->class . ' run', body => $body ); + }; }; - $html .= $self->error( $@ ) if $@; - return $self->page( %$html ) if ref($html) eq 'HASH'; - return $self->page( title => $self->class, body => $html ); + $html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@; + + return $html; } 1;