File Coverage

blib/lib/Perlanet/Trait/YAMLConfig.pm
Criterion Covered Total %
statement 34 37 91.9
branch 9 10 90.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 52 56 92.9


line stmt bran cond sub pod time code
1             package Perlanet::Trait::YAMLConfig;
2              
3 6     6   2059 use strict;
  6         11  
  6         142  
4 6     6   22 use warnings;
  6         8  
  6         159  
5              
6 6     6   24 use Moose::Role;
  6         8  
  6         52  
7 6     6   17120 use namespace::autoclean;
  6         11  
  6         45  
8              
9             =head1 NAME
10            
11             Perlanet::Trait::YAMLConfig - configure Perlanet through a YAML configuration
12             file
13            
14             =head1 SYNOPSIS
15            
16             package MyPerlanet;
17             extends 'Perlanet';
18             with 'Perlanet::Traits::YAMLConfig';
19            
20             my $perlanet = MyPerlanet->new_with_config(
21             configfile => 'whatever.yml'
22             );
23            
24             $perlanet->run;
25            
26             =head1 DESCRIPTION
27            
28             Allows you to move the configuration of Perlanet to an external YAML
29             configuration file.
30            
31             =head2 Example Configuration File
32            
33             title: planet test
34             description: A Test Planet
35             url: http://planet.example.com/
36             author:
37             name: Dave Cross
38             email: dave@dave.org.uk
39             entries: 20
40             opml: opml.xml
41             page:
42             file: index.html
43             template: index.tt
44             feed:
45             file: atom.xml
46             format: Atom
47             cache_dir: /tmp/feeds
48             feeds:
49             - url: http://blog.dave.org.uk/atom.xml
50             title: Dave's Blog
51             web: http://blog.dave.org.uk/
52             - url: http://use.perl.org/~davorg/journal/rss
53             title: Dave's use.perl Journal
54             web: http://use.perl.org/~davorg/journal/
55             - url: http://www.oreillynet.com/pub/feed/31?au=2607
56             title: Dave on O'Reillynet
57             web: http://www.oreillynet.com/pub/au/2607
58            
59             =head1 METHODS
60            
61             =head2 THIRTY_DAYS
62            
63             The default length of caching, if caching options are present in the
64             configuration
65            
66             =head2 get_config_from_file
67            
68             Extracts the configuration from a YAML file
69            
70             =cut
71              
72             with 'MooseX::ConfigFromFile';
73              
74 6     6   459 use Carp qw( carp croak );
  6         16  
  6         267  
75 6     6   24 use YAML qw( LoadFile );
  6         6  
  6         244  
76              
77 6     6   22 use constant THIRTY_DAYS => 30 * 24 * 60 * 60;
  6         7  
  6         1535  
78              
79             sub get_config_from_file {
80 5     5 1 899212   my ($self, $file) = @_;
81              
82 5 100       270   open my $cfg_file, '<:utf8', $file
83                 or croak "Cannot open file $file: $!";
84              
85 4         34   my $cfg = LoadFile($cfg_file);
86              
87 5         79   $cfg->{feeds} = [ map {
88 4         15     Perlanet::Feed->new($_)
89 4         135576   } @{ $cfg->{feeds} } ];
90              
91 4 100       191   $cfg->{max_entries} = $cfg->{entries}
92                 if $cfg->{entries};
93              
94 4 100       10   if ($cfg->{cache_dir}) {
95 1         2     eval { require CHI; };
  1         4  
96              
97 1 50       2     if ($@) {
98 0         0       carp "You need to install CHI to enable caching.\n";
99 0         0       carp "Caching disabled for this run.\n";
100 0         0       delete $cfg->{cache_dir};
101                 }
102               }
103              
104 4 100       17   $cfg->{cache_dir}
105                 and $cfg->{cache} = CHI->new(
106                   driver => 'File',
107                   root_dir => delete $cfg->{cache_dir},
108                   expires_in => THIRTY_DAYS,
109                 );
110              
111 4         315382   return $cfg;
112             }
113              
114             =head1 AUTHOR
115            
116             Oliver Charles, <oliver.g.charles@googlemail.com>
117            
118             =head1 COPYRIGHT AND LICENSE
119            
120             Copyright (c) 2010 by Magnum Solutions Ltd.
121            
122             This library is free software; you can redistribute it and/or modify
123             it under the same terms as Perl itself, either Perl version 5.10.0 or,
124             at your option, any later version of Perl 5 you may have available.
125            
126             =cut
127              
128             1;
129