1 |
package Frey::Pipe; |
package Frey::Pipe; |
2 |
use Moose; |
use Moose; |
3 |
extends 'Frey'; |
extends 'Frey::Class::Loader'; |
4 |
with 'Frey::Web'; |
with 'Frey::Web'; |
5 |
|
|
6 |
|
use lib 'lib'; |
7 |
use Frey::Action; |
use Frey::Action; |
8 |
|
|
9 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
12 |
|
|
13 |
=head1 EXAMPLE |
=head1 EXAMPLE |
14 |
|
|
15 |
Frey::Feed/data + uri=https://blog.rot13.org/index.xml | Frey::Dumper/markup |
Frey::Feed/as_data + uri=https://blog.rot13.org/index.xml | Frey::View::Dumper/as_markup |
16 |
|
|
17 |
this will produce following code: |
this will produce following code: |
18 |
|
|
19 |
Frey::Dumper->new( data => |
Frey::View::Dumper->new( data => |
20 |
Frey::Feed->new( uri => 'https://blog.rot13.org/index.xml' )->data |
Frey::Feed->new( uri => 'https://blog.rot13.org/index.xml' )->as_data |
21 |
)->markup |
)->as_markup |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
has pipe => ( |
has pipe => ( |
26 |
is => 'rw', |
is => 'rw', |
27 |
required => 1, |
required => 1, |
28 |
default => 'Frey::Feed/data+uri=https://blog.rot13.org/index.xml|Frey::Dumper/markup', |
default => 'Frey::Feed/as_data+uri=https://blog.rot13.org/index.xml|Frey::View::Dumper/as_markup', |
29 |
); |
); |
30 |
|
|
31 |
|
sub render_pipe { 'radio' }; |
32 |
|
|
33 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
34 |
|
|
35 |
sub markup { |
sub as_markup { |
36 |
my ($self) = @_; |
my ($self) = @_; |
37 |
|
|
38 |
my $out; |
my $out; |
39 |
my $pipe; |
my $pipe; |
40 |
|
|
41 |
$self->title( ref($self) . ' | ' . $self->pipe ); |
$self->title( $self->pipe ); |
42 |
|
|
43 |
|
my @parts; |
44 |
|
|
45 |
foreach my $part ( split(/\|/, $self->pipe ) ) { |
foreach my $part ( split(/\|/, $self->pipe ) ) { |
46 |
warn "# part: '$part'"; |
warn "# part: '$part'"; |
47 |
if ( $part =~ m{^([^/]+)/([^\+\?]+)(.*)?$} ) { |
if ( $part =~ m{^([^/]+)/([^\+\?]+)(.*)?$} ) { |
48 |
my ( $class, $method, $args ) = ( $1, $2, $3 ); |
my ( $class, $method, $args ) = ( $1, $2, $3 ); |
|
push @{ $self->status }, { $class => { method => $method, args => $args } }; |
|
49 |
my $params = $pipe; |
my $params = $pipe; |
50 |
$params = {} unless defined $params; |
$params = {} unless defined $params; |
51 |
if ( defined $args ) { |
if ( defined $args ) { |
52 |
$args =~ s{^[\?\+\s]}{}; |
$args =~ s{^[\?\+\s]}{}; |
53 |
warn "# class $class method $method args '$args'", $pipe ? " pipe args " . join(',',keys %$pipe) : '' if $self->debug; |
warn "# class $class method $method args '$args'", $pipe ? " pipe args " . join(',',keys %$pipe) : '' if $self->debug; |
|
push @{ $self->status }, { $class =>$args }; |
|
54 |
map { |
map { |
55 |
my ( $name, $value ) = ( $1, $2 ) if m{^([^=]+)=(.+)$} || confess "can't parse '$_'"; |
my ( $name, $value ) = ( $1, $2 ) if m{^([^=]+)=(.+)$} || confess "can't parse '$_'"; |
56 |
$params->{$name} = $value; |
$params->{$name} = $value; |
57 |
} split(/\s*\+\s/, $args) |
} split(/&/, $args) |
58 |
} |
} |
59 |
|
|
60 |
my ( $html, $default ) = Frey::Action->new( class => $class, params => $params )->params_form; |
my ( $html, $default ) = Frey::Action->new( class => $class, params => $params )->params_form; |
61 |
warn "$class need more params than ",dump( $default ) if $html && $self->debug; # FIXME replace with query |
warn "$class need more params than ",dump( $default ) if $html && $self->debug; # FIXME replace with query |
62 |
|
|
63 |
my $code = '$result = ' . $class . '->new' . dump( %$default ) . '->' . $method . '();'; |
warn "# pipe $part" if $self->debug; |
64 |
warn "# pipe $part -> $code" if $self->debug; |
my $o = $self->new_frey_class( $class, $default ); |
65 |
my $result; |
|
66 |
eval $code; |
# XXX copy depends from parts of pipe |
67 |
die $@ if $@; |
if ( $o->can('depends') ) { |
68 |
warn "# result ",ref( $result ); |
$o->depends; |
69 |
# $out .= qq{<span class="frey-popdown">$part<code>} . dump( $result ) . '</code><span><hr>'; |
my $current_head; |
70 |
|
$current_head->{$_}++ foreach $self->head; |
71 |
|
foreach ( $o->head ) { |
72 |
|
next if $current_head->{$_}++; |
73 |
|
$self->add_head( $_ ); |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
|
my $result = $o->$method; |
78 |
|
warn "# result ",length( $result ), " bytes ", ref($result); |
79 |
|
|
80 |
|
my $current_status; |
81 |
|
$current_status->{$_}++ foreach $self->status; |
82 |
|
foreach ( $o->status ) { |
83 |
|
next if $current_status->{$_}++; |
84 |
|
$self->add_status( $_ ); |
85 |
|
warn "# pipe add_status: $_"; |
86 |
|
} |
87 |
|
|
88 |
|
$self->content_type( $o->content_type ) if $o->can('content_type'); |
89 |
|
|
90 |
$out = $result; |
$out = $result; |
91 |
|
$method =~ s{^as_}{}; |
92 |
$pipe = { $method => $result }; |
$pipe = { $method => $result }; |
93 |
|
push @parts, { $class . '->' . $method => $result }; |
94 |
} else { |
} else { |
95 |
die "don't know what to do with '$part' from ",$self->pipe; |
die "don't know what to do with '$part' from ",$self->pipe; |
96 |
} |
} |
97 |
} |
} |
98 |
|
|
99 |
warn "# pipe ", $self->title, dump( $self->status ); |
$pipe = $self->pipe; |
100 |
|
|
101 |
|
$out = $self->error( |
102 |
|
qq|<a href="$pipe" title="copy pipe"><tt>$pipe</tt></a> = $out not scalar\n| |
103 |
|
. $self->html_dump($out) |
104 |
|
) if ref $out; |
105 |
|
|
106 |
return $out; |
return $out; |
107 |
} |
} |
108 |
|
|
109 |
|
__PACKAGE__->meta->make_immutable; |
110 |
|
no Moose; |
111 |
|
|
112 |
1; |
1; |