| 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
| |||||||||||||||||||||||||||||
| 50 | or return {}; | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | return Config::INI::Reader->read_file($file) || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
'Config::INI::Reader'->read_file($file) || {}
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)
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $hash and length $hash
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
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | if ( ref $hash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | die 'Cannot write a configuration file without a config file' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 1 | $doc .= "; This configuration key is required.\n"; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 4 | ▶ | if ( $attr->has_original_default() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 2 | my $def = $attr->original_default(); | |||||||||||||||||||||||||||
| 124 | 2 | ▶ | if ( length $def ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | $def | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | my $value | ||||||||||||||||||||||||||||
| 136 | = exists $p{values}{ $attr->name() } | ||||||||||||||||||||||||||||
| 137 | 4 | ▶ | ? $p{values}{ $attr->name() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $value and length $value
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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
ref $file eq "GLOB" or ref \$file eq "GLOB"
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"
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||