76bf55e05941031f01c3cdacea2d6536e9b43207
[catagits/Reaction.git] / lib / Reaction / UI / ViewPort / Action / Link.pm
1 package Reaction::UI::ViewPort::Action::Link;
2
3 use Reaction::Class;
4 extends 'Reaction::UI::ViewPort';
5
6 use namespace::clean -except => [ qw(meta) ];
7 use MooseX::Types::URI qw/Uri/;
8 use MooseX::Types::Moose qw/Object CodeRef/;
9
10 has uri => ( is => 'rw', isa => Uri, lazy_build => 1);
11 has label => (is => 'rw', required => 1);
12
13 has target => (isa => Object, is => 'rw', required => 1);
14 has action => (isa => CodeRef, is => 'rw', required => 1);
15
16 sub BUILD {
17   my $self = shift;
18   $self->label( $self->label->($self->target) ) if ref $self->label eq 'CODE';
19 }
20
21 sub _build_uri {
22   my $self = shift;
23   my $c = $self->ctx;
24   my ($c_name, $a_name, @rest) = @{ $self->action->($self->target, $c) };
25   $c->uri_for($c->controller($c_name)->action_for($a_name),@rest);
26 }
27
28 __PACKAGE__->meta->make_immutable;
29
30 1;