HiD/Role/IsPost.pm

filter: Statementstmt 86.9% 100 / 115 Branchbran 61.9% 26 / 42 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.7% 21 / 26 Podpod 100% 2 / 2 totaltotal 76.9% 150 / 195 CC 24 SCAR 14.5
CC
24
Cov
78%
_build_author
12.0
is_draft
12.0
__ANON__
6.2
CRAP 4.3
« HiD/Pager.pm ↑ Summary HiD/Processor/IIBlog.pm »
1 # ABSTRACT: Role for blog posts
2  
3  
4 package HiD::Role::IsPost;
5 8 $HiD::Role::IsPost::VERSION = '1.5';
6 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use warnings qw/ FATAL utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use open qw/ :std :utf8 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use charnames qw/ :full /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use feature qw/ unicode_strings /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Date::Parse qw/ str2time /;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use File::Basename qw/ fileparse /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use HiD::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 8 has author => (
25 is => 'ro' ,
26 isa => 'Str' ,
27 lazy => 1 ,
28 builder => '_build_author' ,
29 );
30  
31 sub _build_author {
32 0 ▶ my $self = shift;
Subroutine _build_author: not called CC=3 SCAR=24.8
33  
34 0 my $author = $self->get_metadata( 'author' );
35 0 ▶ return $author if defined $author;
Branch: 1 branch
BranchTrueFalseTotal
if defined $author 0 0 0
36  
37 0 my $default_author = $self->get_config( 'default_author' );
38 0 ▶ return $default_author if defined $default_author;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default_author 0 0 0
39  
40 0 die "Need author for " . $self->basename . "\n"
41 }
42  
43  
44 has categories => (
45 is => 'ro' ,
46 isa => 'ArrayRef' ,
47 lazy => 1 ,
48 default => sub {
49 19 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
50  
51 19 ▶ if ( my $category = $self->get_metadata( 'category' )) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $category = $self->get_metadata('category')) { } 1 18 19
elsif (my $categories = $self->get_metadata('categories')) { } 2 16 18
52 1 return [ $category ];
53 }
54 elsif ( my $categories = $self->get_metadata( 'categories' )) {
55 2 ▶ if ( ref $categories ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $categories) { } 1 1 2
56 1 return [ @$categories ];
57 }
58 else {
59 1 my @categories = split /\s/ , $categories;
60 1 return [ @categories ];
61 }
62 }
63 16 else { return [] }
64 },
65 8 );
66  
67  
68 has date => (
69 is => 'ro' ,
70 isa => 'DateTime' ,
71 lazy => 1,
72 handles => {
73 day => 'day' ,
74 month => 'month' ,
75 strftime => 'strftime' ,
76 year => 'year' ,
77 },
78 default => sub {
79 47 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=14.8
80  
81 47 ▶ if ( $self->get_config( 'publish_drafts' )){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_config("publish_drafts")) 0 47 47
82 0 ▶ return DateTime->now if $self->is_draft;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_draft 0 0 0
83 }
84  
85 47 my( $year , $month , $day );
86 47 ▶ if ( my $date = $self->get_metadata( 'date' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $date = $self->get_metadata('date')) { } 6 41 47
87 6 return DateTime->from_epoch(
88 epoch => str2time( $date ),
89 time_zone => 'local' ,
90 );
91 }
92 else {
93 41 ( $year , $month , $day ) = $self->input_filename
94 =~ m|^.*?/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})-|;
95 41 return DateTime->new(
96 year => $year ,
97 month => $month ,
98 day => $day
99 );
100 }
101 },
102 8 );
103  
104  
105 8 has description => (
106 is => 'ro' ,
107 isa => 'Maybe[Str]' ,
108 lazy => 1 ,
109 builder => '_build_description' ,
110 );
111 0 ▶sub _build_description { shift->get_metadata( 'description' ) }
Subroutine _build_description: not called CC=1 SCAR=6.9
112  
113  
114 8 has excerpt => (
115 is => 'ro',
116 isa => 'Str',
117 lazy => 1,
118 builder => '_build_excerpt',
119 );
120  
121 sub _build_excerpt {
122 10 ▶ my $self = shift;
Subroutine _build_excerpt: called CC=3 SCAR=11.0
123  
124 10 my $content = $self->content;
125  
126 10 ▶ return unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 10 10
127  
128 10 my $sep = $self->hid->excerpt_separator;
129  
130 10 ▶ if($content =~ /^$sep/mp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^$sep/mpu) 1 9 10
131 1 return ${^PREMATCH};
132 }
133  
134 9 return $content;
135 }
136  
137  
138 has tags => (
139 is => 'ro' ,
140 isa => 'ArrayRef',
141 traits => [ qw/ Array / ] ,
142 handles => { join_tags => 'join' } ,
143 default => sub {
144 54 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
145  
146 54 ▶ if ( my $tag = $self->get_metadata( 'tag' )) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $tag = $self->get_metadata('tag')) { } 2 52 54
elsif (my $tags = $self->get_metadata('tags')) { } 2 50 52
147 2 return [ $tag ];
148 }
149 elsif ( my $tags = $self->get_metadata( 'tags' )) {
150 2 ▶ if ( ref $tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tags) { } 1 1 2
151 1 return [ @$tags ];
152 }
153 else {
154 1 my @tags = split /\s/ , $tags;
155 1 return [ @tags ];
156 }
157 }
158 50 else { return [] }
159 } ,
160 8 );
161  
162  
163 8 has title => (
164 is => 'ro' ,
165 isa => 'Str' ,
166 lazy => 1 ,
167 builder => '_build_title' ,
168 );
169  
170 sub _build_title {
171 10 ▶ my $self = shift;
Subroutine _build_title: called CC=3 SCAR=11.4
172  
173 10 my $title = $self->get_metadata( 'title' );
174  
175 10 ▶ return $self->basename unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 10 10
176  
177 10 ▶ return ( ref $title ) ? $$title : $title;
Branch: 1 branch
BranchTrueFalseTotal
ref $title ? : 0 10 10
178 }
179  
180  
181 8 has twitter => (
182 is => 'ro' ,
183 isa => 'Maybe[Str]' ,
184 lazy => 1 ,
185 builder => '_build_twitter' ,
186 );
187  
188 sub _build_twitter {
189 0 ▶ my $self = shift;
Subroutine _build_twitter: not called CC=2 SCAR=17.9
190  
191 0 my $twitter = $self->get_metadata( 'twitter' );
192  
193 0 ▶ return defined $twitter ? $twitter : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $twitter ? : 0 0 0
194 }
195  
196 around BUILDARGS => sub {
197 54 ▶ my $orig = shift;
Subroutine __ANON__: called CC=6 SCAR=18.3
198 54 my $class = shift;
199  
200 54 ▶ my %args = ( ref $_[0] and ref $_[0] eq 'HASH' ) ? %{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq 'HASH' ? : 54 0 54
Condition33%1/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
0054
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
201  
202 54 ▶ if ( my $input = $args{input_filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $input = $args{"input_filename"}) 54 0 54
203 54 ▶ if ( my $source = $args{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $source = $args{"source"}) 10 44 54
204 10 $input =~ s|$source/?||;
205 }
206  
207 54 ▶ if ( my( $cat ) = $input =~ m|^(.+?)/?_posts/| ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($cat) = $input =~ m[^(.+?)/?_posts/]u) 5 49 54
208 5 $args{categories} = [ split '/' , $cat ];
209 }
210 }
211  
212 54 return $class->$orig( \%args );
213 8 };
214  
215  
216 0 ▶sub all_tags { shift->join_tags(',') }
Subroutine all_tags: not called CC=1 SCAR=6.9
Pod documented
217  
218  
219 8 my $drafts_dir;
220 sub is_draft {
221 0 ▶ my $self = shift;
Subroutine is_draft: not called CC=3 SCAR=24.8
Pod documented
222  
223 0 ▶ $drafts_dir //= $self->get_config( 'drafts_dir' );
Condition0%0/3
$drafts_dir //= $self->get_config("drafts_dir")
l!l&&r!l&&!r
000
MC/DC0%0/2
$drafts_dir //= $self->get_config("drafts_dir")
$drafts_dir $self->get_config("drafts_dir")
Truth table0%0/3
A //= B
A= $drafts_dirB= $self->get_config("drafts_dir")
ABresult
1X1
011
000
224 0 ▶ return ( $self->input_filename =~ /^$drafts_dir/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->input_filename =~ /^$drafts_dir/u ? : 0 0 0
225 }
226  
227  
228 8 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 8 1;
230  
231 __END__
232  
233 =pod
234  
235 =encoding utf-8
236  
237 =head1 NAME
238  
239 HiD::Role::IsPost - Role for blog posts
240  
241 =head1 SYNOPSIS
242  
243 package HiD::ThingThatIsAPost;
244 use Moose;
245 with 'HiD::Role::IsPost';
246  
247 ...
248  
249 1;
250  
251 =head1 DESCRIPTION
252  
253 This role is consumed by objects that are blog posts and provides blog
254 post-specific attributes and methods.
255  
256 =head1 ATTRIBUTES
257  
258 =head2 author
259  
260 =head2 categories
261  
262 =head2 date
263  
264 DateTime object for this post.
265  
266 =head2 description
267  
268 A one-line synopsis of the post (used, e.g., for metadata information used by Open Graph)
269  
270 =head2 excerpt
271  
272 It is generally useful to summarize or lead a post with a "read more" tag
273 added to the end of the post. This is ideal for a blog where we might not
274 want to list the full post on the front page.
275  
276 =head2 tags
277  
278 =head2 title
279  
280 =head2 twitter
281  
282 =head1 METHODS
283  
284 =head2 all_tags
285  
286 Returns all tags for this post, joined together with commas
287  
288 =head2 is_draft
289  
290 Returns a boolean value indicating whether this post is coming from the drafts
291 folder or not.
292  
293 =head1 VERSION
294  
295 version 1.5
296  
297 =head1 AUTHOR
298  
299 John SJ Anderson <genehack@genehack.org>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 This software is copyright (c) 2014 by John SJ Anderson.
304  
305 This is free software; you can redistribute it and/or modify it under
306 the same terms as the Perl 5 programming language system itself.
307  
308 =cut
« HiD/Pager.pm ↑ Summary HiD/Processor/IIBlog.pm »