Perlanet/Role/Config.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 93.3% 42 / 45 CC 5 SCAR 11.1
CC
5
Cov
92%
get_config
5.1
BEGIN
1.0
BEGIN
1.0
CRAP 3.0
« Perlanet.pm ↑ Summary Perlanet/Feed.pm »
1 package Perlanet::Role::Config;
2  
3 11 ▶use 5.24.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use constant THIRTY_DAYS => 30 * 24 * 60 * 60;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Perlanet::Role::Config - A role to be used in Perlanet traits that read config.
14  
15 =head1 SYNOPSIS
16  
17 package Perlanet::Trait::MyConfig;
18  
19 use Moose::Role;
20  
21 with 'Perlanet::Role::Config';
22  
23 # Class method to return the config
24 sub get_config_from_somewhere {
25 my $class = shift;
26  
27 # get_config() is defined in this role
28 return $class->get_config(@_)
29 }
30  
31 # get_config() calls this to actually read the config.
32 sub read_config {
33 my $class = shift;
34 my %params = @_;
35  
36 my $cfg = ...;
37  
38 return $cfg;
39 }
40  
41 =head1 DESCRIPTION
42  
43 A role that is used to build Perlanet traits which read config information
44 from various sources.
45  
46 This role does two things:
47  
48 =over 4
49  
50 =item *
51  
52 It exposes a method called C<get_config()> which gets the raw config data
53 (using your C<read_config()> method).
54  
55 =item *
56  
57 It forces your trait to define a method called C<read_config()> which actually
58 reads the config and returns it as a hash reference.
59  
60 =back
61  
62 =head2 METHODS
63  
64 =head3 get_config
65  
66 Calls your C<read_config()> method to actually get the config data (as a hash
67 reference) and then munges that data in various ways to get a useful config
68 hash.
69  
70 =cut
71  
72 11 ▶sub get_config {
Subroutine get_config: called CC=5 SCAR=16.2
Pod documented
73 11 my $class = shift;
74 11 my (%params) = @_;
75
76 11 my $cfg = $class->read_config(%params);
77
78 10 $cfg->{config} = $cfg;
79  
80 $cfg->{feeds} = [ map {
81 12 Perlanet::Feed->new($_)
82 10 } $cfg->{feeds}->@* ];
83
84 $cfg->{max_entries} = $cfg->{entries}
85 10 ▶ if $cfg->{entries};
Branch: 1 branch
BranchTrueFalseTotal
if $cfg->{"entries"} 5 5 10
86
87 10 ▶ if ($cfg->{cache_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"cache_dir"}) 1 9 10
88 1 eval { require CHI; };
89
90 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
91 0 warn "You need to install CHI to enable caching.\n",
92 "Caching disabled for this run.\n";
93 0 delete $cfg->{cache_dir};
94 }
95 }
96
97 $cfg->{cache_dir}
98 and $cfg->{cache} = CHI->new(
99 driver => 'File',
100 root_dir => delete $cfg->{cache_dir},
101 10 ▶ expires_in => THIRTY_DAYS,
Branch: 1 branch
BranchTrueFalseTotal
$cfg->{"cache_dir"} and $cfg->{"cache"} = "CHI"->new("driver", "File", "root_dir", delete $cfg->{"cache_dir"}, "expires_in", 2592000) 1 9 10
102 );
103
104 10 return $cfg;
105 }
106  
107 =head3 THIRTY_DAYS
108  
109 Constant to define the default cache expiration time in seconds.
110  
111 =head1 AUTHOR
112  
113 Dave Cross, <dave@perlhacks.com>
114  
115 =head1 COPYRIGHT AND LICENSE
116  
117 Copyright (c) 2022 by Magnum Solutions Ltd.
118  
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself, either Perl version 5.10.0 or,
121 at your option, any later version of Perl 5 you may have available.
122  
123 =cut
124  
125 11 1;
« Perlanet.pm ↑ Summary Perlanet/Feed.pm »