/[Frey]/trunk/lib/Frey/Run.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/Frey/Run.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 403 - (show annotations)
Tue Nov 18 10:34:42 2008 UTC (15 years, 5 months ago) by dpavlin
File size: 2671 byte(s)
wrap error in page to preserve internal navigation
1 package Frey::Run;
2 use Moose;
3 #extends 'Frey::ClassLoader';
4 extends 'Frey::Action';
5 with 'Frey::Web';
6 with 'Frey::Escape';
7
8 use Data::Dump qw/dump/;
9 use Frey::Dumper;
10
11 =head1 NAME
12
13 Frey::Run - display required form field for Class and run it
14
15 =head1 DESCRIPTION
16
17 This object will try to run other Moose objects from your application. It
18 will try to invoke C<data>, and C<markup> method on the.
19
20 =head1 SEE ALSO
21
22 L<Frey::Action> which creates form for params
23
24 =cut
25
26 use Moose::Util::TypeConstraints;
27
28 sub runnable { qw/data data.js markup sponge/ }
29 enum 'Runnable' => runnable;
30
31 has 'class' => (
32 is => 'rw',
33 isa => 'Str',
34 required => 1,
35 );
36
37 has 'params' => (
38 is => 'rw',
39 isa => 'HashRef',
40 default => sub { {} },
41 );
42
43 has 'run' => (
44 is => 'rw',
45 isa => 'Runnable',
46 default => 'markup',
47 );
48
49 sub html {
50 my ( $self ) = @_;
51
52 my $html;
53 eval {
54 my $class = $self->class;
55 $self->load_class( $class );
56
57 if ( $html = $self->params_form ) {
58 warn "got params form for $class";
59 } else {
60 my $o;
61 # we don't want default status elements
62 $self->params->{status} = [];
63 $o = $class->new( %{ $self->params } );
64 $o->depends if $o->can('depends');
65
66 if ( $self->run eq 'markup' ) {
67 warn "## using ",ref($o), "->markup";
68 $html = $o->markup;
69 # preserve status
70 push @{ $self->status }, { $class => $o->status } if $o->can('status');
71
72 warn ">>> markup $class ",length( $html ), " bytes\n";
73 } elsif ( $self->run eq 'sponge' ) {
74 my $data = $o->sponge;
75 confess "invalid data from sponge = ", dump( $data ) unless ref($data) eq 'HASH';
76 my $rows = $#{ $data->{rows} } + 1;
77 $rows ||= 'no';
78 $html .= "<strong>$rows</strong> rows from <code>$class->new" . dump( $self->params ) . "->sponge</code>";
79 $html .= '<table>';
80 $html .= '<tr><th>' . join('</th><th>', @{$data->{NAME}} ) . '</th></tr>';
81 $html .= '<tr><td>' . join('</td><td>', @$_ ) . '</td></tr>' foreach @{ $data->{rows} };
82 $html .= '</table>';
83 } elsif ( $self->run =~ m{^data(\.(js|json|yaml|yml))?$} ) {
84 my $data = $o->data;
85 if ( my $format = $1 ) {
86 $html .= to_json( $data ) if $format =~ m{js(on)?};
87 $html .= Dump( $data ) if $format =~ m{yaml?};
88 }
89 if ( ! $html ) {
90 $html .= Frey::Dumper->new( data => $data )->markup;
91 push @{ $self->status }, { 'Dump' => $data };
92 }
93 } else {
94 $html = $self->error( "IGNORE: $class ", $o->dump );
95 }
96 };
97
98 if ( ref($html) eq 'HASH' ) {
99 $html = $self->page( %$html );
100 } else {
101 $html = $self->page( title => $self->class, body => $html );
102 };
103 };
104
105 $html = $self->page( title => $self->class, body => $self->error( $@ ) ) if $@;
106
107 return $html;
108 }
109
110 1;

  ViewVC Help
Powered by ViewVC 1.1.26