File Coverage

blib/lib/Rapi/Blog/Scaffold/Config.pm
Criterion Covered Total %
statement 21 46 45.6
branch 0 10 0.0
condition 0 5 0.0
subroutine 7 13 53.8
pod 0 1 0.0
total 28 75 37.3


line stmt bran cond sub pod time code
1             package Rapi::Blog::Scaffold::Config;
2 1     1   9 use strict;
  1         3  
  1         35  
3 1     1   7 use warnings;
  1         2  
  1         35  
4              
5 1     1   6 use RapidApp::Util qw(:all);
  1         3  
  1         644  
6 1     1   11 use Rapi::Blog::Util;
  1         8  
  1         23  
7              
8 1     1   6 use Moo;
  1         2  
  1         6  
9 1     1   2885 use Types::Standard ':all';
  1         3  
  1         11  
10              
11 1     1   48267 use YAML::XS 0.64 'LoadFile';
  1         33  
  1         1004  
12              
13             has 'favicon', is => 'rw', isa => Maybe[Str], default => sub { 'favicon.ico' };
14             has 'landing_page', is => 'rw', isa => Maybe[Str], default => sub { 'index.html' };
15             has 'internal_post_path', is => 'rw', isa => Maybe[Str], default => sub { 'private/post/' };
16             has 'default_view_path', is => 'rw', isa => Maybe[Str], default => sub { 'post/' };
17             has 'not_found', is => 'rw', isa => Maybe[Str], default => sub { undef };
18             has 'view_wrappers', is => 'rw', isa => ArrayRef[HashRef], default => sub { [] };
19             has 'static_paths', is => 'rw', isa => ArrayRef, default => sub { [] };
20             has 'private_paths', is => 'rw', isa => ArrayRef, default => sub { [] };
21             has 'default_ext', is => 'rw', isa => Maybe[Str], default => sub { 'html' };
22              
23             has 'preview_path', is => 'rw', lazy => 1, default => sub { (shift)->default_view_path }, isa => Maybe[Str];
24              
25              
26             has '_supplied_params', is => 'ro', isa => HashRef, required => 1;
27             around BUILDARGS => sub {
28             my $orig = shift;
29             my $class = shift;
30             my %params = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref
31              
32             $params{_supplied_params} = { %params };
33             $class->$orig(%params)
34             };
35              
36              
37             sub BUILD {
38 0     0 0   my $self = shift;
39 0           $self->_apply_params( $self->_supplied_params )
40             }
41              
42            
43             has '_extra_params', is => 'rw', isa => HashRef, default => sub { {} };
44             sub AUTOLOAD {
45 0     0     my $self = shift;
46 0           my $meth = (reverse(split('::',our $AUTOLOAD)))[0];
47 0           $self->_extra_params->{$meth}
48             }
49              
50              
51             sub _load_from_yaml {
52 0     0     my $self = shift;
53 0 0         my $yaml_file = shift or die "Missing required yaml_file argument";
54 0 0         -e $yaml_file or die "YAML file '$yaml_file' not found";
55            
56 0           my $data = LoadFile( $yaml_file );
57            
58 0           $self->_apply_params( $data, 1 );
59            
60             #for (keys %$data) {
61             # if ($self->_supplied_params->{$_}) { # Don't override any user-supplied params
62             # delete $data->{$_}
63             # }
64             # elsif ($self->can($_)) {
65             # $self->$_( delete $data->{$_} )
66             # }
67             #}
68             #
69             ## Save leftover params so they can still be accessed via AUTOLOAD
70             #$self->_extra_params( $data );
71            
72             }
73              
74              
75              
76             sub _apply_params {
77 0     0     my ($self, $new_params, $no_ovr) = @_;
78            
79 0           my $params = clone( $new_params );
80            
81 0           for (keys %$params) {
82 0 0 0       if ($no_ovr && $self->_supplied_params->{$_}) { # Don't override any user-supplied params
    0          
83 0           delete $params->{$_}
84             }
85             elsif ($self->can($_)) {
86 0           $self->$_( delete $params->{$_} )
87             }
88             }
89            
90             # Save leftover params so they can still be accessed via AUTOLOAD
91 0   0       my $extra = $self->_extra_params || {};
92 0           $self->_extra_params( { %$extra, %$params } );
93             }
94              
95              
96              
97             sub _has_param {
98 0     0     my ($self, $p) = @_;
99 0 0         $self->can($p) || exists $self->_extra_params->{$p}
100             }
101              
102              
103              
104             sub _all_as_hash {
105 0     0     my $self = shift;
106            
107             return {
108 0           map { $_ => $self->$_ }
109 0           grep { ! /^_/ } # exclude "private" attributes with start with underscore '_'
110 0           keys %{ $self->_extra_params },
  0            
111             $self->meta->get_attribute_list
112             }
113             }
114              
115              
116             1;