File Coverage

blib/lib/Mira/Config.pm
Criterion Covered Total %
statement 59 97 60.8
branch 8 50 16.0
condition 5 15 33.3
subroutine 13 14 92.8
pod 0 1 0.0
total 85 177 48.0


line stmt bran cond sub pod time code
1             package Mira::Config;
2             $Mira::Config::VERSION = '0.07';
3              
4 1     1   973 use strict;
  1         1  
  1         21  
5 1     1   3 use warnings;
  1         1  
  1         16  
6 1     1   3 use utf8;
  1         0  
  1         3  
7              
8 1     1   451 use YAML;
  1         6933  
  1         51  
9 1     1   8 use File::Spec;
  1         1  
  1         41  
10 1     1   534 use File::Spec::Functions;
  1         672  
  1         75  
11 1     1   4 use File::Basename qw/basename/;
  1         1  
  1         55  
12 1     1   5 use Carp;
  1         1  
  1         47  
13 1     1   501 use Encode;
  1         7665  
  1         61  
14 1     1   1090 use Encode::Locale;
  1         2471  
  1         42  
15 1     1   6 use File::Path qw(make_path);
  1         1  
  1         49  
16 1     1   14 use 5.012;
  1         2  
17              
18             sub new {
19 1     1 0 1804 my $class = shift;
20 1         1 my $source = shift;
21 1         2 my $self = {};
22             #$self->{_source} = $source;
23              
24 1         1 my $yaml;
25 1 50       5 if (-f catfile($source, 'config.yml') )
26             {
27             {
28 1 50       19 open my $fh, '<:encoding(UTF-8)', 'config.yml' or die $!;
  1         17  
29 1         37 local $/ = undef;
30 1         16 $yaml = <$fh>;
31 1         43 close $fh;
32             }
33 1         2 eval { #must read yaml message and print it in error output
34 1         5 $self->{_default} = Load( $yaml );
35             };
36 1 50       16668 if ($@) {croak "$source/config.yml have problem";}
  0         0  
37             } else
38             {
39 0         0 croak " ! - you are not in true path or --source is invalid path or /conf/config.yml isn't valid file";
40             }
41              
42 1 50 33     11 $self->{_default}->{post_num} = "3" unless (exists $self->{_default}->{post_num} and $self->{_default}->{post_num});
43 1 50 33     6 $self->{_default}->{archive_post_num} = "20" unless (exists $self->{_default}->{archive_post_num} and $self->{_default}->{archive_post_num});
44 1 50 33     5 $self->{_default}->{feed_post_num} = "20" unless (exists $self->{_default}->{feed_post_num} and $self->{_default}->{feed_post_num});
45 1 50 33     5 $self->{_default}->{default_floor} = "blog" unless (exists $self->{_default}->{default_floor} and $self->{_default}->{default_floor});
46 1 50 33     5 $self->{_default}->{date_format} = "gregorian" unless (exists $self->{_default}->{date_format} and $self->{_default}->{date_format});
47              
48 1         11 my $glob = catfile($source, 'content', '*');
49              
50 1         16 my @floors = glob encode(locale_fs => $glob);
51 1         238 @floors = grep {-d} @floors;
  0         0  
52 1         35 @floors = map {decode(locale_fs => basename($_))} @floors;
  0         0  
53              
54 1         4 foreach my $floor (@floors)
55             {
56 0 0       0 if (-f catfile($source, 'config', "$floor.yml") )
57             {
58 0         0 my $flyaml = catfile($source, 'config', "$floor.yml");
59             {
60 0 0       0 open my $fh, '<:encoding(UTF-8)', $flyaml or die $!;
  0         0  
61 0         0 local $/ = undef;
62 0         0 $yaml = <$fh>;
63 0         0 close $fh;
64             }
65 0         0 my $floorconf;
66 0         0 eval { #must read yaml message and print it in error output
67 0         0 $floorconf = Load($yaml);
68             };
69 0 0       0 if ($@)
70             {
71 0         0 carp " # - $floor\.yml have problem, use default configs for floor: $floor";
72 0         0 $self->{$floor} = _not_valids($floor, $self);
73 0         0 next;
74             }
75 0         0 $self->{$floor} = $floorconf;
76 0 0       0 $self->{$floor}->{title} = $floor unless ($self->{$floor}->{title});
77 0 0       0 $self->{$floor}->{description} = $self->{_default}->{description} unless ($self->{$floor}->{description});
78 0 0       0 $self->{$floor}->{root} = "$self->{_default}->{root}/$floor" unless ($self->{$floor}->{root});
79 0 0       0 $self->{$floor}->{url} = "$self->{_default}->{url}/$floor" unless ($self->{$floor}->{url});
80 0 0       0 $self->{$floor}->{static} = $self->{_default}->{static} unless ($self->{$floor}->{static});
81 0 0       0 $self->{$floor}->{imageurl} = $self->{_default}->{imageurl} unless ($self->{$floor}->{imageurl});
82 0 0       0 $self->{$floor}->{author} = $self->{_default}->{author} unless ($self->{$floor}->{author});
83 0 0       0 $self->{$floor}->{email} = $self->{_default}->{email} unless ($self->{$floor}->{email});
84 0 0       0 $self->{$floor}->{template} = $self->{_default}->{template} unless ($self->{$floor}->{template});
85 0 0       0 $self->{$floor}->{lists} = $self->{_default}->{lists} unless ($self->{$floor}->{lists});
86 0 0       0 $self->{$floor}->{namespace} = $self->{_default}->{namespace} unless ($self->{$floor}->{namespace});
87 0 0       0 $self->{$floor}->{date_format} = $self->{_default}->{date_format} unless ($self->{$floor}->{date_format});
88 0 0       0 $self->{$floor}->{post_num} = $self->{_default}->{post_num} unless $self->{$floor}->{post_num};
89 0 0       0 $self->{$floor}->{archive_post_num} = $self->{_default}->{archive_post_num} unless $self->{$floor}->{archive_post_num};
90             } else
91             {
92 0         0 $self->{$floor} = _not_valids($floor, $self);
93             }
94             }
95              
96 1         9 return $self;
97             }
98              
99             sub _not_valids {
100 0     0     my $floor = shift;
101 0           my $self = shift;
102             my $configs = {
103             title => "$floor",
104             root => "$self->{_default}->{root}/$floor/",
105             url => "$self->{_default}->{url}/$floor",
106             author => $self->{_default}->{author},
107             permalink => ":year/:month/:day/:title/",
108             default_markup => "markdown",
109             post_num => "5",
110             archive_post_num => "20",
111             static => $self->{_default}->{static},
112             imageurl => $self->{_default}->{imageurl},
113             template => $self->{_default}->{template},
114             lists => $self->{_default}->{lists},
115             namespace => $self->{_default}->{namespace},
116 0           };
117 0           return $configs;
118             }
119              
120             1;