--- trunk/lib/Frey/Run.pm 2008/11/18 10:34:42 403
+++ 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
@@ -28,6 +29,9 @@
sub runnable { qw/data data.js markup sponge/ }
enum 'Runnable' => runnable;
+sub formats_available { qw/html js json yaml yml/ }
+enum 'Formats' => formats_available;
+
has 'class' => (
is => 'rw',
isa => 'Str',
@@ -46,63 +50,79 @@
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;
- # we don't want default status elements
- $self->params->{status} = [];
- $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;
- # preserve status
- push @{ $self->status }, { $class => $o->status } if $o->can('status');
+ if ( $o->can('page') ) {
+ $html = $o->page;
+ $body = $o->markup unless $html;
+ } else {
+ $body = $o->markup;
+ }
- warn ">>> markup $class ",length( $html ), " bytes\n";
+ 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 .= '' . join(' | ', @{$data->{NAME}} ) . ' |
';
- $html .= '' . join(' | ', @$_ ) . ' |
' foreach @{ $data->{rows} };
- $html .= '
';
- } 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 ) . "->sponge
";
+ $body .= '';
+ $body .= '' . join(' | ', @{$data->{NAME}} ) . ' |
';
+ $body .= '' . join(' | ', @$_ ) . ' |
' foreach @{ $data->{rows} };
+ $body .= '
';
}
+ } elsif ( $self->run eq '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($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 );
+ } elsif ( $body && ! $html ) {
+ $html = $self->page( title => $self->class . ' run', body => $body );
};
};
- $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@;
+ $html = $self->page( title => $self->class, body => dump($html) . $self->error( $@ ) ) if $@;
return $html;
}