--- trunk/lib/Frey/Run.pm 2008/11/01 00:14:05 223 +++ trunk/lib/Frey/Run.pm 2008/12/14 14:13:35 835 @@ -1,8 +1,13 @@ package Frey::Run; use Moose; -extends 'Frey'; -with 'Frey::Web'; -with 'Frey::Config'; +#extends 'Frey::Class::Loader'; +extends 'Frey::Action'; +with 'Frey::Session'; + +use Data::Dump qw/dump/; +use Frey::View::Dumper; +use JSON; +use YAML; =head1 NAME @@ -11,11 +16,22 @@ =head1 DESCRIPTION This object will try to run other Moose objects from your application. It -will try to invoke C, C or C method on the. +will try to invoke C, and C method on the. + +=head1 SEE ALSO + +L which creates form for params =cut -sub execute { qw/data markup request/ } +use Moose::Util::TypeConstraints; + +subtype 'Runnable' + => as 'Str', + => where sub { m{^as_} || m{_as_} }; + +sub formats_available { qw/html js json yaml yml/ } +enum 'Formats' => formats_available; has 'class' => ( is => 'rw', @@ -23,55 +39,124 @@ required => 1, ); -use Data::Dump qw/dump/; +has 'params' => ( + is => 'rw', + isa => 'HashRef', + default => sub { {} }, +); + +has 'run' => ( + is => 'rw', + isa => 'Runnable', + default => 'as_markup', +); -sub request { - my ( $self, $req ) = @_; +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}; + }, +); - my %params = $req->params; - my $class = $self->class; +sub html { + my ( $self ) = @_; - my @required = - grep { - defined $_ && !defined( $params{$_} ) - } - map { - my $attr = $class->meta->get_attribute($_); - $attr->is_required && $_ - } $class->meta->get_attribute_list; - - warn "## required = ",dump( @required ); - warn "## params = ",dump( %params ); - - my $html; - - if ( @required ) { - $html = qq|

Required params for $class

|; - foreach my $name ( @required ) { - my $type = $name =~ m/^pass/ ? 'password' : 'text'; - my $value = $self->config($class)->{$name}; - $html .= qq||; - } - $html .= qq|
|; - } else { - my $o = $class->new( %params ); - $o->depends if $o->can('depends'); - if ( $o->can('request') ) { - warn "## turning over to $o->request"; - $o->request( $req ); - } elsif ( $o->can('markup') ) { - warn "## using $o->markup"; - $html = $o->markup; - warn ">>> markup $class ",length( $html ), " bytes\n"; - } elsif ( $o->can('data') ) { - $html = '' . dump( $o->data ) . ''; + my ($html,$body,$data); + eval { + my $class = $self->class; + $self->load_class( $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 { - $html = "IGNORE: $class ", $o->dump; - warn $html; - } + + $self->usage->{ $class }++; + +=begin remove + + my $o; + 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 =~ m{as_markup} ) { + $html = $o->page( run => $self->run ); + } elsif ( $self->run =~ m{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 .= ''; + $body .= '' foreach @{ $data->{rows} }; + $body .= '
' . join('', @{$data->{NAME}} ) . '
' . join('', @$_ ) . '
'; + } + } elsif ( $self->run =~ m{as_data} ) { + my $run = $self->run; + $data = $o->$run; + confess "no data for $class->$run" unless defined $data; + $self->add_status( { $self->run => $data } ); + } else { + $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; + } + + $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; + }; + + }; + + $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 ) ); } - $req->print( $self->page( title => $class, body => $html ) ); + warn $self->class, " produced ", length($html), " bytes of ", $self->content_type; + + return $html; } 1;