--- trunk/lib/Frey/Run.pm 2008/11/18 10:34:42 403
+++ trunk/lib/Frey/Run.pm 2008/11/20 14:39:43 480
@@ -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 Frey::View::Dumper;
+use JSON;
+use YAML;
=head1 NAME
@@ -25,8 +28,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,62 +50,88 @@
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;
- 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?};
+ 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;
}
- if ( ! $html ) {
- $html .= Frey::Dumper->new( data => $data )->markup;
- push @{ $self->status }, { 'Dump' => $data };
+ $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;
+ confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
+ 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 '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 @{ $self->status }, { 'data' => $data };
}
+ if ( ! $html ) {
+ $body .= Frey::View::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($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 );
};
};