File Coverage

blib/lib/Pinwheel/Helpers/Tag.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 18 100.0
condition 6 6 100.0
subroutine 10 10 100.0
pod 0 5 0.0
total 89 94 94.6


line stmt bran cond sub pod time code
1             package Pinwheel::Helpers::Tag;
2              
3 4     4   45471 use strict;
  4         9  
  4         136  
4 4     4   20 use warnings;
  4         6  
  4         96  
5              
6 4     4   20 use Exporter;
  4         6  
  4         190  
7              
8             require Pinwheel::Controller;
9 4     4   744 use Pinwheel::View::String;
  4         16  
  4         3070  
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(
13             content_tag
14             link_to
15             link_to_if
16             link_to_unless
17             link_to_unless_current
18             );
19              
20              
21             sub content_tag
22             {
23 36     36 0 200 my ($tag, $content, $block, $s, $key, $value);
24              
25 36         62 $tag = shift(@_);
26 36 100 100     362 if (@_ && ref($_[-1]) eq 'CODE') {
    100          
27 2         5 $block = pop(@_);
28 2         6 $content = &$block();
29 2         22 $content =~ s/^\s*(.*?)\s*$/$1/;
30 2         8 $content = [$content];
31             } elsif (@_ & 1) {
32 20         40 $content = shift(@_);
33             }
34              
35 36         362 $s = [["<$tag"]];
36 36         100 while (@_ > 1) {
37 39         47 $key = shift;
38 39         48 $value = shift;
39 39 100       97 if (ref($value) eq 'ARRAY') {
40 9 100       18 $value = [grep { defined($_) && length($_) } @$value];
  18         83  
41 9 100       31 $value = scalar(@$value) ? join(' ', @$value) : undef;
42             }
43 39 100       90 next if !defined($value);
44 35         390 push @$s, [" $key=\""], $value, ['"'];
45             }
46 36 100       78 if (defined($content)) {
47 22         100 push @$s, ['>'], $content, ["\n"];
48             } else {
49 14         33 push @$s, [" />\n"];
50             }
51 36         368 return Pinwheel::View::String->new($s);
52             }
53              
54             sub _link_to_href
55             {
56 21     21   30 my ($route_name, $route_params);
57 21         32 my ($params) = @_;
58              
59 21 100 100     142 if (scalar(@$params) > 1 and ref(@$params[1]) eq 'HASH') {
60 10         20 $route_name = shift(@$params);
61 10         153 $route_params = shift(@$params);
62 10         61 return Pinwheel::Controller::url_for($route_name, %$route_params);
63             } else {
64 11         33 return shift(@$params);
65             }
66             }
67              
68             sub link_to
69             {
70 15     15 0 35 my ($content, $href);
71              
72 15         29 $content = shift(@_);
73 15         39 $href = _link_to_href(\@_);
74              
75 15         52 return content_tag('a', $content, href => $href, @_);
76             }
77              
78             sub link_to_if
79             {
80 12     12 0 32 my ($condition, @params) = @_;
81 12 100       28 if ($condition) {
82 8         24 return link_to(@params);
83             } else {
84 4         17 return $params[0];
85             }
86             }
87              
88             sub link_to_unless
89             {
90 9     9 0 1071 my ($condition, @params) = @_;
91 9         29 return link_to_if(!$condition, @params);
92             }
93              
94             sub link_to_unless_current
95             {
96 6     6 0 1035 my ($content, $href);
97              
98 6         10 $content = shift(@_);
99 6         19 $href = _link_to_href(\@_);
100              
101 6         55 return link_to_unless((Pinwheel::Controller::url_for() eq $href), $content, $href, @_);
102             }
103              
104             1;