Configuration/Trait/Object.pm

filter: Statementstmt 97.0% 99 / 102 Branchbran 71.8% 23 / 32 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 1 totaltotal 85.3% 146 / 171 CC 25 SCAR 23.3
CC
25
Cov
88%
write_config_file
20.0
_from_config
5.1
_build_raw_config
3.2
CRAP 10.3
↑ Summary Configuration/Trait/Attribute/ConfigKey.pm »
1 package MooseX::Configuration::Trait::Object;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $MooseX::Configuration::Trait::Object::VERSION = '0.02';
4 }
5  
6 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Config::INI::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use List::AllUtils qw( uniq );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MooseX::Types -declare => ['MaybeFile'];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use MooseX::Types::Moose qw( HashRef Maybe Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use MooseX::Types::Path::Class qw( File );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Scalar::Util qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Text::Autoformat qw( autoformat );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 subtype MaybeFile,
22 as Maybe[File];
23  
24 coerce MaybeFile,
25 from Str,
26 0 ▶ via { Path::Class::File->new($_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
27  
28 1 has config_file => (
29 is => 'ro',
30 isa => MaybeFile,
31 coerce => 1,
32 lazy => 1,
33 builder => '_build_config_file',
34 clearer => '_clear_config_file',
35 );
36  
37 1 has _raw_config => (
38 is => 'ro',
39 isa => HashRef [ HashRef [Str] ],
40 lazy => 1,
41 builder => '_build_raw_config',
42 );
43  
44 0 ▶sub _build_config_file { }
Subroutine _build_config_file: not called CC=1 SCAR=0.0
45  
46 1 ▶sub _build_raw_config {
Subroutine _build_raw_config: called CC=3 SCAR=11.7
47 1 my $self = shift;
48  
49 1 ▶ my $file = $self->config_file()
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->config_file or return {} 1 0 1
50 or return {};
51  
52 1 ▶ return Config::INI::Reader->read_file($file) || {};
Condition50%1/2
'Config::INI::Reader'->read_file($file) || {}
l!l
10
MC/DC0%0/1
'Config::INI::Reader'->read_file($file) || {}
'Config::INI::Reader'->read_file($file)
Truth table50%1/2
A
A= 'Config::INI::Reader'->read_file($file)
Aresult
11
00
53 }
54  
55 7 ▶sub _from_config {
Subroutine _from_config: called CC=5 SCAR=16.4
56 7 my $self = shift;
57 7 my $section = shift;
58 7 my $key = shift;
59  
60 7 my $hash = $self->_raw_config();
61  
62 7 for my $key ( $section, $key ) {
63 14 $hash = $hash->{$key};
64  
65 14 ▶ return unless defined $hash && length $hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash and length $hash 1 13 14
Condition66%2/3
defined $hash and length $hash
!ll&&!rl&&r
1013
MC/DC50%1/2
defined $hash and length $hash
defined $hash length $hash
Truth table66%2/3
A and B
A= defined $hashB= length $hash
ABresult
0X0
100
111
66 }
67  
68 6 ▶ if ( ref $hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hash) 0 6 6
69 0 die
70 "Config for $section - $key did not resolve to a non-reference value";
71 }
72  
73 6 return $hash;
74 }
75  
76 1 ▶sub write_config_file {
Subroutine write_config_file: called CC=19 SCAR=30.0
Pod undocumented
77 1 my $self = shift;
78 1 my %p = @_;
79  
80 1 ▶ my $file = exists $p{file} ? $p{file} : $self->config_file();
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'file'} ? : 1 0 1
81  
82 1 ▶ die 'Cannot write a configuration file without a config file'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 1 1
83 unless defined $file;
84  
85 1 my @sections;
86 my %attrs_by_section;
87  
88 1 for my $attr (
89 5 sort { $a->insertion_order() <=> $b->insertion_order() }
90 7 grep { $_->can('config_section') } $self->meta()->get_all_attributes()
91 ) {
92  
93 4 push @sections, $attr->config_section();
94 4 push @{ $attrs_by_section{ $attr->config_section() } }, $attr;
95 }
96  
97 1 my $content = q{};
98  
99 1 ▶ if ( $p{generated_by} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"generated_by"}) 1 0 1
100 1 $content .= '; ' . $p{generated_by} . "\n\n";
101 }
102  
103 1 for my $section ( uniq @sections ) {
104 2 ▶ unless ( $section eq q{_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($section eq "_") 1 1 2
105 1 $content .= '[' . $section . ']';
106 1 $content .= "\n";
107 }
108  
109 2 for my $attr ( @{ $attrs_by_section{$section} } ) {
110  
111 4 my $doc;
112 4 ▶ if ( $attr->has_documentation() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_documentation) 3 1 4
113 3 $doc = autoformat( $attr->documentation() );
114 3 $doc =~ s/\n\n+$/\n/;
115 3 $doc =~ s/^/; /gm;
116 }
117  
118 4 ▶ if ( $attr->is_required() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->is_required) 1 3 4
119 1 $doc .= "; This configuration key is required.\n";
120 }
121  
122 4 ▶ if ( $attr->has_original_default() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->has_original_default) 2 2 4
123 2 my $def = $attr->original_default();
124 2 ▶ if ( length $def ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $def) 2 0 2
125 2 ▶ $def
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($def) ? : 1 1 2
126 = looks_like_number($def)
127 ? $def
128 : B::perlstring($def);
129 2 $doc .= "; Defaults to $def\n";
130 }
131 }
132  
133 4 ▶ $content .= $doc if defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc 4 0 4
134  
135 my $value
136 = exists $p{values}{ $attr->name() }
137 4 ▶ ? $p{values}{ $attr->name() }
Branch: 1 branch
BranchTrueFalseTotal
exists $p{'values'}{$attr->name} ? : 0 4 4
138 : $self->_from_config(
139 $attr->config_section(),
140 $attr->config_key(),
141 );
142  
143 4 my $key = $attr->config_key();
144  
145 4 ▶ if ( defined $value && length $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and length $value) { } 3 1 4
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
103
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
146 3 $content .= "$key = $value\n";
147 }
148 else {
149 1 $content .= "; $key =\n";
150 }
151  
152 4 $content .= "\n";
153 }
154 }
155  
156 1 my $fh;
157  
158 1 ▶ if ( ref $file eq 'GLOB' || ref(\$file) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'GLOB' or ref \$file eq 'GLOB') { } 1 0 1
Condition33%1/3
ref $file eq "GLOB" or ref \$file eq "GLOB"
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $file eq "GLOB" or ref \$file eq "GLOB"
ref $file eq "GLOB" ref \$file eq "GLOB"
Truth table33%1/3
A or B
A= ref $file eq "GLOB"B= ref \$file eq "GLOB"
ABresult
1X1
011
000
159 1 $fh = $file;
160 }
161 else {
162 0 open $fh, '>', $file;
163 }
164  
165 1 print {$fh} $content;
166 1 close $fh;
167 }
168  
169 1 1;
↑ Summary Configuration/Trait/Attribute/ConfigKey.pm »