File Coverage

blib/lib/Badge/Depot.pm
Criterion Covered Total %
statement 30 30 100.0
branch 6 6 100.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 51 51 100.0


line stmt bran cond sub pod time code
1 1     1   16388 use strict;
  1         3  
  1         36  
2 1     1   4 use warnings;
  1         7  
  1         50  
3              
4             package Badge::Depot;
5              
6             our $VERSION = '0.0102'; # VERSION
7             # ABSTRACT: A framework for badges
8              
9 1     1   441 use Moose::Role;
  1         436843  
  1         8  
10 1     1   5419 use Types::URI qw/Uri/;
  1         169965  
  1         16  
11 1     1   449 use Types::Standard qw/Str/;
  1         2  
  1         8  
12 1     1   1146 use MooseX::AttributeShortcuts;
  1         344719  
  1         6  
13 1     1   61103 use namespace::autoclean;
  1         3  
  1         12  
14              
15             has image_url => (
16             is => 'rw',
17             isa => Uri,
18             init_arg => undef,
19             coerce => 1,
20             predicate => 1,
21             );
22             has image_alt => (
23             is => 'rw',
24             isa => Str,
25             init_arg => undef,
26             predicate => 1,
27             );
28             has link_url => (
29             is => 'rw',
30             isa => Uri,
31             init_arg => undef,
32             coerce => 1,
33             predicate => 1,
34             );
35              
36             around qw/to_html to_markdown/ => sub {
37             my $next = shift;
38             my $self = shift;
39              
40             return '' if !$self->has_image_url;
41             $self->$next;
42             };
43              
44             sub to_html {
45 4     4 1 9 my $self = shift;
46              
47 4 100       190 my $image_text = $self->has_image_alt ? sprintf ' alt="%s"', $self->image_alt : '';
48              
49 4 100       188 if($self->has_link_url) {
50 2         78 return sprintf q{<a href="%s"><img src="%s"%s /></a>}, $self->link_url, $self->image_url, $image_text;
51             }
52             else {
53 2         80 return sprintf q{<img src="%s"%s />}, $self->image_url, $image_text;
54             }
55             }
56             sub to_markdown {
57 4     4 1 8 my $self = shift;
58              
59 4 100       181 if($self->has_link_url) {
60 2   100     81 return sprintf q<[![%s](%s)](%s)>, $self->image_alt // '', $self->image_url, $self->link_url;
61             }
62             else {
63 2   100     86 return sprintf q<![%s](%s)]>, $self->image_alt // '', $self->image_url;
64             }
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Badge::Depot - A framework for badges
78              
79             =head1 VERSION
80              
81             Version 0.0102, released 2015-02-18.
82              
83             =head1 SYNOPSIS
84              
85             # Define a badge class
86             package Badge::Depot::Plugin::Example;
87              
88             use Moose;
89             with 'Badge::Depot';
90              
91             has user => (
92             is => 'ro',
93             isa => 'Str',
94             required => 1,
95             );
96              
97             sub BUILD {
98             my $self = shift;
99             $self->link_url(sprintf 'https://example.com/users/%s', $self->user);
100             $self->image_url(sprintf 'https://example.com/users/%s.svg', $self->user);
101             $self->image_alt('Example text');
102             }
103              
104             # Somewhere else
105             my $badge = Badge::Depot::Plugin::Example->new(user => 'my_username');
106              
107             print $badge->to_html;
108             # prints '<a href="https://example.com/users/my_username"><img src="https://example.com/users/my_username.svg" alt="Example text" /></a>'
109              
110             =head1 DESCRIPTION
111              
112             C<Badge::Depot> is a framework for documentation badges. Using badges in your documentation can give
113             end users of your distribution dynamic information without you having to update the documentation.
114              
115             You only need use this distribution directly if you want to create a new badge in the C<Badge::Depot::Plugin> namespace.
116              
117             =head1 OVERVIEW
118              
119             C<Badge::Depot> is a L<Moose> role that adds a few attributes and methods.
120              
121             =head1 ATTRIBUTES
122              
123             These attributes are expected to be set when the badge class returns from C<new>. See L<synopsis|/SYNOPSIS>.
124              
125             =head2 image_url
126              
127             Required. L<Uri|Types::URI>.
128              
129             The url to the actual badge. The src attribute for the img tag when rendered to html.
130              
131             =head2 image_alt
132              
133             Optional. L<Str|Types::Standard>.
134              
135             The alternative text of the badge. The alt attribute for the img tag when rendered to html. No alternative text is created if this isn't set.
136              
137             =head2 link_url
138              
139             Optional (but recommended). L<Uri|Types::URI>.
140              
141             The url to link the badge to. The href attribute for the a tag when rendered to html. No link is created if this isn't set.
142              
143             =head1 METHODS
144              
145             These methods are used when rendering the badge, and are not useful inside badge classes.
146              
147             =head2 to_html
148              
149             Returns a string with the badge rendered as html.
150              
151             =head2 to_markdown
152              
153             Returns a string with the badge rendered as markdown.
154              
155             =head1 SEE ALSO
156              
157             =over 4
158              
159             =item *
160              
161             L<WWW::StatusBadge> (an alternative to this)
162              
163             =back
164              
165             =head1 SOURCE
166              
167             L<https://github.com/Csson/p5-Badge-Depot>
168              
169             =head1 HOMEPAGE
170              
171             L<https://metacpan.org/release/Badge-Depot>
172              
173             =head1 AUTHOR
174              
175             Erik Carlsson <info@code301.com>
176              
177             =head1 COPYRIGHT AND LICENSE
178              
179             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
180              
181             This is free software; you can redistribute it and/or modify it under
182             the same terms as the Perl 5 programming language system itself.
183              
184             =cut