--- trunk/lib/Frey/Run.pm 2008/11/17 14:37:48 369
+++ trunk/lib/Frey/Run.pm 2008/11/19 15:28:23 455
@@ -4,9 +4,12 @@
extends 'Frey::Action';
with 'Frey::Web';
with 'Frey::Escape';
+with 'Frey::Session';
use Data::Dump qw/dump/;
use Frey::Dumper;
+use JSON;
+use YAML;
=head1 NAME
@@ -25,9 +28,12 @@
use Moose::Util::TypeConstraints;
-enum 'Runnable' => qw/data markup sponge/;
+subtype 'Runnable'
+ => as 'Str',
+ => where sub { m{^as_} };
-sub runnable { qw/data markup sponge/ }
+sub formats_available { qw/html js json yaml yml/ }
+enum 'Formats' => formats_available;
has 'class' => (
is => 'rw',
@@ -44,52 +50,94 @@
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;
- $o = $class->new( %{ $self->params } );
+ my ( $meta, $is_role, $instance ) = $self->class_meta( $class );
+ if ( $is_role ) {
+ $o = $instance;
+ } else {
+ $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";
+ 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 'sponge' ) {
- my $data = $o->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 .= '' . join(' | ', @{$data->{NAME}} ) . ' |
';
- $html .= '' . join(' | ', @$_ ) . ' |
' foreach @{ $data->{rows} };
- $html .= '
';
+ 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 .= '' . join(' | ', @{$data->{NAME}} ) . ' |
';
+ $body .= '' . join(' | ', @$_ ) . ' |
' foreach @{ $data->{rows} };
+ $body .= '
';
+ }
} elsif ( $self->run eq 'data' ) {
- my $data = $o->data;
- $html .= Frey::Dumper->new( data => $data )->markup;
- $html .= '
dump' . $self->html_dump( $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 @{ $self->status }, { 'data' => $data };
+ }
+ if ( ! $html ) {
+ $body .= Frey::Dumper->new( data => $data )->as_markup;
}
+
+ # override our status with one from object
+ eval {
+ $self->status( $o->status );
+ };
+ warn "can't override status: $@" if $@;
};
+
+ 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 => $self->error( $@ ) ) if $@;
+
+ return $html;
}
1;