--- trunk/lib/Frey/Run.pm 2008/11/18 02:16:38 400
+++ trunk/lib/Frey/Run.pm 2009/01/06 00:22:56 934
@@ -1,12 +1,15 @@
package Frey::Run;
use Moose;
-#extends 'Frey::ClassLoader';
+#extends 'Frey::Class::Loader';
extends 'Frey::Action';
-with 'Frey::Web';
-with 'Frey::Escape';
+with 'Frey::Session';
use Data::Dump qw/dump/;
-use Frey::Dumper;
+use JSON;
+use YAML;
+
+use lib 'lib';
+use Frey::View::Dumper;
=head1 NAME
@@ -24,9 +27,14 @@
=cut
use Moose::Util::TypeConstraints;
+use Frey::Class::Loader; # class_runnable_re
+
+subtype 'Runnable'
+ => as 'Str',
+ => where sub { Frey::Class::Loader::class_runnable_re };
-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',
@@ -43,66 +51,129 @@
has 'run' => (
is => 'rw',
isa => 'Runnable',
- default => 'markup',
+ default => 'as_markup',
+);
+
+has 'format' => (
+ is => 'rw',
+ isa => 'Formats',
+ default => 'html',
+);
+
+has 'request_url' => (
+ documentation => 'Take url from params if not specified',
+ is => 'rw',
+ isa => 'Uri', coerce => 1,
+ lazy => 1,
+ default => sub {
+ my $self = shift;
+ $self->params->{request_url};
+ },
);
sub html {
my ( $self ) = @_;
- my $html;
+ my ($html,$body,$data);
+
+ my $current_status;
+ $current_status->{$_}++ foreach $self->status;
+
eval {
my $class = $self->class;
$self->load_class( $class );
- if ( $html = $self->params_form ) {
- warn "got params form for $class";
+ if ( my $form = $self->params_form ) {
+ $html = $self->page( body => $form );
+ warn "got required params form for $class ", $self->run, " format: ", $self->format;
} else {
+
+ $self->usage->{ $class }++;
+
+=begin remove
+
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;
+ if ( $o->can('add_status') ) {
+ $self->TODO("missing add_status in $o");
+ Frey::Web->meta->apply( $o );
+ warn "# apply Frey::Web to $class instance $o";
+ }
+ } else {
+ $o = $self->new_frey_class( $class, $self->params );
+ }
+=cut
+
+ my $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;
+ if ( $self->run =~ m{as_markup} ) {
+ $html = $o->page( run => $self->run );
+ } elsif ( $self->run =~ m{(.*as_sponge)} ) {
+ $data = $o->$1;
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 ) . "->as_sponge
";
+ $body .= '';
+ $body .= '' . join(' | ', @{$data->{NAME}} ) . ' |
';
+ $body .= '' . join(' | ', @$_ ) . ' |
' foreach @{ $data->{rows} };
+ $body .= '
';
+
+ delete( $data->{rows} ); # too much dumplication
+ $body .= Frey::View::Dumper->new( data => $data )->as_markup if $data;
}
+ } elsif ( $self->run =~ m{(as_data|sql)} ) {
+ my $run = $self->run;
+ $data = $o->$run;
+ confess "no data for $class->$run" unless defined $data;
+ $self->add_status( { $self->run => $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};
+ }
+ 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 && ! defined $body;
+ }
+
+ $o->title( $class );
+
+ $html = $o->page( body => $body ) if $body && !$html;
+ $self->content_type( $o->content_type );
+
+ confess "no html output for $class ", $o->dump unless defined $html;
+
+ if ( $o->can('status') ) {
+ foreach ( $o->status ) {
+ next if $current_status->{$_}++;
+ $self->add_status( $_ );
+ warn "# run add_status: ", $self->dump( $_ );
+ }
}
- };
- if ( ref($html) eq 'HASH' ) {
- $html = $self->page( %$html );
- } else {
- $html = $self->page( title => $self->class, body => $html );
};
+
};
- $html = $self->error( $@ ) if $@;
+ $self->status_parts;
+
+ if ( $@ ) {
+ my $error = $@;
+ my $o = Frey->new;
+ $o->{request_url} = $self->request_url; # sigh, this is dynamic languge, right?
+ Frey::Web->meta->apply( $o );
+ $html = $o->page( body => $self->error( $error, undef ) );
+ }
+
+ warn $self->class, " produced ", length($html), " bytes of ", $self->content_type;
return $html;
}