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