File Coverage

blib/lib/Statocles/Link.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 15 15 100.0


line stmt bran cond sub pod time code
1             package Statocles::Link;
2             our $VERSION = '0.084';
3             # ABSTRACT: A link object to build <a> and <link> tags
4              
5 68     68   466 use Statocles::Base 'Class';
  68         158  
  68         672  
6 68     68   476044 use Scalar::Util qw( blessed );
  68         174  
  68         23480  
7              
8             #pod =attr href
9             #pod
10             #pod The URL location being linked to. Sets the C<href> attribute.
11             #pod
12             #pod =cut
13              
14             has href => (
15             is => 'rw',
16             isa => Str,
17             required => 1,
18             coerce => sub {
19             my ( $href ) = @_;
20             if ( blessed $href && $href->isa( 'Path::Tiny' ) ) {
21             return $href->absolute( '/' )->stringify;
22             }
23             return $href;
24             },
25             );
26              
27             #pod =attr text
28             #pod
29             #pod The text inside the link tag. Only useful for <a> links.
30             #pod
31             #pod =cut
32              
33             has text => (
34             is => 'ro',
35             isa => Maybe[Str],
36             lazy => 1,
37             default => sub {
38             # For ease of transition, let's default to title, which we used for the
39             # text prior to this class.
40             return $_[0]->title;
41             },
42             );
43              
44             #pod =attr title
45             #pod
46             #pod The title of the link. Sets the C<title> attribute.
47             #pod
48             #pod =cut
49              
50             has title => (
51             is => 'ro',
52             isa => Str,
53             );
54              
55             #pod =attr rel
56             #pod
57             #pod The relationship of the link. Sets the C<rel> attribute.
58             #pod
59             #pod =cut
60              
61             has rel => (
62             is => 'ro',
63             isa => Str,
64             );
65              
66             #pod =attr type
67             #pod
68             #pod The MIME type of the resource being linked to. Sets the C<type> attribute for C<link>
69             #pod tags.
70             #pod
71             #pod =cut
72              
73             has type => (
74             is => 'ro',
75             isa => Str,
76             );
77              
78             #pod =method new_from_element
79             #pod
80             #pod my $link = Statocles::Link->new_from_element( $dom_elem );
81             #pod
82             #pod Construct a new Statocles::Link out of a Mojo::DOM element (either an <a> or a <link>).
83             #pod
84             #pod =cut
85              
86             sub new_from_element {
87 54     54 1 310818 my ( $class, $elem ) = @_;
88             return $class->new(
89 106         1409 ( map {; $_ => $elem->attr( $_ ) } grep { $elem->attr( $_ ) } qw( href title rel ) ),
  162         2004  
90 54         91 ( map {; $_ => $elem->$_ } qw( text ) ),
  54         785  
91             );
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding UTF-8
101              
102             =head1 NAME
103              
104             Statocles::Link - A link object to build <a> and <link> tags
105              
106             =head1 VERSION
107              
108             version 0.084
109              
110             =head1 SYNOPSIS
111              
112             my $link = Statocles::Link->new( text => 'Foo', href => 'http://example.com' );
113             say $link->href;
114             say $link->text;
115              
116             say sprintf '<a href="%s">%s</a>', $link->href, $link->text;
117              
118             =head1 DESCRIPTION
119              
120             This object encapsulates a link (either an C<a> or C<link> tag in HTML). These objects
121             are friendly for templates and can provide some sanity checks.
122              
123             =head1 ATTRIBUTES
124              
125             =head2 href
126              
127             The URL location being linked to. Sets the C<href> attribute.
128              
129             =head2 text
130              
131             The text inside the link tag. Only useful for <a> links.
132              
133             =head2 title
134              
135             The title of the link. Sets the C<title> attribute.
136              
137             =head2 rel
138              
139             The relationship of the link. Sets the C<rel> attribute.
140              
141             =head2 type
142              
143             The MIME type of the resource being linked to. Sets the C<type> attribute for C<link>
144             tags.
145              
146             =head1 METHODS
147              
148             =head2 new_from_element
149              
150             my $link = Statocles::Link->new_from_element( $dom_elem );
151              
152             Construct a new Statocles::Link out of a Mojo::DOM element (either an <a> or a <link>).
153              
154             =head1 AUTHOR
155              
156             Doug Bell <preaction@cpan.org>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2016 by Doug Bell.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut