File Coverage

blib/lib/Mira/Config.pm
Criterion Covered Total %
statement 59 98 60.2
branch 8 52 15.3
condition 5 15 33.3
subroutine 13 14 92.8
pod 0 1 0.0
total 85 180 47.2


line stmt bran cond sub pod time code
1             package Mira::Config;
2             $Mira::Config::VERSION = '0.07';
3              
4 1     1   868 use strict;
  1         1  
  1         20  
5 1     1   3 use warnings;
  1         1  
  1         17  
6 1     1   3 use utf8;
  1         0  
  1         3  
7              
8 1     1   421 use YAML;
  1         6312  
  1         44  
9 1     1   5 use File::Spec;
  1         1  
  1         15  
10 1     1   381 use File::Spec::Functions;
  1         522  
  1         61  
11 1     1   4 use File::Basename qw/basename/;
  1         1  
  1         48  
12 1     1   3 use Carp;
  1         1  
  1         42  
13 1     1   488 use Encode;
  1         6684  
  1         53  
14 1     1   929 use Encode::Locale;
  1         2092  
  1         36  
15 1     1   4 use File::Path qw(make_path);
  1         1  
  1         43  
16 1     1   12 use 5.012;
  1         2  
17              
18             sub new {
19 1     1 0 1539 my $class = shift;
20 1         2 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         3 $self->{_default} = Load( $yaml );
35             };
36 1 50       16317 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     10 $self->{_default}->{post_num} = "5" unless (exists $self->{_default}->{post_num} and $self->{_default}->{post_num});
43 1 50 33     5 $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     6 $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         10 my $glob = catfile($source, 'content', '*');
49              
50 1         13 my @floors = glob encode(locale_fs => $glob);
51 1         170 @floors = grep {-d} @floors;
  0         0  
52 1         21 @floors = map {decode(locale_fs => basename($_))} @floors;
  0         0  
53              
54 1         3 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 0 0       0 $self->{$floor}->{feed_post_num} = $self->{_default}->{feed_post_num} unless $self->{$floor}->{feed_post_num};
91             } else
92             {
93 0         0 $self->{$floor} = _not_valids($floor, $self);
94             }
95             }
96              
97 1         4 return $self;
98             }
99              
100             sub _not_valids {
101 0     0     my $floor = shift;
102 0           my $self = shift;
103             my $configs = {
104             title => "$floor",
105             root => "$self->{_default}->{root}/$floor/",
106             url => "$self->{_default}->{url}/$floor",
107             author => $self->{_default}->{author},
108             permalink => ":year/:month/:day/:title/",
109             default_markup => "markdown",
110             post_num => "5",
111             archive_post_num => "20",
112             feed_post_num => "20",
113             static => $self->{_default}->{static},
114             imageurl => $self->{_default}->{imageurl},
115             template => $self->{_default}->{template},
116             lists => $self->{_default}->{lists},
117             namespace => $self->{_default}->{namespace},
118 0           };
119 0           return $configs;
120             }
121              
122             1;