--- trunk/lib/Frey/Run.pm 2008/11/05 08:20:54 281 +++ trunk/lib/Frey/Run.pm 2008/11/18 02:16:38 400 @@ -1,10 +1,13 @@ package Frey::Run; use Moose; -extends 'Frey'; +#extends 'Frey::ClassLoader'; +extends 'Frey::Action'; with 'Frey::Web'; -with 'Frey::Config'; with 'Frey::Escape'; +use Data::Dump qw/dump/; +use Frey::Dumper; + =head1 NAME Frey::Run - display required form field for Class and run it @@ -12,11 +15,18 @@ =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/ } +use Moose::Util::TypeConstraints; + +sub runnable { qw/data data.js markup sponge/ } +enum 'Runnable' => runnable; has 'class' => ( is => 'rw', @@ -30,56 +40,71 @@ default => sub { {} }, ); -use Data::Dump qw/dump/; +has 'run' => ( + is => 'rw', + isa => 'Runnable', + default => 'markup', +); sub html { my ( $self ) = @_; - my $class = $self->class; - - my @required = - grep { - defined $_ && !defined( $self->params->{$_} ) - } - map { - my $attr = $class->meta->get_attribute($_); - $attr->is_required && $_ - } $class->meta->get_attribute_list; - - warn "## required = ",dump( @required ), " for $class"; - my $html; - my $values = {}; - my $values = $self->config($class) if $self->can('config'); + eval { + my $class = $self->class; + $self->load_class( $class ); - if ( @required ) { - $html = qq|

Required params for $class

|; - foreach my $name ( @required ) { - my $type = $name =~ m/^pass/ ? 'password' : 'text'; - my $value = $values ? $values->{$name} : ''; - $html .= qq||; - } - $html .= qq|
|; - } else { - my $o = $class->new( %{ $self->params } ); - $o->depends if $o->can('depends'); - if ( $o->can('markup') ) { - warn "## using ",ref($o), "->markup"; - $html = eval { $o->markup }; - if ( $@ ) { - warn $@; - $html .= qq{$@}; + if ( $html = $self->params_form ) { + warn "got params form for $class"; + } else { + my $o; + # we don't want default status elements + $self->params->{status} = []; + $o = $class->new( %{ $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 .= ''; + $html .= '' foreach @{ $data->{rows} }; + $html .= '
' . join('', @{$data->{NAME}} ) . '
' . join('', @$_ ) . '
'; + } 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 }; + } + } else { + $html = $self->error( "IGNORE: $class ", $o->dump ); } - warn ">>> markup $class ",length( $html ), " bytes\n"; - } elsif ( $o->can('data') ) { - $html = '' . $self->html_escape( dump( $o->data ) ) . ''; + }; + + if ( ref($html) eq 'HASH' ) { + $html = $self->page( %$html ); } else { - $html = "IGNORE: $class ", $o->dump; - warn $html; - } - } + $html = $self->page( title => $self->class, body => $html ); + }; + }; + + $html = $self->error( $@ ) if $@; - return $self->page( title => $class, body => $html ); + return $html; } 1;