| 1 | package Perlanet::Trait::YAMLConfig; | ||||||||||||
| 2 | |||||||||||||
| 3 | 11 | ▶ | use 5.10.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 | 11 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Perlanet::Trait::YAMLConfig - configure Perlanet through a YAML configuration | ||||||||||||
| 13 | file | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | package MyPerlanet; | ||||||||||||
| 18 | extends 'Perlanet'; | ||||||||||||
| 19 | with 'Perlanet::Traits::YAMLConfig'; | ||||||||||||
| 20 | |||||||||||||
| 21 | my $perlanet = MyPerlanet->new_with_config( | ||||||||||||
| 22 | configfile => 'whatever.yml' | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | $perlanet->run; | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||
| 28 | |||||||||||||
| 29 | Allows you to move the configuration of Perlanet to an external YAML | ||||||||||||
| 30 | configuration file. | ||||||||||||
| 31 | |||||||||||||
| 32 | =head2 Example Configuration File | ||||||||||||
| 33 | |||||||||||||
| 34 | title: planet test | ||||||||||||
| 35 | description: A Test Planet | ||||||||||||
| 36 | url: http://planet.example.com/ | ||||||||||||
| 37 | author: | ||||||||||||
| 38 | name: Dave Cross | ||||||||||||
| 39 | email: dave@dave.org.uk | ||||||||||||
| 40 | entries: 20 | ||||||||||||
| 41 | opml_file: opml.xml | ||||||||||||
| 42 | page: | ||||||||||||
| 43 | file: index.html | ||||||||||||
| 44 | template: index.tt | ||||||||||||
| 45 | feed: | ||||||||||||
| 46 | file: atom.xml | ||||||||||||
| 47 | format: Atom | ||||||||||||
| 48 | cache_dir: /tmp/feeds | ||||||||||||
| 49 | feeds: | ||||||||||||
| 50 | - feed: http://blog.dave.org.uk/atom.xml | ||||||||||||
| 51 | title: Dave's Blog | ||||||||||||
| 52 | web: http://blog.dave.org.uk/ | ||||||||||||
| 53 | - feed: http://use.perl.org/~davorg/journal/rss | ||||||||||||
| 54 | title: Dave's use.perl Journal | ||||||||||||
| 55 | web: http://use.perl.org/~davorg/journal/ | ||||||||||||
| 56 | - feed: http://www.oreillynet.com/pub/feed/31?au=2607 | ||||||||||||
| 57 | title: Dave on O'Reillynet | ||||||||||||
| 58 | web: http://www.oreillynet.com/pub/au/2607 | ||||||||||||
| 59 | |||||||||||||
| 60 | =head1 METHODS | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 get_config_from_file | ||||||||||||
| 63 | |||||||||||||
| 64 | Extracts the configuration from a YAML file | ||||||||||||
| 65 | |||||||||||||
| 66 | =cut | ||||||||||||
| 67 | |||||||||||||
| 68 | 11 | with 'MooseX::ConfigFromFile', 'Perlanet::Role::Config'; | |||||||||||
| 69 | |||||||||||||
| 70 | 11 | ▶ | use YAML qw( LoadFile ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | |||||||||||||
| 72 | 11 | ▶ | sub get_config_from_file { | ||||||||||
|
Subroutine
get_config_from_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 73 | 11 | my $class = shift; | |||||||||||
| 74 | 11 | return $class->get_config(config_file => $_[0]); | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | =head2 read_config | ||||||||||||
| 78 | |||||||||||||
| 79 | Actually reads the YAML. | ||||||||||||
| 80 | |||||||||||||
| 81 | =cut | ||||||||||||
| 82 | |||||||||||||
| 83 | 11 | ▶ | sub read_config { | ||||||||||
|
Subroutine
read_config: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 84 | 11 | my $class = shift; | |||||||||||
| 85 | 11 | my (%params) = @_; | |||||||||||
| 86 | |||||||||||||
| 87 | my $file = $params{config_file} | ||||||||||||
| 88 | 11 | ▶ | // die "No config file passed to read_config().\n"; | ||||||||||
|
Condition50%1/2
$params{"config_file"} // die("No config file passed to read_config().\n")
MC/DC0%0/1
$params{"config_file"} // die("No config file passed to read_config().\n")
$params{"config_file"}
Truth table50%1/2
A
A= $params{"config_file"}
| |||||||||||||
| 89 | |||||||||||||
| 90 | 11 | ▶ | open my $cfg_file, '<:encoding(UTF-8)', $file | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | or die "Cannot open file $file: $!\n"; | ||||||||||||
| 92 | |||||||||||||
| 93 | 10 | my $cfg = LoadFile($cfg_file); | |||||||||||
| 94 | |||||||||||||
| 95 | 10 | return $cfg; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 AUTHOR | ||||||||||||
| 99 | |||||||||||||
| 100 | Oliver Charles, <oliver.g.charles@googlemail.com> | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 103 | |||||||||||||
| 104 | Copyright (c) 2010 by Magnum Solutions Ltd. | ||||||||||||
| 105 | |||||||||||||
| 106 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 107 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||||||||||||
| 108 | at your option, any later version of Perl 5 you may have available. | ||||||||||||
| 109 | |||||||||||||
| 110 | =cut | ||||||||||||
| 111 | |||||||||||||
| 112 | 11 | 1; | |||||||||||