File Coverage

blib/lib/Catalyst/View/Template/Lace/Role/URI.pm
Criterion Covered Total %
statement 6 22 27.2
branch 0 16 0.0
condition n/a
subroutine 2 5 40.0
pod n/a
total 8 43 18.6


line stmt bran cond sub pod time code
1             package Catalyst::View::Template::Lace::Role::URI;
2              
3 1     1   11657 use Moo::Role;
  1         2  
  1         6  
4 1     1   2206 use Scalar::Util;
  1         2  
  1         261  
5              
6 0     0     sub uri_for { shift->ctx->uri_for(@_) }
7              
8 0     0     sub action_for { shift->ctx->controller->action_for(@_) }
9              
10             sub uri {
11 0     0     my ($self, $action_proto, @args) = @_;
12              
13 0 0         $action_proto = $self->ctx->action unless $action_proto;
14              
15             # If its already an action object just use it.
16 0 0         return $self->ctx->uri_for($action_proto, @args)
17             if Scalar::Util::blessed($action_proto);
18              
19 0           my $controller = $self->ctx->controller;
20 0           my $action;
21            
22             # Otherwise its a string which is a full or relative path
23             # to the private name of an action. Resolve it.
24             # Starts with '/' means its a full absolute private name
25             # Otherwise its a realtive name to the current controller
26             # namespace.
27              
28 0 0         if($action_proto =~/\//) {
29 0 0         my $path = $action_proto=~m/^\// ? $action_proto : $controller->action_for($action_proto)->private_path;
30 0 0         die "$action_proto is not an action for controller ${\$controller->catalyst_component_name}" unless $path;
  0            
31 0 0         die "$path is not a private path" unless $action = $self->ctx->dispatcher->get_action_by_path($path);
32             } else {
33 0 0         die "$action_proto is not an action for controller ${\$controller->catalyst_component_name}"
  0            
34             unless $action = $controller->action_for($action_proto);
35             }
36 0 0         die "Could not create a URI from '$action_proto' with the given arguments" unless $action;
37 0           return $self->ctx->uri_for($action, @args);
38             }
39              
40             1;
41              
42             =head1 NAME
43              
44             Catalyst::View::Template::Lace::Role::URI - Shortcut to create a URI on the current controller
45              
46             =head1 SYNOPSIS
47              
48             package MyApp::View::User;
49              
50             use Moo;
51             extends 'Catalyst::View::Template::Lace';
52             with 'Catalyst::View::Template::Lace::Role::URI';
53              
54             sub template {q[
55             <html>
56             <head>
57             <title>Link Example</title>
58             </head>
59             <body>
60             <a>Link</a>
61             </body>
62             </html>
63             ]}
64              
65             sub process_dom {
66             my ($self, $dom) = @_;
67             $dom->at('a')
68             ->href($self->uri('../display'));
69             }
70              
71             =head1 DESCRIPTION
72              
73             A role that gives your model object a C<uri> method. This method works
74             similarly to "$c->uri_for" except that it only takes an action object or
75             a string that is an absolute or relative (to the current controller) private
76             name.
77              
78             B<NOTE> Since this role uses "$c->controller" to determine the 'last controller
79             executed' you must take care to use "$c->go" rather than "$c->detach", since the
80             latter will leave "$c->controller" to the value of the controller detached from
81             rather then the actual last controller executed.
82              
83             =head1 METHOD
84              
85             This role defines the following methods
86              
87             =head2 uri
88              
89             $self->uri($action);
90             $self->uri('/user/display');
91             $self->uri('display');
92             $self->uri('../list');
93              
94             First argument is an action object or a string. If a string it must be either
95             an absolute private name to an action or a relative one
96              
97             If you want to build a L<URI> from the current action you can just pass C<undef>.
98              
99             =head1 SEE ALSO
100            
101             L<Catalyst::View::Template::Lace>.
102              
103             =head1 AUTHOR
104              
105             Please See L<Catalyst::View::Template::Lace> for authorship and contributor information.
106            
107             =head1 COPYRIGHT & LICENSE
108            
109             Please see L<Catalyst::View::Template::Lace> for copyright and license information.
110              
111             =cut